Description
Question 1 – Fun with Polymorphism
Create a hierarchy of shapes with the classes Shape, Circle, Rectangle and Square. (Hint: Square is a special Rectangle whose length=breadth)
Each class has its own printDescription() method which prints its dimensions and area.
Create a ShapeManager class which instantiates the various shapes and calls for the description of each shape using the method manageShape(Shape).
void manageShape(Shape myShape) {
myShape.printDescription();
}
The constructors of the other classes are as follows:
• Circle(double radius)
• Rectangle(double length, double breadth)
• Square(double side)
Declare the constructor of each class with a print statement.
Shape() {
System.out.println(“Default constructor of Shape”);
}
Shape(…) {
System.out.println(“Constructor of Shape with parameters xyz”);
}
Question 2 – Who Am I?
Construct the following inheritance hierarchy.
A typical way to introduce oneself is “Hello, my name is xxx and my hobby is yyy”.
(Hint: use a combination of the getName() method and the HOBBY String constant)
Arif is a CSE Student at IEM, who secretly moonlights as a hacker. Have Arif introduce himself
(1) at a get-together for student leaders of various colleges in Salt Lake
(2) at a closed-door Hacker Society meeting
(3) at his cousin’s birthday party where he meets a beautiful girl who is a Tagore fan.
(Hint: Instantiate Arif as a CSEStudent, then use a switch case for his different behaviour e.g. choice #2 implies Arif is at the Hacker Society Meeting.)
(Hint: Use the super keyword; you may also introduce additional methods)