7-1 设计圆和圆柱体

切换布局

作者 殷伟凤

单位 浙江传媒学院

编写一个完整的Java Application 程序。包含类Circle、Cylinder、Main,具体要求如下。

(1)编写类Circle,表示圆形对象,包含以下成员
①属性:
1)  radius:私有,double型,圆形半径;
②方法:
1)  Circle(double radius), 构造方法,用参数设置圆的半径
2)  Circle(),构造方法,将圆形初始化为半径为0。
3)  void setRadius(double r):用参数r设置radius的值
4)  double getRadius():返回radius的值
5)  double getArea(),返回圆形的面积
6)  double getPerimeter(),返回圆形的周长
7)  public String toString( ),将把当前圆对象的转换成字符串形式,例如圆半径为10.0,返回字符串"Circle(r:10.0)"。
(2)编写一个类Cylinder,表示圆柱形对象,包含以下成员
①属性:
1)  height:私有,double型,圆柱体高度;
2)  circle:私有,Circle类型,圆柱体底面的圆形;
②方法:
1)  Cylinder(double height,Circle circle), 构造方法,用参数设置圆柱体的高度和底面的圆形
2)  Cylinder(),构造方法,将圆柱体的高度初始化为0,底面圆形初始化为一个半径为0的圆形。
3)  void setHeight(double height):用参数height设置圆柱体的高度
4)  double getHeight():返回圆柱体的高度
5)  void setCircle(Circle circle):用参数circle设置圆柱体底面的圆形
6)  Circle getCircle():返回圆柱体底面的圆形
7)  double getArea(),重写Circle类中的area方法,返回圆柱体的表面积
8)  double getVolume(),返回圆柱体的体积
9)  public String toString( ),将把当前圆柱体对象的转换成字符串形式,例如半径为10.0,高为5.0,返回字符串"Cylinder(h:5.0,Circle(r:10.0))"。
(3)编写公共类Main,在main()方法中实现如下功能
输入一个整数n,表示有n个几何图形。
对于每一个几何图形,先输入一个字符串,“Circle”表示圆形,“Cylinder”表示圆柱体。
如果是圆形,输入一个浮点数表示其半径。要求计算其面积和周长并输出。
如果是圆柱体,输入两个浮点数分别表示其半径和高度。要求计算其面积和体积并输出。

将以下代码附在自己的代码后面:

public class Main{
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        for(int i = 0; i < n; i++) {
            String str = input.next();
            if(str.equals("Circle")) {
                Circle c = new Circle(input.nextDouble());
                System.out.println("The area of " + c.toString() + " is " + String.format("%.2f",c.getArea()));
                System.out.println("The perimeterof " + c.toString() + " is "+ String.format("%.2f",c.getPerimeter()));
            } else if(str.equals("Cylinder")) {
                Cylinder r = new Cylinder(input.nextDouble(), new Circle(input.nextDouble()));
                System.out.println("The area of " + r.toString() + " is " + String.format("%.2f",r.getArea()));
                System.out.println("The volume of " + r.toString() + " is " + String.format("%.2f",r.getVolume()));
            }
        }
    }
}

输入格式:

第一行输入一个整数n,表示有n个几何图形。
以下有n行,每行输入一个几何图形的数据。
每行先输入一个字符串表示几何图形的类型,“Circle”表示圆形,“Cylinder”表示圆柱体。
如果是圆形,输入一个浮点数表示其半径。
如果是圆柱体,输入两个浮点数分别表示其半径和高度。

输出格式:

如果是圆形,要求计算其面积和周长并输出。
如果是圆柱体,要求计算其面积和体积并输出。
注意,圆周率用Math.PI

输入样例:

在这里给出一组输入。例如:

4
Circle 10.0
Cylinder 10.0 10.0
Circle 1.1
Cylinder 1.1 3.4

输出样例:

在这里给出相应的输出。例如:

The area of Circle(r:10.0) is 314.16
The perimeterof Circle(r:10.0) is 62.83
The area of Cylinder(h:10.0,Circle(r:10.0)) is 1256.64
The volume of Cylinder(h:10.0,Circle(r:10.0)) is 3141.59
The area of Circle(r:1.1) is 3.80
The perimeterof Circle(r:1.1) is 6.91
The area of Cylinder(h:1.1,Circle(r:3.4)) is 96.13
The volume of Cylinder(h:1.1,Circle(r:3.4)) is 39.95

答案:

import java.util.*;
public class Main{
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        for(int i = 0; i < n; i++) {
            String str = input.next();
            if(str.equals("Circle")) {
                Circle c = new Circle(input.nextDouble());
                System.out.println("The area of " + c.toString() + " is " + String.format("%.2f",c.getArea()));
                System.out.println("The perimeterof " + c.toString() + " is "+ String.format("%.2f",c.getPerimeter()));
            } else if(str.equals("Cylinder")) {
                Cylinder r = new Cylinder(input.nextDouble(), new Circle(input.nextDouble()));
                System.out.println("The area of " + r.toString() + " is " + String.format("%.2f",r.getArea()));
                System.out.println("The volume of " + r.toString() + " is " + String.format("%.2f",r.getVolume()));
            }
        }
    }
}
class Circle{
 
    private double radius;
    public Circle(double radius){
        this.radius=radius;
    }
    public Circle(){
        radius=0;
    }
    public void setRadius(double r){
        radius=r;
    }
    public double  radius(){
        return radius;
    }
    public double getArea(){
        return Math.PI*radius*radius;
    }
    public double getPerimeter(){
        return 2*Math.PI*radius;
    }
    public String toString( ){
        return "Circle(r:"+this.radius+")";
    }

}
class Cylinder{
    private double height;
    private Circle circle;
    Cylinder(double height,Circle circle){
        this.height=height;
        this.circle=circle;
    }
    Cylinder(){
        height=0;
        circle.setRadius(0.0);
    }
    void setHeight(double height){
        this.height=height;
    }
    double getHeight(){
        return height;
    }
    void setCircle(Circle circle){
        this.circle=circle;
    }
    Circle getCircle(){
        return circle;
    }
    double getArea(){
        return Math.PI*circle.radius()*circle.radius()*2+2*Math.PI*circle.radius()*height;
    }
    double getVolume(){
        return circle.getArea()*height;
    }
    public String toString( ){
        return "Cylinder(h:"+height+",Circle(r:" +circle.radius()+"))";
    }
}

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你设计一个计算圆柱体的程序,并使用继承来实现代码重用。 首先,我们可以创建一个名为`Circle`的类来计算的面积和周长: ```python class Circle: def __init__(self, radius): self.radius = radius def area(self): return 3.14 * self.radius ** 2 def perimeter(self): return 2 * 3.14 * self.radius ``` 接下来,我们可以创建一个名为`Cylinder`的子类,该子类继承了`Circle`类,并添加了一个高度属性来表示圆柱体的高度。我们还可以定义一个新的方法来计算圆柱体的表面积和体积: ```python class Cylinder(Circle): def __init__(self, radius, height): super().__init__(radius) self.height = height def surface_area(self): return 2 * self.area() + 2 * 3.14 * self.radius * self.height def volume(self): return self.area() * self.height ``` 现在,我们可以创建一个和一个圆柱体对象,并调用它们的方法来计算它们的面积、周长、表面积和体积: ```python # 创建一个对象 circle = Circle(5) print("的面积:", circle.area()) print("的周长:", circle.perimeter()) # 创建一个圆柱体对象 cylinder = Cylinder(5, 10) print("圆柱体的表面积:", cylinder.surface_area()) print("圆柱体的体积:", cylinder.volume()) ``` 输出: ``` 的面积: 78.5 的周长: 31.400000000000002 圆柱体的表面积: 471.0 圆柱体的体积: 785.0 ``` 这样,我们就成功地使用继承来实现了圆柱体的计算。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值