假设有若干 (如10)个Circle,Rectangle和Square形状,计算总面积
今天小编给各位分享rectangle的知识,文中也会对其通过假设有若干 (如10)个Circle,Rectangle和Square形状,计算总面积和java编程等多篇文章进行知识讲解,如果文章内容对您有帮助,别忘了关注本站,现在进入正文!
内容导航:
一、假设有若干 (如10)个Circle,Rectangle和Square形状,计算总面积
package OctoberTwentyfour;
abstract class Shape{
protected String type;
protected double area, perim;
public abstract void computeArea();
public String toString()
{ return type + ":Area = " + area + "Perimeter = " + perim;
}
}//父类
package OctoberTwentyfour;
class Rectangle extends Shape{
protected double width, height;
public Rectangle(double _width, double _height)
{ type = "Rectangle";
width = _width;
height = _height;
}
public void computeArea()
{ area = width * height; }
public void computePerimeter()
{ perim = 2*width + 2*height; }
public String toString()
{ return super.toString() + "Width = " + width + "Height = " + height;
}
}//矩形子类
package OctoberTwentyfour;
class Circle extends Shape
{ protected double radius;
public Circle(double _radius)
{ type = "Circle";
radius = _radius;
}
public void computeArea()
{ area = Math.PI * radius * radius; }
public void computePerimeter()
{ perim = 2.0 * Math.PI * radius; }
public String toString()
{ return super.toString() +"Radius = " + radius;
}
}
//圆子类
package OctoberTwentyfour;
class Square extends Rectangle
{ public Square(double _side)
{ super(_side, _side);//重写父类方法super
type = "Square";
}
}
//继承于矩形的正方形子类
package OctoberTwentyfour;
public class ShapeTest
{ public static void main(String args[])
{ Shape shapes[] = new Shape[10];
double total_area=0.0;
int k=0;
for (int i=0; i<shapes.length; i++ ){
k=(int)Math.ceil(Math.random()*3);
System.out.println("k is :"+k);
switch (k) {
case 1: shapes[i]=new Circle(Math.random()*10);
break;
case 2: shapes[i]=new Rectangle(Math.random()*10, Math.random()*10);
break;
case 3: shapes[i]=new Square(Math.random()*10);
break;
}
}
for (int i = 0; i < shapes.length; i++) {
shapes[i].computeArea();
total_area+=shapes[i].area;
}
System.out.println("Total area is :"+total_area);
}
}
//测试
一、java编程
//亲手为你所写abstract class Shape{
public abstract double area();
}
class Circle extends Shape{
int r;
final double pi = 3.14;
public Circle(int r){
this.r= r;
}
public double area(){
return pi*r*r;
}
}
class Rectang extends Shape{
int w;
int h;
public Rectang(int w,int h){
this.w = w;
this.h = h;
}
public double area(){
return w*h;
}
}
public class Test{
public static void main(String[] args){
double area_total=0;
int i;
Circle c1 = new Circle(2);
Circle c2 = new Circle(3);
Rectang rg1 = new Rectang(2,3);
Rectang rg2 = new Rectang(3,4);
Shape sshape[] = {c1,c2,rg1,rg2};
for (i=0; i *pvec )
{
// ...
for ( ; it != end_it; ++it )
{
Query *pq = *it;
// ...
delete pq;
}
}
为了使函数能够正确执行,在应用delete 表达式时,必须调用pq 指向的动态类型的析构函数。为此,必须把Query 类析构函数声明为虚拟的。
关于rectangle的问题,通过《java类求N个图形的面积和》、《应用纯虚函数的使用,定义几个派生类如三角形,矩形,圆形等,求出所有形状的总面积。》等文章的解答希望已经帮助到您了!如您想了解更多关于rectangle的相关信息,请到本站进行查找!
爱资源吧版权声明:以上文中内容来自网络,如有侵权请联系删除,谢谢。