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();
}
}
运行结果: