package shapepkg; public abstract class Shape implements Comparable { private String myName; public Shape (String shapeName ) { myName = shapeName; } abstract public double getArea( ); // Provided by sub-class public String getName() { return myName; } public String toString() { return "Name = " + myName + ", Area = " + getArea( ); } public int compareTo (Object rhs) { if(getArea( ) < ((Shape)rhs).getArea()) return -1; else if (getArea() > ((Shape)rhs).getArea()) return 1; else return 0; } } ======================================================================== package shapepkg; public class Circle extends Shape { private double myRadius; public Circle(String name, double r) { super( name ); myRadius = r; } public double getArea() { return Math.PI * myRadius * myRadius; } public double getRadius() { return myRadius; } public String toString() { return super.toString() + ", Radius: " + myRadius; } } ======================================================================== package shapepkg; public class Rectangle extends Shape { private double myLength; private double myWidth; public Rectangle(String name, double len, double wid) { super( name ); myLength = len; myWidth = wid; } public double getArea() { return myLength * myWidth; } public double getLength() { return myLength; } public double getWidth() { return myWidth; } public String toString() { return super.toString() + ", Length = " + myLength + ", Width = " + myWidth; } } =============================================================== package shapepkg; public class Square extends Rectangle { public Square(String name, double side) { super(name, side, side); } public double getSide() { return getLength(); } public String toString() { return super.toString() + ", Side = " + getSide(); } } ============================================================== package shapepkg; // Sorts.java: object insertion sort public class Sorts { // insertionSort: Sorts array of objects using the insertion sort public static void insertionSort (Comparable[] objects) { for (int index = 1; index < objects.length; index++) { Comparable key = objects[index]; int position = index; // shift larger values to the right while (position > 0 && objects[position-1].compareTo(key) > 0) { objects[position] = objects[position-1]; position--; } objects[position] = key; } } } ================================================================ package shapepkg; public class ShapeTest { public static void main(String[] args) { Shape[] myShapes = new Shape[5]; myShapes[0] = new Circle("Circle", 3); myShapes[1] = new Rectangle("Rectangle", 3, 4); myShapes[2] = new Square("Square", 6); myShapes[3] = new Rectangle("Another Rectangle", 4, 5); myShapes[4] = new Circle("Circle", 5); System.out.println("Original array:"); for (int k = 0; k < myShapes.length; k++) System.out.println(myShapes[k]); Sorts.insertionSort(myShapes); System.out.println("Sorted array:"); for (int k = 0; k < myShapes.length; k++) System.out.println(myShapes[k]); } } ================================================================ Run: Original array: Name = Circle, Area = 28.274333882308138, Radius: 3.0 Name = Rectangle, Area = 12.0, Length = 3.0, Width = 4.0 Name = Square, Area = 36.0, Length = 6.0, Width = 6.0, Side = 6.0 Name = Another Rectangle, Area = 20.0, Length = 4.0, Width = 5.0 Name = Circle, Area = 78.53981633974483, Radius: 5.0 Sorted array: Name = Rectangle, Area = 12.0, Length = 3.0, Width = 4.0 Name = Another Rectangle, Area = 20.0, Length = 4.0, Width = 5.0 Name = Circle, Area = 28.274333882308138, Radius: 3.0 Name = Square, Area = 36.0, Length = 6.0, Width = 6.0, Side = 6.0 Name = Circle, Area = 78.53981633974483, Radius: 5.0 ================================================================ Other integer sorts: // bubbleSort: keep exchanging any two elements not in order public static void bubbleSort (int[] numbers) { int temp; for (int dummy = 0; dummy < numbers.length - 1; dummy++) { for (int index = 1; index < numbers.length; index++) { if (numbers[index-1] > numbers[index]) { // Swap the values temp = numbers[index-1]; numbers[index-1] = numbers[index]; numbers[index] = temp; } } } } // selectionSort: keep selecting out the minimum value public static void selectionSort (int[] numbers) { int min, temp; for (int index = 0; index < numbers.length-1; index++) { min = index; for (int scan = index+1; scan < numbers.length; scan++) if (numbers[scan] < numbers[min]) min = scan; // Swap the values temp = numbers[min]; numbers[min] = numbers[index]; numbers[index] = temp; } }