关于#java#的问题:radius、h、PI,成员方法 volume()能够计算圆柱体的体积

请编写一个程序,定义一个Circle类,成员属性:radius、h、PI,成员方法 volume()能够计算圆柱体的体积。要求圆周率PI定义为最终的变量,值为3.14,赋值的3个方式都写一下。

有测试类哈


class Circle {
    private double radius;
 
    public Circle() {
    }
 
    public Circle(double radius) {
        this.radius = radius;
    }
 
    //求圆的面积
    public double getArea() {
        return 3.14 * radius * radius;
    }
 
    //求圆的周长
    public double getPerimeter() {
        return 2 * 3.14 * radius;
    }
 
    public void show() {
        System.out.printf("半径为:" + radius + ",周长为:" + getPerimeter() + ",面积为:" + getArea());
    }
}
 
public class Cylinder extends Circle {
    private double hight;
 
    public Cylinder(double radius, double hight) {
        super(radius);
        this.hight = hight;
    }
 
    //获取圆柱体的体积
    public double getVolume() {
        return getArea() * hight;
    }
 
    //将圆柱体的体积输出到屏幕
    public void showVolume() {
        System.out.println("圆柱体的体积为:" + getVolume());
    }
}
 
class Demo{
    public static void main(String[] args) {
        //创建一个圆柱体对象
        Cylinder cylinder = new Cylinder(10.0, 10.0);
        cylinder.showVolume();
        cylinder.show();
    }
}

翻译题,汉语翻译java

public class Circle {
    private int radius;// 半径
    private int h;// 高
    final static  double PI=3.14;
    protected Circle(int radius, int h) {
        super();
        this.radius = radius;
        this.h = h;
    }
    
    public void setRadius(int radius) {
        this.radius = radius;
    }

    public void setH(int h) {
        this.h = h;
    }

    public double volume() {
        return PI*radius*radius*h;
    }
}