问题标题:
【定义一个抽象类SHAPE,在其中声明计算面积AREA()和周长PERINETER()方法,定义矩形类RECTANGLE和圆形类CI】
问题描述:
定义一个抽象类SHAPE,在其中声明计算面积AREA()和周长PERINETER()方法,定义矩形类RECTANGLE和圆形类CI
来秀海回答:
我有个类似的,你看一下
importjava.awt.*;
importjava.applet.*;
abstractclassShapes
{
protectedintx,y,k;
protecteddoublem;
publicShapes(intx,inty,intk,doublem)
{this.x=x;this.y=y;
this.k=k;this.m=m;
}
abstractpublicdoublegetArea();
abstractpublicdoublegetPerimeter();
}
classRectextendsShapes
{publicdoublegetArea()
{return(k*m);}
publicdoublegetPerimeter()
{return(2*k+2*m);}
publicRect(intx,inty,intwidth,intheight)
{super(x,y,width,height);}
}
classTriangleextendsShapes
{publicdoublegetArea()
{return(Math.sqrt(m*(m-k)*(m-x)*(m-y)));}
publicdoublegetPerimeter()
{return(k+x+y);}
publicTriangle(intbaseA,intbaseB,intbaseC)
{super(baseA,baseB,baseC,0);
m=(baseA+baseB+baseC)/2.0;
}
}
classCircleextendsShapes
{publicdoublegetArea()
{return(m*m*Math.PI);}
publicdoublegetPerimeter()
{return(2*Math.PI*m);}
publicCircle(intx,inty,intwidth)
{
super(x,y,width,width/2.0);
}
}
publicclassRunShapeextendsApplet
{Rectrect=newRect(5,15,25,25);
Triangletri=newTriangle(5,5,8);
Circlecir=newCircle(13,90,25);
publicvoidpaint(Graphicsg)
{
g.drawRect(rect.x,rect.y,rect.k,(int)rect.m);
g.drawString("RectArea:"+rect.getArea(),50,35);
g.drawString("RectPerimeter:"+rect.getPerimeter(),50,55);
g.drawString("TriangleArea:"+tri.getArea(),50,75);
g.drawString("TrianglePerimeter:"+tri.getPerimeter(),50,95);
g.drawOval(cir.x-(int)cir.k/2,cir.y-(int)cir.k/2,cir.k,cir.k);
g.drawString("CircleArea:"+cir.getArea(),50,115);
g.drawString("CirclePerimeter:"+cir.getPerimeter(),50,135);
}
}
点击显示
其它推荐
热门其它推荐