OOP 9 - Inheritance
Note to Couprie - New for next year 2018/19
This assignment needs a few additions
- add InstanceOf
- Compare results of a rectangle to the same size of pyramid
- Add units as a instance variable?
Imagine all the rectangles from your Geometry assignment represent buildings. You will have 3 types of buildings:
- A basic building (for commercial properties) - For this assignment, we assume that the Rectangle class is the same as this basic building
- A pyramid
- A house (for residential properties)
Read this tutorial on Inheritance: https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
- When you read the tutorial, notice that the the parent class has 3 instance variables and the child class requires a 4th.
- A common exam question is to get you to create the constructor method for a child class. Closely look at how that works.
Subclass 1: Pyramid (60%)
Add a Pyramid class to your Geometry project that extends (is subclass of) Rectangle. Pyramids will use the same methods and instance variables as Rectangle with one exception: The formula for the volume of a pyramid is ⅓ * width*length*height.
Important note: You must change the private variables in your Rectangle class to either public or protected. If you do not, you can not use those variables in the subclass.
- Create the new Pyramid subclass so that extends Rectangle
- Add the appropriate constructor. (You will likely need to look at the Inheritance Coding link above closely. The Pyramid’s constructor will use the keyword SUPER.)
- Update the printArea method to take into account the new shape
- Write the printVolume method in the Pyramid class to use the new formula.
- Override the Drill method to have only a printout saying ‘this is not a drillable shape’
Main Class
Create a new class called OOP8_InheritanceMain. Copy as much code as you need from the original main class, then:
- Delete the old rectangles if you copied them over.
- Add a new rectangle called Cube with the dimensions 2 x 2 x 2
- Add a new pyramid called Pyra-Cube with the dimensions of 2 x 2 x 2
- add 2 new Pyramids (pyramidOfGiza and edmontonCityHall (look up the actual dimensiosn of these pyramids. If the actual dimensions are not available make reasonable estimates)
- test all the area and volume methods for both.
- Do not use the drill method for this assignment.
Subclass 2: House (40%)
Add a House class to your Geometry project that extends (is subclass of) Rectangle. Houses will use the same methods and instance variables as Rectangle with a few exceptions:
- they add yard width and yard length variables. You can assume all yards are rectangles and include the building rectangle within the yard measurements.
- the area method now prints 2 different measurements:
- The house area is ___. (using the building dimensions - it just prints the area of its base)
- The yard area is ___. (using the yard dimensions)
Back in Main
Create 2 new houses and test that the area methods print both the area of the house and the yard You do not need to use the volume method for your houses.
Something to think about
How could we use these classes to build a program that would layout a new subdivision of a city? What other classes of objects would we need?