package com.quadrangle; /** * A class for representing a rectangle. * * @author Quadrangle and Co. */ 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; } /** * 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 String.format("Rectangle of width %d and height %d", this.width, 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; } /** * 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; } }