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(" | + | System.out.println("myRect.getLength(): " + myRect.getLength()); |
− | System.out.println(" | + | System.out.println("myRect.getWidth(): " + myRect.getWidth()); |
− | |||
− | |||
− | |||
− | |||
//testing all setters (mutators) | //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 | |
− | |||
− | |||
− | System.out.println(" | ||
− | |||
− | System.out.println(" | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | //test | + | //test getArea() |
− | + | double area = myRect.getArea(); | |
− | System.out.println( | + | 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
}
}