Java圆柱体面积体积求解
(设计题)造一个凳子(stool)是由三个圆柱体组成,分别表示它的 顶部,中部和底部。计算凳子的体积和表面积。 请设计UML图(包括类以及类的关系,可以省略方法和属性),并完成代码。
编写测试代码,创建一个顶部半径为3.0,高度为1的圆柱体,创建一个半径为1.5,高度为5的圆柱体,创建一个底部半径为4.0,高度为1的圆柱体的凳子。求这个凳子的表面积和体积。运行代码截图如下:
package test;
public class Stool {
private double top_r;
private double top_h;
private double middle_r;
private double middle_h;
private double botton_r;
private double botton_h;
public Stool(double top_r, int top_h, double middle_r, int middle_h, double botton_r, int botton_h) {
this.top_r=top_r;
this.top_h=top_h;
this.middle_r=middle_r;
this.middle_h=middle_h;
this.botton_r=botton_r;
this.botton_h=botton_h;
}
public double getTop_r() {
return top_r;
}
public void setTop_r(double top_r) {
this.top_r = top_r;
}
public double getTop_h() {
return top_h;
}
public void setTop_h(double top_h) {
this.top_h = top_h;
}
public double getMiddle_r() {
return middle_r;
}
public void setMiddle_r(double middle_r) {
this.middle_r = middle_r;
}
public double getMiddle_h() {
return middle_h;
}
public void setMiddle_h(double middle_h) {
this.middle_h = middle_h;
}
public double getBotton_r() {
return botton_r;
}
public void setBotton_r(double botton_r) {
this.botton_r = botton_r;
}
public double getBotton_h() {
return botton_h;
}
public void setBotton_h(double botton_h) {
this.botton_h = botton_h;
}
public double getArea(){
double Area=(top_r*top_r+botton_r*botton_r-middle_r*middle_r)*Math.PI*2+(top_h*top_r-middle_h*middle_r+botton_r*botton_h)*Math.PI*2;
return Area;
}
//计算面积
public double getVol(){
double Vol=Math.PI*(top_r*top_r*top_h+middle_r*middle_r*middle_h+botton_h*botton_r*botton_r);
return Vol;
}
@Override
public String toString() {
return "Stool{" +
"top_r=" + top_r +
", top_h=" + top_h +
", middle_r=" + middle_r +
", middle_h=" + middle_h +
", botton_r=" + botton_r +
", botton_h=" + botton_h +
'}';
}
}
##测试类
package test;
public class test1 {
public static void main(String[] args) {
Stool stool=new Stool(3.0,1,1.5,5,4.0,1);
System.out.println(stool.toString());
System.out.println("Area is "+stool.getArea());
System.out.println("Vol is "+stool.getVol());
}
}
##运行结果