Rectangle class assignment

From WLCS

Rectangle class

Create a Rectangle class using the following specifications (HINT: Use your Circle class as a template):

BE SURE TO COMMENT YOUR CODE

Attributes (private):

  • double length (default: 0.0)
  • double width (default: 0.0)

Methods (public)

  • default constructor (sets all attributes to their defaults)
  • specific constructor (will have as many parameters as there are attributes)
    • set all the attributes to be the same as the input parameters
  • setters (mutators) for all attributes
  • getters (accessors) for all attributes
  • double getArea() (returns the area of the retangle)

RectangleTestMain:

Use the following code to test your Rectangle class:

public class RectangleTestMain
{
  public static void main(String [] args)
  {
    //use the default constructor to create a new instance of Car
    Rectangle myRect = new Rectangle();

    //testing all getters (accessors)
    System.out.println("myRect.getLength(): " + myRect.getLength());
    System.out.println("myRect.getWidth(): " + myRect.getWidth());

    //testing all setters (mutators)
    myRect.setLength(3.1);
    myRect.setWidth(2.0);
    
    System.out.println("myRect.getLength(): " + myRect.getLength());  //should see 3.1
    System.out.println("myRect.getWidth(): " + myRect.getWidth());    //should see 2.0

    //test getArea()
    double area = myRect.getArea();
    System.out.println("Area => " + area);  //should see 6.2
  }
}