/** * This version of Rectangle accompanies Chapter 2. Method * setLength() now throws an IllegalArgumentException * if its argument is invalid. */ public class Rectangle extends Shape { private double length; private double height; /** * Construct a Rectangle object using the default size * for its dimensions. */ public Rectangle() { super( "Rectangle" ); setLength( super.DEFAULT_SIZE ); setHeight( Shape.DEFAULT_SIZE ); } /** * Construct a Rectangle object using the arguments. * If an argument is <= 0, the default size specified in * Shape is used instead. * @param theLength the length of this Rectangle; * must be > 0 * @param theHeight the height of this Rectangle; * must be > 0 */ public Rectangle( double theLength, double theHeight ) { super( "Rectangle" ); setLength( theLength ); setHeight( theHeight ); } /** * Get the surface area of this Rectangle. * @return the surface area of this Rectangle */ public double getSurfaceArea() { return this.length * this.height; } /** * Get the perimeter of this Rectangle. * @return the perimeter of this Rectangle */ public double getPerimeter() { return this.length + 2 * this.height; } /** * Get the length dimension of this Rectangle. * @return the length of this Rectangle */ public double getLength() { return this.length; } /** * Get the height dimension of this Rectangle. * @return the height of this Rectangle */ public double getHeight() { return this.height; } /** * Set the length dimension of this Rectangle. * If theLength is <= 0, the dimension is unchanged. * @param theLength new length of this Rectangle; * must be > 0 */ public void setLength( double theLength ) { if ( theLength <= 0 ) { throw new IllegalArgumentException( "Invalid Rectangle length (" + theLength + "): must be > 0 " ); } this.length = theLength; } /** * Set the height dimension of this Rectangle. * If theHeight is <= 0, the dimension is unchanged. * @param theHeight the new height of this Rectangle; * must be > 0 */ public void setHeight( double theHeight ) { if ( theHeight <= 0 ) { return; } this.height = theHeight; } /** * Returns a String object representing this * Rectangle's value. Overridden from Object. * @return a string representation of this object */ public String toString() { return this.getShapeName() + ": length = " + this.length + ", height = " + this.height; } /** * Returns true if and only if the argument is not * null and is a Rectangle object that * represents the same rectangle value as this object. The * comparison is done based on the dimension's length and * height. * @param o the object to compare with * @return true if the Rectangle objects * represent the same value; false otherwise. */ public boolean equals( Object o ) { if ( ( o == null ) || ( ! ( o instanceof Rectangle ) ) ) { return false; } return ( ( ( ( Rectangle ) o ).getLength() == getLength() ) && ( ( ( Rectangle ) o ).getHeight() == getHeight() ) ); } }