规定 输入1为计算圆形 2 则三角形 0 则结束程序
代码示例:
package test;
import java.util.Scanner;
public class areas{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
Circ sd=new Circ() ;
int y=0;
double l=0,o=0,p=0;
Rectangle re=new Rectangle();
int h=0;
while(s.hasNext())
{
h=s.nextInt();
if(h==1)
{
y++;
int k=s.nextInt();
int n=s.nextInt();
String str=s.next();
boolean st=s.nextBoolean();
Circ ds=new Circ(str,st,k) ;
o=ds.getArea();
System.out.printf("Circle area is:%.6f color is:%s\n",ds.getArea(),ds.getcolor());
}
else if(h==2)
{
y++;
int k1=s.nextInt();
int n1=s.nextInt();
String str1=s.next();
boolean st1=s.nextBoolean();
Rectangle er=new Rectangle(str1,st1,k1,n1);
p=er.getArea();
System.out.printf("Rectangle area is:%.6f color is:%s\n",er.getArea(),er.getcolor());
}
else
break;
h=0;
l=o+p+l;
o=0;
p=0;
}
System.out.println("----------");
System.out.printf("%d objects, total area are: %.6f",y,l);
}
}
abstract class puji {
private String color;
private boolean filled;
public puji()
{
}
public puji(String yan,boolean fill)
{
this.color=yan;
this.filled=fill;
}
public abstract double getArea();
public void setcolor(String yan)
{
color=yan;
}
public void setfilled(boolean filled)
{
filled=filled;
}
public String getcolor()
{
return color;
}
public boolean getfilled()
{
return filled;
}
}
class Circ extends puji {
public int r;
public Circ()
{
}
public Circ(String yan,boolean fill,int r)
{
super(yan,fill);
this.r=r;
}
public double getArea()
{
return Math.PI*r*r;
}
}
class Rectangle extends puji
{
public int l,w;
public Rectangle()
{
}
public Rectangle(String yan,boolean fill,int l,int w)
{
super(yan,fill);
this.l=l;
this.w=w;
}
public Rectangle(int l,int w)
{
this.l=l;
this.w=w;
}
public double getArea()
{
return l*w;
}
}