Discussion Section 11: Inheritance
Due: 10pm Tuesday April 10
Purpose: In this
discussion section, you will build class hierarchies using
inheritance. One of the principles this structure enforces is that
you will organizing your code to be reusable.
Notice that the class is abstract and the interface that it defines consists of the methods getPerimeter() and getArea(), which return the perimeter and the area of the Shape, respectively. Shape also has an instance variable colorName, which stores an extra attribute, its color name (as a String here). You must define the rest of the classes such that they create the hierarchy specified above.
The Circle will have an extra instance variable radius, which defines the radius of the circle. The Rectangle will contain two instance variables: a width and a height. The Square must use the instance variables that it inherits from Rectangle. Each of these classes should implement the above abstract methods, by making appropriate use of the instance variables in the class, and each class will provide the behavior that is appropriate for each different kind of shape. You will also define constructors that initialize all the instance variables from the appropriate parameters to the constructor. Finally, you will provide a toString() method that prints out all the appropriate information for each object of these classes (i.e., its discerning characteristics, its area, its perimeter and its color name).
You will write a TestShapes class that will just contain the main() method of the program. Inside it, you must create several objects of all of the above types and print out their values.
You should turn in the following java files: TestShapes.java, Shape.java, Circle.java, Rectangle.java, Square.java
Your program should be internally correct (sound logic) and
externally correct (following Java style
guideline).