CS 3340 OOP
Exercise 3: Geometric Shapes: Inheritance
Chapter Readings: TIJ
[7]
[8]
Goal: To learn the power of inheritance and the significance of specialized constructors.
Assignment
Consider this table of various geometric shapes:
Shape | Area using dimensions d1, d2, d3
|
Trapezoid | d1*(d2+d3)/2 [where d1 = height, d2 = base1, d3 = base2]
|
Parallelogram | same as above but d2=d3
|
Rectangle | same as above
|
Square | same as above but d1=d2=d3
|
RegularPolygon | (d1*d2*d3)/2 [where d1 = #sides, d2 = length of side, d3 = radius]
|
Pentagon | d1 = 5
|
Hexagon | d1 = 6
|
Heptagon | d1 = 7
|
Implement various classes representing the above shapes in a file called Shape.java.
Only the Shape class needs to be public, and be sure to put a main body in Shape to test your
code. Construct exactly ONE instance of each (non-abstract) class.
All dimensions should be given the value of 2. Printout their name and area.
This means we should all get the same output.
Pay particular attention to
- abstract method, hence abstract class? (An abstract class forces subclasses.
An abstract method forces an implementation somewhere.)
- inheritance (which classes extend another class? That is, subclass "is a" superclass.) The above table suggests the relationships.
- constuctor signature (what are the necessary parameters?)
- use of parent constructor (what are the parameters to the super call?)
- only implement area computation when absolutely necessary (the table shows this too)
- Shape's d1, d2, d3 should be protected, so subclasses can access easily (but still be
careful about the parameters to the super constructor call)
- Assume, in our case, that RegularPolygon is always intended to be subclassed.
With respect to this last issue, here is how to call a parent's constructor:
class Y { // base class, or superclass
protected int i; // storage for i, usually private, but protected in this assignment
public Y(int i) { // constructor accepts "i" value
this.i = i; // save "i"
}
}
class X extends Y { // derived class, or subclass
public X(int i) { // constructor
super(i); // call to parent's constructor, "i" is passed to X's constructor
}
}
Hints:
- Consider X and Y. If X "is a" Y, then X extends Y. Which of these are true?
- A Heptagon is a Hexagon?
- A Square is a Rectangle?
- A Rectangle is a Square?
- If someone wanted to make an X, how much information would they have to give (i.e. the constructor)?
- X should call Y's constructor (super) with correct parameters, hence, the dimensions all float up
to Shape.
- If X can rely on Y's getArea(), then X does not need a getArea() of its own.
- If X is not abstract, then X should be constructed by the testing main().
- If Y needs dimensions in a computation, these are available because they are protected in Shape.
Subclasses can "see" protected variables. This is not usually a good design principle but we are
just doing a record structure to hold the dimensions.
Turn-In: Shape.java, including commented-out listing of resultant output.