This assignment will be an exercise in overloading constructors and overriding methods. It will also cover the basics of using objects. You will be writing an application.
For this assignment, you will be required to write a base class with constructors and methods. The base class will be inherited to create additional classes. The child classes will also have several overload constructors and its methods will override those of the parent class.
For this assignment: Make a base class: Shapes. Shape should have 1 variable (type double): area; Shape should have 2 constructors: If no parameters are passed area should be -1.0; If a parameter is passed, area should be the passed value. Shape should have 2 methods: computeArea and printSelf; computeArea should return area. printSelf should print area.
You next should, by inheritence from Shape, create Rectangle and Circle. Of course, Rectangle will inherit area. Rectangle must have 4 additional datum - integer x1 y1 x2 y2. Rectangle must by default have area -2.0 x1 = 0, x2 = 0, y1 =0, y2 =0. Circle will also inherit area; Circle must have 3 additional datum - integer x1 y1 double radius. Circle must by default have area -3.0 x1 = 0, y1 = 0, radius = 0.0 Have another constructor for each Rectangle and Circle that overloads this constructor behavior as shape did. (See the harness code to see what is required) You should have override methods computeArea and printSelf, in Rectangle and Circle, that override the compute and print behavior of the base class. printSelf should print all instance variable values for Rectangle and Circle.
Implement Shape, Circle, Rectangle then write a harness routine that tests as in the following:
Shapes s = new Shapes(); s.printSelf(); s = new Shapes(13); s.printSelf(); Rectangle r = new Rectangle(); r.printSelf(); r = new Rectangle(0,0,2,4); r.computeArea(); r.printSelf(); Circle c = new Circle(); c.printSelf(); c = new Circle(20,20,1.0); c.computeArea(); c.printSelf(); // polymorphic print Shapes o = new Shapes; o=s; o=r; o=c; System.out.println("\n ***Polymorphic Print***\n"); for(int i = 0; i < 3; i++) o[i].printSelf();
In Shapes area = -1.0 In Shapes area = 13.0 In Rectangle area = -2.0 In Rectangle x1, y1 = 0 0 In Rectangle x2, y2 = 0 0 In Rectangle area = 8.0 In Rectangle x1, y1 = 0 0 In Rectangle x2, y2 = 2 4 In Circle area = -3.0 In Circle x1, y1 = 0 0 In Circle radius = 0.0 In Circle area = 3.14159 In Circle x1, y1 = 20 20 In Circle radius = 1.0 ***Polymorphic Print*** In Shapes area = 13.0 In Rectangle area = 8.0 In Rectangle x1, y1 = 0 0 In Rectangle x2, y2 = 2 4 In Circle area = 3.14159 In Circle x1, y1 = 20 20 In Circle radius = 1.0