Difference between revisions of "Rectangle class assignment"

From WLCS
(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...")
 
 
Line 3: Line 3:
  
 
'''BE SURE TO COMMENT YOUR CODE'''
 
'''BE SURE TO COMMENT YOUR CODE'''
 
  
 
'''Attributes (private):'''
 
'''Attributes (private):'''
Line 29: Line 28:
  
 
     //testing all getters (accessors)
 
     //testing all getters (accessors)
     System.out.println("myCar.getColor(): " + myCar.getColor());
+
     System.out.println("myRect.getLength(): " + myRect.getLength());
     System.out.println("myCar.getYear(): " + myCar.getYear());
+
     System.out.println("myRect.getWidth(): " + myRect.getWidth());
    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)
 
     //testing all setters (mutators)
     myCar.setColor("green");
+
     myRect.setLength(3.1);
    myCar.setYear(2001);
+
     myRect.setWidth(2.0);
     myCar.setMake("Isuzu");
+
      
    myCar.setModel("Rodeo");
+
     System.out.println("myRect.getLength(): " + myRect.getLength()); //should see 3.1
     myCar.setSpeed(55);
+
     System.out.println("myRect.getWidth(): " + myRect.getWidth());   //should see 2.0
    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()
+
     //test getArea()
     myCar.park();
+
     double area = myRect.getArea();
     System.out.println(myCar);
+
     System.out.println("Area => " + area); //should see 6.2
 
   }
 
   }
 
}
 
}
 
</source>
 
</source>

Latest revision as of 10:29, 3 June 2011

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
  }
}