Rectangle class assignment

From WLCS
Revision as of 10:23, 3 June 2011 by Admin (talk | contribs) (Created page with "=== Rectangle class === Create a Rectangle class using the following specifications (HINT: Use your Circle class as a template): '''BE SURE TO COMMENT YOUR CODE''' '''Attribut...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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("myCar.getColor(): " + myCar.getColor());
    System.out.println("myCar.getYear(): " + myCar.getYear());
    System.out.println("myCar.getMake(): " + myCar.getMake());
    System.out.println("myCar.getModel(): " + myCar.getModel());
    System.out.println("myCar.getSpeed(): " + myCar.getSpeed());
    System.out.println("myCar.getGear(): " + myCar.getGear());

    //testing all setters (mutators)
    myCar.setColor("green");
    myCar.setYear(2001);
    myCar.setMake("Isuzu");
    myCar.setModel("Rodeo");
    myCar.setSpeed(55);
    myCar.setGear("D");

    System.out.println("myCar.getColor(): " + myCar.getColor());
    System.out.println("myCar.getYear(): " + myCar.getYear());
    System.out.println("myCar.getMake(): " + myCar.getMake());
    System.out.println("myCar.getModel(): " + myCar.getModel());
    System.out.println("myCar.getSpeed(): " + myCar.getSpeed());
    System.out.println("myCar.getGear(): " + myCar.getGear());

    //test drive()
    myCar.drive(88);

    //test toString()
    System.out.println(myCar);

    //test park()
    myCar.park();
    System.out.println(myCar);
  }
}