Develop the Shape application such that Rectangle Ellipse an
Develop the ‘Shape’ application such that:
‘Rectangle’, ‘Ellipse’, and ‘Triangle’ classes inherit from the ‘Shape’ class.
Develop the ‘Square’ and ‘Circle’ class where ‘Square’ inherits from ‘Rectangle’ and ‘Circle’ inherits from ‘Ellipse’. ‘Triangle’ has no derived class.
For each class, implement the overridden methods ‘draw’, ‘move’, and ‘erase’. Each method should only have an output statement such as “Rectangle – draw method” that will be displayed when the method is invoked.
Implement the default constructors for each class with a corresponding message to be displayed when invoked. No initializations are required; that is, the output message will be the only executable statement in the constructors.
Do not implement any other methods for these classes ( i.e., ‘toString’, ‘equals’, getters and setters ). Implement a ‘ShapeTest’ class which will instantiate an object of each class.
Exercise each of the ‘draw’, ‘move’, and ‘erase’ methods of each class
Remember to make sure that there is only a single class per file.
Solution
import java.util.*;
import java.lang.*;
import java.io.*;
class Shape {
public Shape(){
System.out.println(\"Shape class - constructor\");
}
public void draw() {
System.out.println(\"Shape - draw method\");
}
public void move() {
System.out.println(\"Shape - move method\");
}
public void erase() {
System.out.println(\"Shape - erase method\");
}
}
class Rectangle extends Shape{
public Rectangle(){
System.out.println(\"Rectangle class - constructor\");
}
public void draw() {
System.out.println(\"Rectangle - draw method\");
}
public void move() {
System.out.println(\"Rectangle - move method\");
}
public void erase() {
System.out.println(\"Rectangle - erase method\");
}
}
class Ellipse extends Shape{
public Ellipse(){
System.out.println(\"Ellipse class - constructor\");
}
public void draw() {
System.out.println(\"Ellipse - draw method\");
}
public void move() {
System.out.println(\"Ellipse - move method\");
}
public void erase() {
System.out.println(\"Ellipse - erase method\");
}
}
class Triangle extends Shape{
public Triangle(){
System.out.println(\"Triangle class - constructor\");
}
public void draw() {
System.out.println(\"Triangle - draw method\");
}
public void move() {
System.out.println(\"Triangle - move method\");
}
public void erase() {
System.out.println(\"Triangle - erase method\");
}
}
class Square extends Rectangle{
public Square(){
System.out.println(\"Square class - constructor\");
}
public void draw() {
System.out.println(\"Square - draw method\");
}
public void move() {
System.out.println(\"Square - move method\");
}
public void erase() {
System.out.println(\"Square - erase method\");
}
}
class Circle extends Ellipse{
public Circle(){
System.out.println(\"Circle class - constructor\");
}
public void draw() {
System.out.println(\"Circle - draw method\");
}
public void move() {
System.out.println(\"Circle - move method\");
}
public void erase() {
System.out.println(\"Circle - erase method\");
}
}
class Shapes {
public static void main(String args[]) {
Shape s = new Shape();
Rectangle r = new Rectangle();
Ellipse e = new Ellipse();
Triangle t = new Triangle();
Square sq = new Square();
Circle c = new Circle();
s.draw();
s.move();
s.erase();
r.draw();
r.move();
r.erase();
e.draw();
e.move();
e.erase();
t.draw();
t.move();
t.erase();
sq.draw();
sq.move();
sq.erase();
c.draw();
c.move();
c.erase();
}
}


