import java.math.BigInteger; import java.util.Date; abstract class Shape { public abstract double getArea( ); public abstract double getPerimeter( ); public double getSemiPerimeter( ) { return getPerimeter( ) / 2; } } interface Stretchable { void stretch( double factor ); double getSemiPerimeter( ); } interface CorneredShape { int countCorners( ); } abstract class ShapeWithCorners extends Shape implements CorneredShape { } abstract class Quadrilateral extends ShapeWithCorners { public int countCorners( ) { return 4; } } class Rectangle extends Quadrilateral implements Stretchable { public Rectangle( double len, double wid ) { length = len; width = wid; } // makes length = length * factor public void stretch( double factor ) { length *= factor; } public double getArea( ) { return length * width; } public double getPerimeter( ) { return 2 * ( length + width ); } public double getLength( ) { return length; } public double getWidth( ) { return width; } public String toString( ) { return "Rectangle " + length + "*" + width; } private double length; private double width; } class Ellipse extends Shape implements Stretchable { public Ellipse( double len, double wid ) { length = len; width = wid; } // makes length = length * factor public void stretch( double factor ) { length *= factor; } public double getArea( ) { return Circle.PI / 4 * length * width; } public double getPerimeter( ) { return Circle.PI / 2 * ( length + width ); } public double getLength( ) { return length; } public double getWidth( ) { return width; } public String toString( ) { return "Ellipse " + length + "*" + width; } private double length; private double width; } class Square extends Quadrilateral implements Comparable { public Square( double s ) { side = s; } public int compareTo( Square other ) { if( side == other.side ) return 0; else if( side < other.side ) return -1; else return +1; } public double getArea( ) { return side * side; } public double getPerimeter( ) { return 4 * side; } public String toString( ) { return "Square with side " + side; } private double side; } class Circle extends Shape implements Comparable { public static final double PI = 3.14; public Circle( double r ) { radius = r; } public int compareTo( Circle other ) { if( radius < other.radius ) return -1; else if( radius == other.radius ) return 0; else return +1; } public double getPerimeter( ) { return 2 * PI * radius; } public double getArea( ) { return PI * radius * radius; } public String toString( ) { return "Circle with radius " + radius; } private double radius; } public class Day14 { public static void printArray( String msg, Shape [ ] arr ) { System.out.print( msg + " [" ); for( Shape s : arr ) System.out.print( " " + s ); System.out.println( " ]" ); } public static double totalArea( Shape [ ] arr ) { double total = 0.0; for( Shape s : arr ) total += s.getArea( ); return total; } public static void stretchAll( Stretchable [ ] arr, double factor ) { for( Stretchable r : arr ) r.stretch( factor ); } // arr.length >= 1 public static Comparable findMax( Comparable [ ] arr ) { int maxIndex = 0; for( int i = 1; i < arr.length; i++ ) if( arr[ i ].compareTo( arr[ maxIndex ] ) > 0 ) maxIndex = i; return arr[ maxIndex ]; } public static void main( String [ ] args ) { Square [ ] sarr = { new Square( 3 ), new Square( 2 ) } ; printArray( "sarr: ", sarr ); System.out.println( "Total area of sarr is: " + totalArea( sarr ) ); Circle [ ] carr = { new Circle( 2 ), new Circle( 1 ) }; printArray( "carr: ", carr ); System.out.println( "Total area of carr is: " + totalArea( carr ) ); Shape [ ] sharr = { new Square( 3 ), new Ellipse( 4, 4 ), new Circle( 2 ), new Circle( 1 ), new Square( 2 ), new Rectangle( 5, 5 ) }; printArray( "sharr: ", sharr ); System.out.println( "Total area of sharr is: " + totalArea( sharr ) ); Rectangle [ ] rarr = { new Rectangle( 6, 4 ), new Rectangle( 7, 1 ) }; Ellipse [ ] earr = { new Ellipse( 5, 3 ), new Ellipse( 2, 1 ) } ; printArray( "rarr: ", rarr ); printArray( "earr: ", earr ); stretchAll( rarr, 2.0 ); stretchAll( earr, 2.0 ); printArray( "rarr: ", rarr ); printArray( "earr: ", earr ); BigInteger [ ] barr = { new BigInteger( "345345345"), new BigInteger( "65456356345634534534" ), new BigInteger( "3453453453453453453" ) }; Date [ ] darr = { new Date( "Jan 1, 2007" ), new Date( "Feb 4, 2009" ), new Date( "Dec 31, 2008" ) }; String [ ] starr = { "abc", "def", "bee" }; System.out.println( "Max BigInteger: " + findMax( barr ) ); System.out.println( "Max Date: " + findMax( darr ) ); System.out.println( "Max String: " + findMax( starr ) ); System.out.println( "Max Circle: " + findMax( carr ) ); } }