/** * A class for representing a rectangle. This class contains * no static attributes and no static methods. * * @author Franck van Breugel */ public class Rectangle implements Comparable { private int width; private int height; /** * Initializes this rectangle with the given width and height. * * @param width the width of this rectangle. * @pre. width >= 0 * @param height the height of this rectangle. * @pre. height >= 0 */ public Rectangle(int width, int height) { this.setWidth(width); this.setHeight(height); } /** * Initializes this rectangle with zero width and height. */ public Rectangle() { this(0, 0); } /** * Initializes this rectangle with the same width and * height as the given rectangle. * * @param rectangle the rectangle to be copied. */ public Rectangle(Rectangle rectangle) { this(rectangle.getWidth(), rectangle.getHeight()); } /** * Returns the width of this rectangle. * * @return the width the width of this rectangle. */ public int getWidth() { return this.width; } /** * Sets the width of this rectangle to the given width. * * @param width the width of this rectangle. * @pre. width >= 0 */ public void setWidth(int width) { this.width = width; } /** * Returns the height of this rectangle. * * @return the height of this rectangle. */ public int getHeight() { return this.height; } /** * Sets the height of this rectangle to the given height. * * @param height the height of this rectangle. * @pre. height >= 0 */ public void setHeight(int height) { this.height = height; } /** * Returns the area of this rectangle. * * @return the area of this rectangle. */ public int getArea() { return this.getWidth() * this.getHeight(); } /** * Returns a string representation of this rectangle. * For example, the string representation of * new Rectangle(1, 2) is * "Rectangle of width 1 and height 2". * * @return a string representation of this rectangle. */ public String toString() { return "Rectangle of width " + this.getWidth() + " and height " + this.getHeight(); } /** * Scales this rectangle with the given factor. * * @param factor the scaling factor. * @pre. factor >= 0 */ public void scale(int factor) { this.setWidth(this.getWidth() * factor); this.setHeight(this.getHeight() * factor); } /** * Returns the hash code of this rectangle. * * @return the hash code of this rectangle. */ public int hashCode() { return this.getWidth() * this.getHeight(); } /** * Tests if this rectangle is the same as the given object. * Two rectangles are considered the same if they have the * same width and height. * * @param object an object. * @return true if this rectangle is the same as the given * object, false otherwise. */ public boolean equals(Object object) { boolean equal; if (object != null && this.getClass() == object.getClass()) { Rectangle other = (Rectangle) object; equal = this.getWidth() == other.getWidth() && this.getHeight() == other.getHeight(); } else { equal = false; } return equal; } /** * Compares two rectangles. This rectangle is smaller than another * rectangle if its width is smaller than the width of the other * rectangle, or the width of the rectangles is the same and the * height of this rectangle is smaller than the height of the other * rectangle. * * @param other another rectangle. * @return 0 if this rectangle is equal to the other rectangle; a * value less than 0 if this rectangle is smaller than the other * rectangle; a value greater than 0 otherwise. */ public int compareTo(Rectangle other) { int difference; if (this.getWidth() == other.getWidth()) { difference = this.getHeight() - other.getHeight(); } else { difference = this.getWidth() - other.getWidth(); } return difference; } }