A.接口的使用 | |||||
| |||||
Description | |||||
先定义一个接口IGetArea,有一个方法computeArea,有一个常量PI为3.14。 | |||||
Input | |||||
第一行输入数据的组数N,然后有N组数据。每组数据由几个整型数组成,第一个是图形类型,0表示Circle,1表示Rectangle;而后如果前面是0,后面跟一个浮点,如果是1后面跟两个浮点。 | |||||
Output | |||||
图形类型及其面积(精度保留2位)。 | |||||
Sample Input | |||||
4 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--;
}
}
}