求大神谁知道java 第二集4-3课后习题的答案 谢谢
//抽象类
public abstract class Shape {
public abstract void perimeter();
public abstract void area();
}
//矩形类
public class Rectangle extends Shape {
public double height;
public double width;
@Override
public void perimeter() {
// TODO Auto-generated method stub
System.out.println("矩形的周长是:"+(height+width)*2);
}
@Override
public void area() {
// TODO Auto-generated method stub
System.out.println("矩形的面积是:"+height*width);
}
}
//圆形类
public class Circle extends Shape {
public double radius;
public final double PI=Math.PI;
@Override
public void perimeter() {
System.out.println("圆形的周长是:"+2*PI*radius);
}
@Override
public void area() {
System.out.println("圆形的面积是:"+PI*radius*radius);
}
}
//测试类
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入矩形的长和宽:");
Rectangle rec=new Rectangle();
System.out.print("长:");
rec.height=input.nextDouble();
System.out.print("宽:");
rec.width=input.nextDouble();
rec.perimeter();
rec.area();
System.out.println("请输入圆的半径:");
Circle cir=new Circle();
cir.radius=input.nextDouble();
cir.perimeter();
cir.area();
input.close();
}
}
这是我自己写,就是对结果的精度没有做处理