/*2.12
设计实现一个MyGraphic类及子类,它们代表一些基本图形,这些图形包括矩形、三角形、圆、椭圆、菱形、梯形等。
试给能描述这些图形所必需的属性及必要的方法
**/
public class MyGraphic {
String lineColor;//线条颜色
String fillColor;//填充颜色
MyGraphic(String lc,String fc){//图形构造方法
this.lineColor=lc;
this.fillColor=fc;
}
void print()
{ System.out.println("line color is "+this.lineColor+"\t fill color is "+this.fillColor); }
public static void main(String args[]){
float rd=(float)4.5;
MyCircle mc =new MyCircle(rd,"black","white");//定义“黑边白心”圆
MyRectangle mr =new MyRectangle(4,6,"red","blue");//定义“红边蓝边”矩形
MyEllipse me=new MyEllipse(3,4,"green","gray");
System.out.println("Circle info: ");
mc.print();
System.out.println("circumference is "+mc.calCircum());//圆周长
System.out.println("square is "+mc.calSquare());//圆面积
System.out.println("Rectangle info: ");
mr.print();
System.out.println("circumference is "+mr.calCircum());//矩形周长
System.out.println("square is "+mr.calSquare());//矩形面积
System.out.println("Ellipse info: ");
me.print();
System.out.println("circumference is "+me.calCircum());//矩形周长
System.out.println("square is "+me.calSquare());//矩形面积
}
}
class MyRectangle extends MyGraphic{
float rLong;
float rWidth;
MyRectangle (float rl, float rw, String lc, String fc){
super(lc,fc);
this.rLong=rl;
this.rWidth=rw;
}
float calCircum(){
return ((float)((this.rLong+this.rWidth))*2);
}
float calSquare(){
return ((float)(this.rLong*this.rWidth));
}
};
class MyCircle extends MyGraphic{
float radius;
float rWidth;
MyCircle (float rd,String lc, String fc){
super(lc,fc);
this.radius=rd;
}
float calCircum(){
return ((float)(this.radius*3.14*2));
}
float calSquare(){
return ((float)(this.radius*this.radius*3.14));
}
};
class MyEllipse extends MyGraphic{
float ra;
float rb;
MyEllipse (float a,float b,String lc,String fc){
super(lc,fc);
this.ra=a;
this.rb=b;
}
float calCircum(){// L=2πb+4(a-b
return ((float)(this.rb*2*3.14+4*(this.ra-this.rb)));}
float calSquare(){return ((float)(this.ra*rb*3.14));}
};