2021-06-23

Java——设计圆和圆柱体

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()
	{
		this.radius=0;
	}
	public void setRadius(double radius)
	{
		this.radius=radius;
	}
	public double getRadius()
	{
		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 extends Circle
{
	private double height;
	private Circle circle;
	private double radius;
	public Cylinder(double radius)
	{
		super(radius);
	}
	public Cylinder(double height,Circle circle)
	{
		this.height=height;
		this.circle=circle;
	}
	public Cylinder()
	{
		this.height=0;
		this.circle=null;
	}
	public void setHeight(double height)
	{
		this.height=height;
	}
	public void setCircle(Circle circle)
	{
		this.circle=circle;
	}
	public double getHeight()
	{
		return height;
	}
	public Circle getCircle()
	{
		return circle;
	}
	public double getArea()
	{
		return circle.getArea()*2+circle.getPerimeter()*this.height;
	}
	public double getVolume()
	{
		//double s=(double)circle;
		//circle=(double)circle;
		return circle.getArea()*this.height;
	}
	public String toString()
	{
		return "Cylinder(h:"+this.height+","+this.circle.toString()+")";
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值