Employee c3=new PieceWorker(\王\五\
System.out.println(\王五月工资:\ Employee c4=new HourlyWorker(\刘\三\
System.out.println(\刘三月工资:\} }
(8)public class Circle { String shape; double r;
double height; double pi;
public Circle(String shape,double r,double height,double pi) {this.shape=shape; this.height=height; this.r=r; this.pi=pi;} }
public interface Volume {
public abstract double area();
public abstract double NewVolume(); }
public class Yuanzhu extends Circle implements Volume {
public Yuanzhu(String shape, double r, double height, double pi) { super(shape, r, height, pi); } public double area() { return pi*r*r;} public double NewVolume() {return area()*height;} }
public class Yuanzhui extends Yuanzhu implements Volume {
public Yuanzhui(String shape, double r, double height, double pi) { super(shape, r, height, pi);
// TODO Auto-generated constructor stub }
double s;
public double area()
{s=Math.sqrt(height*height+r*r); return pi*r*s+pi*r*r;} public double NewVolum()
{return 1.0/3*pi*r*pi*r*height;} }
public class Test {
public static void main(String[] args) { Yuanzhu c1=new Yuanzhu(\圆柱\
24
Yuanzhui c2=new Yuanzhui(\圆锥\ System.out.println(\圆柱表面积:\ System.out.println(\圆柱体积:\ System.out.println(\圆锥表面积:\ System.out.println(\圆锥体积:\}
(9) public interface CanFly {//定义接口CanFly public void fly(); }
public class Plane implements CanFly{//使用接口 @Override
public void fly() {
// TODO Auto-generated method stub
System.out.println(\飞机借助螺旋桨飞上天空\ } }
public class Bird implements CanFly{ @Override
public void fly() {
// TODO Auto-generated method stub
System.out.println(\小鸟 借助翅膀飞上天空\ } }
public class Test {
static void makeFly(CanFly obj){ obj.fly(); }
public static void main(String[] args) { // TODO Auto-generated method stub CanFly p =new Plane(); makeFly(p);
CanFly b =new Bird(); makeFly(b); } }
(10) import java.io.*; public class Ex1 {
public static void main(String args[ ]) {
try{
BufferedReader strin=new BufferedReader(
new InputStreamReader(System.in));//建立输入流缓冲区 System.out.print(\请输入除数:\
String cl=strin.readLine();//键盘输入
25
int a=Integer .parseInt(cl);
System .out .print(\请输入被除数:\ cl=strin .readLine();
int b=Integer .parseInt(cl); int c=b/a;
System .out .println(\商为:\ }
//捕获与I/O有关的异常(空白处补全捕获语句) catch(IOException e)
{System.out.println(\输入输出异常\ //捕获数值转化时的异常,如不能将字符转化成数值 catch(NumberFormatException e)
{System.out.println(\数值格式异常,重新输入\ }
//捕获除数为0的异常
catch(ArithmeticException e)
{System.out.println( \除数为0,重新输入\ } } (11)(1)MyException类: package exp2_11;
public class MyException extends Exception{ MyException (String msg){ super(msg); } }
(2)Div主类: package exp2_11; import java.io.*; public class Div {
public static void main(String args[]) throws MyException{ try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print(\请输入实数除法运算的被除数:\ String str = in.readLine();
double a = Double.parseDouble(str); System.out.print(\请输入除数:\ str = in.readLine();
double b = Double.parseDouble(str);
System.out.println(\商结果:\ }
catch(ArithmeticException e1){
System.out.println(\商结果:Infinity\
26
System.out.println(\商结果:NaN\ }
catch(NumberFormatException e2){
System.out.println(\异常:字符串不能转换成整数!\ }
catch(IOException e3){
System.out.println(\异常:IO异常\ }
finally{
System.out.println(\程序结束!\ } }
static double division(double a,double b)throws MyException{ if(a==100 && (b==4 || b==13))
throw new MyException(\不符规范\ else return (a/b); } }
四、实验结果与分析(程序运行结果及其分析)
(1)
去掉Man类的public修饰, 程序运行不出来,提示缺少Man的公开方法。 (2)
(3)
(4)
27
(5)
(6)
(7)
(8)
(9)
(10)
28