PHW6: Rectangle
In class we saw how to use inheritance to reuse code between our Ellipse and Circle classes. Now it’s your turn.
Rx: <10 min
Av: 10-20 min
Sd: 20-30 min
DNF: 30+ min
Instructions
- If you haven’t been following along in class, download the Shapes Project and import it into your workspace.
- Use inheritance to add a Rectangle class to your Shapes project. First, think about how Square and Rectangle are related. Should Rectangle extend Square, or vice versa? We went over both ways in class.
- Determine what fields are needed in the Square & Rectangle classes. Overlapping fields should only be in one class.
- Your Rectangle & Square classes should have at least 3 constructors each:
- Default constructor (no parameters)
- Constructor with parameters for each field except
outlineColor
- Constructor with parameters for each field
- Add get & set methods for all the fields. There should not be any duplicated get/set methods.
- Make sure the
getArea()
and getPerimeter()
methods work for both classes, and test in main by creating at least one Rectangle and one Square object.
- Make sure the
draw()
method works for both classes and test it by adding a Rectangle and Square to your Picture class.
- Run the program to verify your changes.