CIS 180: Object-Oriented Programming
Homework #3
Rectangles
Due September 28, 2009
Problem Statement
Consider a Java class to represent Rectangles.
- Each rectangle object should have a width and a height.
- The fields should be declared protected instead of private.
- There should be a constructor method with no parameters (a default constructor)
that sets the width and height to some reasonable default values.
- There should be another constructor method with parameters for the initial width and height.
- There should be mutator methods setWidth, and setHeight to change the width and height of the rectangle.
- There should be methods getPerimeter and getArea to calculate the perimeter and area of the rectangle.
These methods should return the appropriate values, not print them out.
- There should be a main method to test your code. The main method should:
- Create a rectangle object, specifying an initial width and height.
- Print out the rectangle's initial width, height, perimeter, and area. The perimeter and area
should be obtained by sending the appropriate messages to the rectangle.
- Send a message to change the rectangle's width.
- Print out the new width, perimeter, and area.
- Send a message to change the rectangle's height.
- Print out the new height, perimeter, and area.
Next, consider a class to represent Squares. As you know, a square is a special kind of rectangle where
the width and height are equal. There should be a constructor method for squares with one parameter for the side
length (since the width and height must be the same). If a square's width is changed, the height must also change.
Likewise, changing the the height requires a change in the width. Otherwise, squares behave exactly like other rectangles.
Design
- Draw a UML class diagram showing the Rectangle
and Square classes and the relationship between them. Include
all details (types, parameters, accessibility).
Implementation
-
Create an eclipse project called HW3 and add a Rectangle class with a main method to the project.
- Add a comment block at the beginning of the
Rectangle.java file with the name of the class, the author (your name), and a
description of the purpose of the class.
- Complete and test the Rectangle class before
you attempt to implement the Square class.
- Add a Square class to your eclipse project. Do not include a main method in the Square class.
- Add a comment block at the beginning of the
Square.java file with the name of the class, the author (your name), and a
description of the purpose of the class.
- Complete the implementation of the Square class, taking advantage of inheritance to minimize the amount of
new code you write.
- Add code to the main method (in the Rectangle class) to test your Square class the same way you have
tested the Rectangle class. Note that it doesn't matter which class the main method is in, but it isn't a
good idea to have more than one main method in the same project.
When your program is complete it should produce output similar to the following:
The rectangle with initial width 5 and height 10 has perimeter = 30 and area = 50.
After changing the width to 4 the rectangle has perimeter = 28 and area = 40.
After changing the height to 12 the rectangle has perimeter = 32 and area = 48.
The square with initial width and height 12 has perimeter = 48 and area = 144.
After changing the width to 10 the square has perimeter = 40 and area = 100.
After changing the height to 8 the square has perimeter = 32 and area = 64.
What to turn in
Submit your source code (Rectangle.java and Square.java)
files as well as your class diagram as email attachments to:
ttao@umassd.edu.
Alternatively, you may submit your class diagram in hardcopy.
Use the subject line CIS-180 HW#3 in your email.
There will be a 10% penalty for assignments received after the due date.
Assignments will not be accepted more than one week past the due date.