编程练习 P142 7.2 Java定义一个名为Cylinder类表示圆柱,它继承Circle类,要求定义一个变量height表示圆柱高度。覆盖getArea()方法求圆柱的表面积,定义......

Java定义一个名为Cylinder类表示圆柱,它继承Circle类,要求定义一个变量height表示圆柱高度。覆盖getArea()方法求圆柱的表面积,定义getVolume()方法求圆柱体,定义默认构造方法和带radius和height两个参数的构造方法。
画出Circle类和Cylinder类的UML图,并实现这些类。编写测试程序,提示用户输入圆柱的底面圆的半径和高度,程序创建一个圆柱对象,计算并输出圆柱表面积和体积.

Java定义一个名为Cylinder类表示圆柱代码如下:

// Circle类表示圆形
class Circle {
    protected double radius; // 半径

    // 构造函数
    public Circle() {
        this.radius = 0;
    }

    // 带参数的构造函数
    public Circle(double radius) {
        this.radius = radius;
    }

    // 计算圆面积
    public double getArea() {
        return Math.PI * radius * radius;
    }
}

// Cylinder类表示圆柱,继承Circle类
class Cylinder extends Circle {
    private double height; // 圆柱的高度

    // 构造函数
    public Cylinder() {
        super();
        this.height = 0;
    }

    // 带参数的构造函数
    public Cylinder(double radius, double height) {
        super(radius);
        this.height = height;
    }

    // 计算圆柱表面积
    @Override
    public double getArea() {
        return super.getArea() * 2 + 2 * Math.PI * radius * height;
    }

    // 计算圆柱体积
    public double getVolume() {
        return super.getArea() * height;
    }
}

// 测试类
public class Main {
    public static void main(String[] args) {
        // 创建一个圆柱对象,半径为3,高度为5
        Cylinder cylinder = new Cylinder(3, 5);

        // 输出圆柱的表面积
        System.out.println("圆柱的表面积为:" + cylinder.getArea());

        // 输出圆柱的体积
        System.out.println("圆柱的体积为:" + cylinder.getVolume());
    }
}

画出Circle类和Cylinder类的UML图

Circle类的UML图:
----------------------------------------
|        Circle                        |
----------------------------------------
| - radius: double                     |
| + Circle()                           |
| + Circle(radius: double)             |
| + getRadius():double                 |
| + setRadius(radius: double): void    |
| + getArea(): double                  |
----------------------------------------
Cylinder类继承自Circle类,增加了height属性和getVolume()方法:
----------------------------------------
|        Cylinder                      |
----------------------------------------
| - radius: double                     |
| - height: double                     |
| + Cylinder()                         |
| + Cylinder(radius: double, height: double)|
| + getHeight():double                 |
| + setHeight(height: double): void    |
| + getArea(): double                  |
| + getVolume(): double                |
----------------------------------------

Circle.java

public class Circle {
    private double radius;

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

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

    public double getRadius() {
        return radius;
    }

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

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

Cylinder.java

public class Cylinder extends Circle {
    private double height;

    public Cylinder() {
        super();
        this.height = 0.0;
    }

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

    public double getHeight() {
        return height;
    }

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

    // 覆盖父类的getArea()方法,求圆柱的表面积
    @Override
    public double getArea() {
        return super.getArea() * 2 + 2 * Math.PI * super.getRadius() * height;
    }

    // 求圆柱体积
    public double getVolume() {
        return super.getArea() * height;
    }
}

TestCylinder.java

import java.util.Scanner;

public class TestCylinder {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("请输入圆柱的底面圆的半径:");
        double radius = input.nextDouble();

        System.out.print("请输入圆柱的高度:");
        double height = input.nextDouble();

        Cylinder cylinder = new Cylinder(radius, height);

        System.out.println("圆柱的表面积为:" + cylinder.getArea());
        System.out.println("圆柱的体积为:" + cylinder.getVolume());

        input.close();
    }
}

运行结果:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_39719874

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值