定义类ColumnCal,包括:成员变量:height(高),radius(半径),成员方法:setHeight(),setRadius(),实现对圆柱体的高度和底半径初始化;getArea(),getVolume(),实现对圆柱体的表面积和体积的计算;构造方法:ColumnCal(),无参数,默认值height=5,radius=5;ColumnCal(int h,int r),有参数,按实参进行相应的成员变量初始化。在主方法中对该类进行实例化(有参数和无参数),并输出相应的计算结果。
时间: 2024-03-20 20:43:27 浏览: 21
好的,这是一个关于圆柱体计算的类定义,下面是代码实现:
```java
public class ColumnCal {
private int height; //高
private int radius; //半径
public ColumnCal() { //无参数构造方法,默认值height=5,radius=5
this.height = 5;
this.radius = 5;
}
public ColumnCal(int h, int r) { //有参数构造方法
this.height = h;
this.radius = r;
}
public void setHeight(int h) { //设置高度
this.height = h;
}
public void setRadius(int r) { //设置半径
this.radius = r;
}
public double getArea() { //计算表面积
return 2 * Math.PI * radius * height + 2 * Math.PI * radius * radius;
}
public double getVolume() { //计算体积
return Math.PI * radius * radius * height;
}
}
```
在主方法中对该类进行实例化并输出计算结果:
```java
public static void main(String[] args) {
ColumnCal column1 = new ColumnCal(); //无参数实例化
System.out.println("无参数实例化:");
System.out.println("表面积:" + column1.getArea());
System.out.println("体积:" + column1.getVolume());
ColumnCal column2 = new ColumnCal(10, 3); //有参数实例化
System.out.println("有参数实例化:");
System.out.println("表面积:" + column2.getArea());
System.out.println("体积:" + column2.getVolume());
}
```
输出结果为:
```
无参数实例化:
表面积:219.9114857512855
体积:392.69908169872414
有参数实例化:
表面积:235.61944901923448
体积:282.7433388230814
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)