设计圆和圆柱体

编写一个完整的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.Scanner;

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 getRadius(){
        return this.radius;
    }
    public double getArea(){
        return Math.PI * radius * radius;
    }
    public double getPerimeter(){
        return 2 * Math.PI * radius;
    }
    @Override
    public String toString(){
        return "Circle(r:" + radius + ")";
    }
}

class Cylinder{
    private double height;
    private Circle circle;

    public Cylinder(double height, Circle circle){
        this.height = height;
        this.circle = circle;
    }
    public Cylinder(){
        height = 0;
        circle = new Circle(0);
    }
    public void setHeight(double height){
        this.height = height;
    }
    public double getHeight(){
        return height;
    }
    public void setCircle(Circle circle){
        this.circle = circle;
    }
    public Circle getCircle(){
        return circle;
    }
    public double getArea(){
        return 2 * Math.PI * circle.getRadius() * circle.getRadius() + 2 * Math.PI * circle.getRadius() * height;
    }
    public double getVolume(){
        return Math.PI * circle.getRadius() * circle.getRadius() * height;
    }
    @Override
    public String toString(){
        return "Cylinder(h:" + height + "," + circle.toString() + ")";
    }
}
  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值