java构造一个圆类 Circle 内置 字段 PI=3.14, 半径radius, 求其面积的方法和求其周长的方法,在测试程序中构造该类的对象,并给半径赋值,求出其周长和面积。 在Circle类的基础上构造一个圆柱体类Cylinder,添加高度字段height ,在测试程序中创建该圆柱体类对象,给半径和高度赋值,求其底面圆周长及面积以及该圆柱体的体积
时间: 2023-05-20 21:04:07 浏览: 125
好的,以下是您的回答:
Circle类的代码:
```
public class Circle {
public static final double PI = 3.14;
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return PI * radius * radius;
}
public double getPerimeter() {
return 2 * PI * radius;
}
}
```
测试程序的代码:
```
public class Test {
public static void main(String[] args) {
Circle circle = new Circle(5);
System.out.println("圆的面积为:" + circle.getArea());
System.out.println("圆的周长为:" + circle.getPerimeter());
Cylinder cylinder = new Cylinder(5, 10);
System.out.println("圆柱体的底面圆的面积为:" + cylinder.getBottomArea());
System.out.println("圆柱体的底面圆的周长为:" + cylinder.getBottomPerimeter());
System.out.println("圆柱体的体积为:" + cylinder.getVolume());
}
}
```
Cylinder类的代码:
```
public class Cylinder extends Circle {
private double height;
public Cylinder(double radius, double height) {
super(radius);
this.height = height;
}
public double getBottomArea() {
return super.getArea();
}
public double getBottomPerimeter() {
return super.getPerimeter();
}
public double getVolume() {
return super.getArea() * height;
}
}
```
输出结果:
```
圆的面积为:78.5
圆的周长为:31.400000000000002
圆柱体的底面圆的面积为:78.5
圆柱体的底面圆的周长为:31.400000000000002
圆柱体的体积为:392.5
```
希望这个回答能够帮到您!
阅读全文