class ShapeDemo // Fig 4.11 & 4.12, pg 104-5
{
public static
double totalArea( Shape [ ] arr )
{
double
total = 0;
for( int
i = 0; i < arr.length; i++ )
{
if(
arr[ i ] != null )
total += arr[ i ].area( );
}
return
total;
}
public static void printAll( Shape [ ] arr )
{
for( int
i = 0; i < arr.length; i++ )
System.out.println( arr[ i ] );
}
public static
void main( String [ ] args )
{
Shape [ ]
a = { new Circle( 2.0 ), new Rectangle( 1.0, 3.0 ),
null, new Square( 2.0 ) };
System.out.println( "Total area = " + totalArea( a ) );
System.out.println( "Total semiperimeter = " +
totalSemiperimeter(
a ) );
printAll(
a );
}
}
public abstract class Shape
{
public
abstract double area( );
public
abstract double perimeter( );
public double
semiperimeter( )
{ return perimeter( ) / 2; }
}