java案例_面向对象编程_Stool

一个小型家具厂主要生产小凳子,形状由上中下三个圆柱体组成,如下图所示。参照图定义类Circle,Cylinder, Stool,要求属性必须私有,根据类的数据成员定义类的构造方法(参数为数值类型》。定义测试类Program和主方法,测试计算2件凳子和3个圆柱体需要多少公斤油漆和立方米木料,假设园柱体的半径和高度均以米为单位,每平方米需要油滚О.1公斤,每立方米体积需要1.2倍的木料。

在这里插入图片描述

项目结构:
项目结构

运行结果:
运行结果

源代码:

package com.serein.stool_opp_bata;


public class Program {
    public static void main(String[] args) {
        Cylinder cylinder1 = new Stool(0.5,0.1);
        Cylinder cylinder2 = new Stool(0.2,0.4);
        Cylinder cylinder3 = new Stool(0.8,0.2);

        Stool stool4 = new Stool(); //无参构造凳子对象,以便后续调用凳子对象专有求面积体积方法

        if (cylinder1 instanceof Stool) {
            Stool stool1 = (Stool) cylinder1;
            System.out.println("第1个圆柱所需要的油漆是:" + stool1.getArea() + "公斤");
            System.out.println("第1个圆柱所需要的木料是:" + stool1.getVolume() + "立方米");
        }

        if (cylinder2 instanceof Stool) {
            Stool stool2 = (Stool) cylinder2;
            System.out.println("第2个圆柱所需要的油漆是:" + stool2.getArea() + "公斤");
            System.out.println("第2个圆柱所需要的木料是:" + stool2.getVolume() + "立方米");
        }

        if (cylinder3 instanceof Stool) {
            Stool stool3 = (Stool) cylinder3;
            System.out.println("第3个圆柱所需要的油漆是:" + stool3.getArea() + "公斤");
            System.out.println("第3个圆柱所需要的木料是:" + stool3.getVolume() + "立方米");
        }

        System.out.println("----------------------------");
        System.out.println("2个凳子需要的油漆是:" + stool4.getStoolArea(0.5, 0.1, 0.2, 0.4, 0.8, 0.2) *2 + "公斤");
        System.out.println("2个凳子需要的木料是:" + stool4.getStoolVolume(0.5, 0.1, 0.2, 0.4, 0.8, 0.2) *2 + "立方米");
    }
}

package com.serein.stool_opp_bata;

public class Circle {
    private double radius;
    public final static double PI = 3.14159;

    public double getCircumference(){
        return 2*PI*radius;
    }

    public double getArea(){
        return PI*radius*radius;
    }

    public Circle() {
    }

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }
}

package com.serein.stool_opp_bata;

public class Cylinder extends Circle {
    private double height;

    @Override
    public double getArea() {
        return super.getArea()*2 + getCircumference()*height;
    }

    public double getVolume(){
        return super.getArea()*height;
    }

    public Cylinder() {
    }

    public Cylinder(double height) {
        this.height = height;
    }

    public Cylinder(double radius, double height) {
        super(radius);
        this.height = height;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }
}

package com.serein.stool_opp_bata;

public class Stool extends Cylinder{

    @Override
    public double getArea(){
        return super.getArea() * 0.1;
    }
    @Override
    public double getVolume(){
        return super.getVolume() * 1.2;
    }


    public double getStoolVolume(double topRadius, double topHeight,double middleRadius, double middleHeight,double bottomRadius, double bottomHeight) {
        return (PI * topRadius * topRadius * topHeight + PI * middleRadius * middleRadius * middleHeight
                + PI * bottomRadius * bottomRadius * bottomHeight) * 1.2;
    }
    /**
     *计算凳子表面积时,必须记得圆柱体拼接,会有4个中间圆柱底面被遮住,不能单纯相加
     */
    public double getStoolArea(double topRadius, double topHeight,double middleRadius, double middleHeight,double bottomRadius, double bottomHeight){
        return (PI * topRadius *topRadius *2 + 2 * PI *topRadius * topHeight
                + 2 * PI *middleRadius * middleHeight
                + PI * bottomRadius *bottomRadius *2 + 2 * PI *bottomRadius * bottomHeight
                - PI * middleRadius *middleRadius *2) * 0.1;
    }

    public Stool() {
    }

    public Stool(double height) {
        super(height);
    }

    public Stool(double radius, double height) {
        super(radius, height);
    }

}

感谢大家观看!欢迎点赞评论转发哦!

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值