/** * 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; } }