Home > Back-end >  For this to do?
For this to do?

Time:09-26

The Shape of a given as shown, Circle and Rectangle classes of UML diagrams, the Shape is an abstract class, its getArea () method is abstract, Circle and Rectangle is a subclass of Shape,
Write programs in com. The demo package defined in the class, write the Test in the same package, in the main () method to write the following code:


Double d=0.0;

Shape [] shapes=new Shape [5];

for(int i=0; i
D=Math. The random ();

If (d> 0.5) {

Shapes [I]=new Circle (1);

} else {

Shapes [I]=new Rectangle (1, 2);

}

}

Double sumArea=sum (shapes);

System. The out. Printf (". The area of the shape=% 2 f, sumArea);

}

Here: sum (Shpae shapes []) method is to Test the static method of a class, you need to define the method,

Below is the application of a run results:

The shape of the total area=12.28
  • Related