package designingclasses; /** * Class to represent rectangles. * @author maye */ public class Rectangle { // instance variables private double length; private double width; /** * No-arg constructor */ public Rectangle() { length = 1.0; width = 1.0; } /** * Constructs a Rectangle object and initializes the fields to the passed values. * @param len Initial length for the rectangle object. * @param w Initial width for the rectangle object. */ public Rectangle(double len, double w) { length = len; width = w; } /** * This is a copy constructor. It initializes the fields of the object being created to the * same values as the fields of the object passed as an argument. * @param rectangleObj The object being copied. */ public Rectangle(Rectangle rectangleObj) { if( rectangleObj != null ) { length = rectangleObj.length; width = rectangleObj.width; } } /** * The getLength method returns a Rectangle's length * @return The value in the length field. */ public double getLength() { return length; } /** * The getWidth method returns a Rectangle's width * @return The value in the width field. */ public double getWidth() { return width; } /** * The setLength method stores a new value in the length field. * @param newLength The new value to store in the length. */ public void setLength(double newLength) { length = newLength; } /** * The setWidth method stores a new value in the width field. * @param newWidth The new value to store in the width. */ public void setWidth(double newWidth) { width = newWidth; } /** * The getArea method returns a Rectangle object's area. * @return The product of length times width. */ public double getArea() { return length * width; } public String toString() { return "Length: " + length + "\nWidth: " + width; } /** * The equals method compares the Rectangle object calling this method with the Rectangle object * passed as an argument. * @param obj The Rectangle object to compare with. * @return True if both objects have the same value for the length and width instance variables. * False otherwise. */ @Override public boolean equals(Object obj) { // check that the type of the parameter is Rectangle if( !(obj instanceof Rectangle) ) return false; // we already know that obj is of type Rectangle, so it's safe to cast Rectangle rectangleObject = (Rectangle) obj; // return true or false depending on whether length and width have the same value return length == rectangleObject.length && width == rectangleObject.width; } }