CIS 180: Object-Oriented Programming
Homework #3

Rectangles

Due Monday, October 3, 2011

Problem Statement

Consider a Java class to represent Rectangles.

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

  1. Draw a UML class diagram showing the Rectangle and Square classes and the relationship between them. Include all details (types, parameters, accessibility).

Implementation

  1. Create an eclipse project called HW3 and add a Rectangle class with a main method to the project.
  2. 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.
  3. Complete and test the Rectangle class before you attempt to implement the Square class.
  4. Add a Square class to your eclipse project. Do not include a main method in the Square class.
  5. 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.
  6. Complete the implementation of the Square class, taking advantage of inheritance to minimize the amount of new code you write.
  7. 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 your lab instructor. Alternatively, you may submit your class diagram in hardcopy.

Use the subject line CIS-180 HW#3 in your email.

Make sure you send your homework to the proper place. Homework sent to the wrong address may not receive credit!

Lab SectionGraderEmail
L1 Mon. 2PM Wes Fowlks u_wfowlks@umassd.edu
L2 Wed. 9AM Neelima Mothe nmothe@umassd.edu
L3 Wed. 1PM Wes Fowlks u_wfowlks@umassd.edu

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.