实验5-抽象类与接口实验-计科编程题5. 抽象类和接口8_2形状接口

【问题描述】

定义接口Shape,其中包括一个方法size(),设计矩形、圆、圆柱体等类实现Shape接口,其size()方法分别表示计算矩形面积、圆面积、圆柱体的体积(圆面积*高);分别创建代表矩形、圆、圆柱体的3个对象存入一个Shape类型的数组中,通过调用size()方法将数组中各类图形的大小输出。(注:圆面积用Math.PI计算)

【输入形式】

输入一行,输入矩形长、宽,圆半径,圆柱体高,空格分隔。
【输出形式】

输出各类图形的大小。保留两位小数。

【样例输入】

3.5 6.8 2 7

【样例输出】

23.80

12.57

87.96

package www;
import java.util.*;
import java.lang.Math;
interface Shape{
	 double size();
}
class Rectangle implements Shape{
	double width,heigth;
	public Rectangle(double width,double heigth) {
		this.width=width;
		this.heigth=heigth;
	}
	public double size() {
		return width*heigth;
	}
}
class Circle implements Shape{
	double radious;
	public Circle() {
	}
	public Circle(double radious) {
		this.radious=radious;
	}
	public double size() {
		return Math.PI*radious*radious;
	}
}
class Cylinder implements Shape {
	double heigth,radious;
	public Cylinder(double radious,double heigth) {
		this.heigth=heigth;
		this.radious=radious;
	}
	public double size() {
		return Math.PI*radious*radious*heigth;
	}
}
public class Main {

	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		double x=scanner.nextDouble();
		double y=scanner.nextDouble();
		double z=scanner.nextDouble();
		double w=scanner.nextDouble();
		Shape[] a=new Shape[3];
		a[0]=new Rectangle(x,y);
		a[1]=new Circle(z);
		a[2]=new Cylinder(z,w);
		for(int i=0;i<3;i++) {
			System.out.printf("%.2f\n",a[i].size());
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值