/** * A class for representing a rectangle. This class contains no static attributes and no static methods. * * @author Franck van Breugel */ public class Rectangle { 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.width = width; this.height = height; } /** * Initializes this rectangle with zero width and height. */ public Rectangle() { this.width = 0; this.height = 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.width = rectangle.width; this.height = rectangle.height; } /** * 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.width * this.height; } /** * 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.width + " and height " + this.height; } /** * Scales this rectangle with the given factor. * * @param factor the scaling factor. * @pre. factor >= 0 */ public void scale(int factor) { this.width = this.width * factor; this.height = this.height * factor; } /** * 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.width == other.width && this.height == other.height; } 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.width == other.width) { difference = this.height - other.height; } else { difference = this.width - other.width; } return difference; } }