java 1.构造一个Circle类: 1)该类有一个double型成员变量radius存放半径; 2)该类有一个有参构造方法,为成员变量radius赋值; 3)该类具有getArea和getLength两个方法,能够利用半径和Math.PI计算高精度的面积和周长。 2.构造一个Column类: 1)该类有一个Circle型成员变量bottom为圆柱体的底面; 2)该类有一个double型成员变量height存放圆柱体的高; 3)该类有getBottom和setBottom方法作为成员变量bottom的访问方法和赋值方法; 4)该类有getHeight和setHeight方法作为成员变量height的访问方法和赋值方法; 5)该类有getVolume方法,计算并返回圆柱体的体积。
时间: 2023-06-19 12:07:01 浏览: 984
Java代码如下:
Circle类:
```java
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * Math.pow(radius, 2);
}
public double getLength() {
return 2 * Math.PI * radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
```
Column类:
```java
public class Column {
private Circle bottom;
private double height;
public Column(Circle bottom, double height) {
this.bottom = bottom;
this.height = height;
}
public Circle getBottom() {
return bottom;
}
public void setBottom(Circle bottom) {
this.bottom = bottom;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getVolume() {
return bottom.getArea() * height;
}
}
```
示例代码:
```java
public class Main {
public static void main(String[] args) {
Circle circle = new Circle(5.0);
System.out.println("圆的面积:" + circle.getArea());
System.out.println("圆的周长:" + circle.getLength());
Circle bottom = new Circle(3.0);
Column column = new Column(bottom, 10.0);
System.out.println("圆柱体的体积:" + column.getVolume());
}
}
```
输出结果:
```
圆的面积:78.53981633974483
圆的周长:31.41592653589793
圆柱体的体积:282.7433388230814
```
阅读全文