use JAVA programing Develop the Shape application such that

use JAVA programing

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

//Abstract class Shape
abstract class Shape
{
   //Abstract methods
   abstract void draw();
   abstract void move();
   abstract void erase();
}
//Class Rectangle derived from Shape
class Rectangle extends Shape
{
   //Rectangle constructor
   Rectangle()
   {
       System.out.println(\"\ Rectangle Constructor\");  
   }
   //Override draw method
   void draw()
   {
       System.out.println(\"Rectangle - Draw method\");
   }
   //Override move method
   void move()
   {
       System.out.println(\"Rectangle - Move method\");
   }
   //Override erase method
   void erase()
   {
       System.out.println(\"Rectangle - Erase method\");
   }  
}
//Class Ellipse derived from Shape
class Ellipse extends Shape
{
   //Ellipse constructor
   Ellipse()
   {
       System.out.println(\"\ Ellipse Constructor\");  
   }
   //Override draw method  
   void draw()
   {
       System.out.println(\"Ellipse - Draw method\");
   }
   //Override move method
   void move()
   {
       System.out.println(\"Ellipse - Move method\");
   }
   //Override erase method
   void erase()
   {
       System.out.println(\"Ellipse - Erase method\");
   }  
}
//Class Triangle derived from Shape
class Triangle extends Shape
{
   //Triangle constructor
   Triangle()
   {
       System.out.println(\"\ Triangle Constructor\");  
   }
   //Override draw method
   void draw()
   {
       System.out.println(\"Triangle - Draw method\");
   }
   //Override move method
   void move()
   {
       System.out.println(\"Triangle - Move method\");
   }
   //Override erase method
   void erase()
   {
       System.out.println(\"Triangle - Erase method\");
   }  
}
//Class Square derived from Rectangle
class Square extends Rectangle
{
   //Square constructor
   Square()
   {
       System.out.println(\"\ Square Constructor\");  
   }
   //Override draw method
   void draw()
   {
       System.out.println(\"Square - Draw method\");
   }
   //Override move method
   void move()
   {
       System.out.println(\"Square - Move method\");
   }
   //Override erase method
   void erase()
   {
       System.out.println(\"Square - Erase method\");
   }  
}
//Class Circle derived from Ellipse
class Circle extends Ellipse
{
   //Circle constructor
   Circle()
   {
       System.out.println(\"\ Circle Constructor\");  
   }
   //Override draw method
   void draw()
   {
       System.out.println(\"Circle - Draw method\");
   }
   //Override move method
   void move()
   {
       System.out.println(\"Circle - Move method\");
   }
   //Override erase method
   void erase()
   {
       System.out.println(\"Circle - Erase method\");
   }  
}
//Driver class
class ShapeTest
{
   public static void main(String st[])
   {
       //Creates object for each class and calls the methods
       Rectangle r = new Rectangle();
       r.draw();
       r.move();
       r.erase();
       Ellipse e = new Ellipse();
       e.draw();
       e.move();
       e.erase();
       Triangle t = new Triangle();
       t.draw();
       t.move();
       t.erase();
       Square s = new Square();
       s.draw();
       s.move();
       s.erase();
       Circle c = new Circle();
       c.draw();
       c.move();
       c.erase();
   }
}

Output:

Rectangle Constructor
Rectangle - Draw method
Rectangle - Move method
Rectangle - Erase method

Ellipse Constructor
Ellipse - Draw method
Ellipse - Move method
Ellipse - Erase method

Triangle Constructor
Triangle - Draw method
Triangle - Move method
Triangle - Erase method

Rectangle Constructor

Square Constructor
Square - Draw method
Square - Move method
Square - Erase method

Ellipse Constructor

Circle Constructor
Circle - Draw method
Circle - Move method
Circle - Erase method

use JAVA programing Develop the ‘Shape’ application such that: ‘Rectangle’, ‘Ellipse’, and ‘Triangle’ classes inherit from the ‘Shape’ class. Develop the ‘Squar
use JAVA programing Develop the ‘Shape’ application such that: ‘Rectangle’, ‘Ellipse’, and ‘Triangle’ classes inherit from the ‘Shape’ class. Develop the ‘Squar
use JAVA programing Develop the ‘Shape’ application such that: ‘Rectangle’, ‘Ellipse’, and ‘Triangle’ classes inherit from the ‘Shape’ class. Develop the ‘Squar
use JAVA programing Develop the ‘Shape’ application such that: ‘Rectangle’, ‘Ellipse’, and ‘Triangle’ classes inherit from the ‘Shape’ class. Develop the ‘Squar

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site