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
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:
Turn-In: Shape.java, including commented-out listing of resultant output.