class IllegalShapeException extends RuntimeException { public IllegalShapeException( ) { } public IllegalShapeException( String msg ) { super( msg ); } } interface StrechableShape { void stretch( ); void stretch( double factor ); } abstract class Shape { // PLACEHOLDER abstract public double getArea( ); } interface Colorable { String getColor( ); void setColor( String color ); } class Circle extends Shape { public static final double PI = 3.14; public Circle( double r ) { if( r < 0 ) throw new IllegalShapeException( "Negative radius! " + r ); radius = r; } public String toString( ) { return "Circle with radius " + radius; } public double getArea( ) { return PI * radius * radius; } private double radius; } class Rectangle extends Shape implements StrechableShape, Colorable { public Rectangle( double len, double wid ) { this( len, wid, "green" ); } public Rectangle( double len, double wid, String c ) { length = len; width = wid; color = c; } public String getColor( ) { return color; } public void setColor( String c ) { color = c; } // Makes one dimension larger public void stretch( ) { stretch( 2.0 ); } public void stretch( double factor ) { length *= factor; } public double getArea( ) { return length * width; } public double getLength( ) { return length; } public String toString( ) { return "Rectangle: " + length + " -by- " + width + " with color " + color; } private double length; private double width; private String color; } class Ellipse extends Shape implements StrechableShape { public Ellipse( double len, double wid ) { length = len; width = wid; } // Makes one dimension larger public void stretch( ) { stretch( 2.0 ); } public void stretch( double factor ) { length *= factor; } public double getArea( ) { return Circle.PI / 4 * length * width; } public double getLength( ) { return length; } public String toString( ) { return "Ellipse: " + length + " -by- " + width; } private double length; private double width; } 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 getColor( ) { return color; } public void setColor( String c ) { color = c; } public String toString( ) { return "Square with side " + side + " with color " + color; } public double getArea( ) { return side * side; } private double side; private String color; } /* class Square extends Rectangle { public Square( double s ) { super( s, s ); } public String toString( ) { return "Square with side " + getLength( ); } } * */ class Day14 { public static void print( Object [ ] arr ) { for( int i = 0; i < arr.length; i++ ) System.out.println( "arr[" + i + "]=" + arr[ i ] ); } public static double getTotalArea( Shape [ ] arr ) { double total = 0.0; for( Shape x : arr ) total += x.getArea( ); return total; } public static void stretch( StrechableShape [ ] arr, double factor ) { for( StrechableShape s : arr ) s.stretch( factor ); } public static void colorAll( Colorable [ ] arr, String newColor ) { for( Colorable c : arr ) c.setColor( newColor ); } // Some are stretchable, some not // stretch the ones you can public static void stretch( Shape [ ] arr ) { for( Shape s : arr ) { if( s instanceof StrechableShape ) { StrechableShape ss = ( StrechableShape ) s; ss.stretch( ); } } } public static void main( String [ ] args ) { Shape [ ] arr3 = { new Circle( 2.0 ), new Circle( 1.0 ), new Square( 2.0 ), new Square( 1.0 ), new Ellipse( 1.0, 5.0 ), new Square( 4.0 ), new Circle( 4.0 ), new Ellipse( 1.0, 1.0 ), new Rectangle( 10.0, 12.0 ) }; print( arr3 ); stretch( arr3 ); System.out.println( "AFTER stretch: "); print( arr3 ); System.out.println( "Total area is: " + getTotalArea( arr3 ) ); Rectangle [ ] arr4 = { new Rectangle( 3, 4 ), new Rectangle( 4, 12 ) }; System.out.println( "BEFORE stretch: "); print( arr4 ); stretch( arr4, 2.0 ); colorAll( arr4, "purple" ); System.out.println( "AFTER stretch and color: "); print( arr4 ); } }