接口的使用

A.接口的使用
Time Limit: 1000 MSMemory Limit: 32768 K
Total Submit: 68 (40 users)Total Accepted: 38 (37 users)Special Judge: No
Description

先定义一个接口IGetArea,有一个方法computeArea,有一个常量PI为3.14。 
 然后定义一个实现该接口的Circle类,有double类型的半径属性。  
 再定义一个实现该接口的Rectangle类,有double类型的长和宽属性。  
 要求main方法中至少包含如下代码(这些语句不要求必须放在一起): 
 IGetArea iga; 
 iga=new Circle(r); 
 iga=new Rectangle(a,b);  
 iga.computeArea();


Input
第一行输入数据的组数N,然后有N组数据。每组数据由几个整型数组成,第一个是图形类型,0表示Circle,1表示Rectangle;而后如果前面是0,后面跟一个浮点,如果是1后面跟两个浮点。
Output
图形类型及其面积(精度保留2位)。
Sample Input

0 10 
1 20 10 
1 5 4 
0 20
Sample Output
Circle:314.00
Rectangle:200.00
Rectangle:20.00
Circle:1256.00



import java.util.*;

interface IGetArea
{
	double pi = 3.14;
	double computeArea();
}

class Circle implements IGetArea
{
	double r;
	public Circle(double r)
	{
		this.r = r;
	}

	public double computeArea() {
		// TODO Auto-generated method stub
		IGetArea u;
		return pi*r*r;
	}	
}

class Rectangle
{
	double l,r;
	public Rectangle(double l,double r)
	{
		this.l = l;
		this.r = r;
	}
	
	public double computeArea()
	{
		return l*r;
	}
}

public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int n = input.nextInt();
		while(n>0)
		{
			int m = input.nextInt();
			if(m == 0)
			{
				double r = input.nextDouble();
				System.out.printf("Circle:");
				Circle yuan = new Circle(r);
				System.out.printf("%.2f\n",yuan.computeArea());
			}
			else if(m == 1)
			{
				double l,r;
				l = input.nextDouble();
				r = input.nextDouble();
				System.out.printf("Rectangle:");
				Rectangle ju = new Rectangle(l,r);
				System.out.printf("%.2f\n",ju.computeArea());
			}
			
			n--;
		}
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值