圆柱体面积和体积求解

(设计题)造一个凳子(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());
    }
}

##运行结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值