图形体积计算

Description
先定义一个接口IComVolume,有一个方法computeVolume,有一个常量PI为3.14。
然后定义一个实现该接口的圆柱体Cylinder类,有double类型的半径和高两个属性。
再定义一个实现该接口的长方体Block类,有double类型的长和宽、高三个属性。
要求main方法中至少包含如下代码(这些语句不要求必须放在一起):
IComVolume icv;
icv=new Cylinder(r,h);
icv=new Block(a,b,c);
icv.computeVolume();
Input
第一行输入数据的组数N,然后有N组数据。每组数据由一个字符和几个浮点数组成,第一个是图形类型,c表示Cylinder,b表示Block;如果前面是c,后面跟两个浮点,分别为圆柱底半径和高;如果是b后面跟三个浮点,分别为长宽高。
Output
图形类型及其体积(精度保留2位)。
Sample Input
4
c 10 10
b 20 10 10
b 5 4 5
c 20 5
Sample Output
Cylinder:3140.00
Block:2000.00
Block:100.00
Cylinder:6280.00


import java.util.*;
public class Main {

	public static void main(String[] args) {


	Scanner in=new Scanner(System.in);
int t;
double r,h ,a,b,c;
t=in.nextInt();
IComVolume icv;
for(int i=0;i<t;i++)
{
	char m;
	m=in.next().charAt(0);
	if(m=='c')
	{
		r=in.nextDouble();
		h=in.nextDouble();
		icv=new Cylinder(r,h); 
		r=icv.computeVolume();
		System.out.printf("Cylinder:%.2f\n",r);
	}
	if(m=='b')
	{
		a=in.nextDouble();
		b=in.nextDouble();
		c=in.nextDouble();
		icv=new Block(a,b,c); 
		a=icv.computeVolume();
		System.out.printf("Block:%.2f\n",a);
	}
}

}}
abstract class IComVolume//抽象图形父类
{
	public abstract double computeVolume();//抽象方法
	public static final float PI=(float) 3.14;
}
class Cylinder extends IComVolume{
	private double radius;
	private double high;
	public Cylinder(double radius,double high)//构造方法
	{
		this.radius=radius;
		this.high=high;
		
	}
	@Override
	public double computeVolume() {
		// TODO Auto-generated method stub
		return PI*radius*radius*high;
	}
}
 class Block extends IComVolume{
private double a;
private double b;
private double c;
public Block(double a,double b,double c) 
{
	this.a=a;
	this.b=b;
	this.c=c;
}
	@Override
	public double computeVolume() {
		// TODO Auto-generated method stub
		return a*b*c;
	}
	 
 }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不关我事~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值