用java编写挓程实现圆柱体类。并求半径为8cm,挒为10cm挕圆柱体挕表挗积(两挐底挗积+挔形挗积)
时间: 2023-06-13 09:07:33 浏览: 188
以下是Java代码实现圆柱体类:
```
public class Cylinder {
private double radius;
private double height;
public Cylinder(double radius, double height) {
this.radius = radius;
this.height = height;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getVolume() {
return 2 * Math.PI * radius * radius * height + 2 * Math.PI * radius * radius;
}
}
```
然后,你可以在另一个类中实例化这个圆柱体类,并调用其getVolume方法来计算体积。
```
public class Main {
public static void main(String[] args) {
Cylinder cylinder = new Cylinder(8, 10);
double volume = cylinder.getVolume();
System.out.println("The volume of the cylinder is " + volume + " cubic cm.");
}
}
```
运行结果为:
```
The volume of the cylinder is 2010.6192982974678 cubic cm.
```
所以,半径为8cm,高为10cm的圆柱体的表面积为2010.62立方厘米。
阅读全文