import java.util.Arrays; public class Rectangle implements Comparable { public int width = 0; public int height = 0; public Point origin; // four constructors public Rectangle() { origin = new Point(0, 0); } public Rectangle(Point p) { origin = p; } public Rectangle(int w, int h) { origin = new Point(0, 0); width = w; height = h; } public Rectangle(Point p, int w, int h) { origin = p; width = w; height = h; } // a method for moving the rectangle public void move(int x, int y) { origin.setX(x); origin.setY(y); } // a method for computing // the area of the rectangle public int getArea() { return width * height; } // a method required to implement // the Comparable interface @Override public int compareTo(Comparable other) { Rectangle otherRect = (Rectangle)other; return this.getArea() - otherRect.getArea(); } public static void main(String args[]) { Rectangle [] shapes = new Rectangle[4]; shapes[0] = new Rectangle(4, 5); shapes[1] = new Rectangle(1, 8); shapes[2] = new Rectangle(2, 15); shapes[3] = new Rectangle(9, 2); System.out.println("Initial array:"); for (Rectangle rectangle : shapes) { System.out.println(rectangle + ", area: " + rectangle.getArea()); } Arrays.sort(shapes); System.out.println("Sorted array:"); for (Rectangle rectangle : shapes) { System.out.println(rectangle + ", area: " + rectangle.getArea()); } } }