abstract class Shape { // Placeholder abstract public double getArea( ); } interface Stretchable { abstract public void stretch( ); abstract public void stretch( double expandFactor ); } interface Colorable { abstract public String getColor( ); abstract public void setColor( String color ); } class Circle extends Shape { public static final double PI = 3.14; public Circle( double r ) { radius = r; } public String toString( ) { return "Circle with radius " + radius; } public double getArea( ) { return PI * radius * radius; } private double radius; } class Ellipse extends Shape implements Stretchable { public Ellipse( double len, double wid ) { length = len; width = wid; } public void stretch( ) { stretch( 2.0 ); } public void stretch( double expandFactor ) { length *= expandFactor; } public double getArea( ) { return Circle.PI / 4 * length * width; } public String toString( ) { return "Ellipse with dimensions " + length + "*" + width; } double length; double width; } class Rectangle extends Shape implements Stretchable, Colorable { public Rectangle( double len, double wid, String c ) { length = len; width = wid; color = c; } public Rectangle( double len, double wid ) { this( len, wid, "blue" ); } public void stretch( ) { stretch( 2.0 ); } public void stretch( double expandFactor ) { length *= expandFactor; } public String getColor( ) { return color; } public void setColor( String c ) { color = c; } public double getArea( ) { return length * width; } public String toString( ) { return "Rectangle with dimensions " + length + "*" + width + " with color " + color; } private double length; private double width; private String color; } class Square extends Shape implements Colorable { public Square( double s, String c ) { side = s; color = c; } public Square( double s ) { this( s, "green" ); } public String toString( ) { return "Square with side " + side + " with color " + color; } public String getColor( ) { return color; } public void setColor( String c ) { color = c; } public double getArea( ) { return side * side; } private double side; private String color; } class Day14 { public static double getTotalArea( Shape [ ] arr ) { double sum = 0.0; for( Shape s : arr ) sum += s.getArea( ); return sum; } public static void stretchAll( Stretchable [ ] arr ) { for( Stretchable s : arr ) s.stretch( ); } public static void stretchAll( Shape [ ] arr ) { for( Shape s : arr ) if( s instanceof Stretchable ) ((Stretchable)s).stretch( ); } public static void color( Shape [ ] arr, String color ) { for( Shape s : arr ) if( s instanceof Colorable ) ((Colorable)s).setColor( color ); } public static void printArray( Object [ ] arr, String str ) { for( int i = 0; i < arr.length; i++ ) System.out.println( str + "[" + i + "] = " + arr[ i ] ); } public static void main( String [ ] args ) { Circle [ ] arr1 = { new Circle( 2 ), new Circle( 4 ) }; Square [ ] arr2 = { new Square( 2 ), new Square( 4 ) }; printArray( arr1, "Circle" ); System.out.println( "Total area is: " + getTotalArea( arr1 ) ); printArray( arr2, "Square" ); System.out.println( "Total area is: " + getTotalArea( arr2 ) ); Shape [ ] arr3 = { arr1[ 0 ], arr2[ 0 ], arr1[ 1 ], arr2[ 1 ], new Rectangle( 4, 5 ), new Ellipse( 3, 6 ) }; printArray( arr3, "Shape" ); System.out.println( "Total area is: " + getTotalArea( arr3 ) ); stretchAll( arr3 ); printArray( arr3, "Shape" ); System.out.println( "Total area is: " + getTotalArea( arr3 ) ); color( arr3, "white" ); printArray( arr3, "Shape" ); } }