编辑代码

// 创建一个圆类Circle
class Circle {
    // 私有属性半径radius
    private double radius;
    // 静态常量PI=3.1415
    private static final double PI = 3.1415;
    // 构造函数
    public Circle(double radius) {
        this.radius = radius;
    }
    // 计算圆面积的方法area()
    public double area() {
        return PI * radius * radius;
    }
}

// 创建一个圆柱体类Cylinder,继承于Circle
class Cylinder extends Circle {
    // 私有属性高度height
    private double height;
    // 构造函数
    public Cylinder(double radius, double height) {
        super(radius);
        this.height = height;
    }
    // 重载方法area(),计算圆柱体的底面积
    public double area(double radius) {
        return super.area();
    }
    // 重载方法area(),计算圆柱体的表面积
    public double area(double radius, double height) {
        return 2 * super.area() + 2 * PI * radius * height;
    }
}

// 主函数
public class Main {
    public static void main(String[] args) {
        // 从控制台获取输入
        Scanner scanner = new Scanner(System.in);
        double radius1 = scanner.nextDouble();
        double radius2 = scanner.nextDouble();
        double height = scanner.nextDouble();
        // 创建1个圆柱体类实例对象
        Cylinder cylinder = new Cylinder(radius1, height);
        // 调用计算圆柱体的底面积的方法area()
        double area1 = cylinder.area(radius1);
        // 创建1个圆柱体类实例对象
        Cylinder cylinder2 = new Cylinder(radius2, height);
        // 调用计算圆柱体的表面积的方法area()
        double area2 = cylinder2.area(radius2, height);
        // 输出圆的面积和圆柱表面积,结果保留1位小数
        System.out.printf("%.1f %.1f", area1, area2);
    }
}