Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Inheritance: Problem: Develop the ‘Shape’ application such that: • ‘Rectangle’,

ID: 3765378 • Letter: I

Question

Inheritance:

Problem: 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

This is what I have so far. I am getting an error in ShapeTest.

public class Shape1 {

public static void main(String[] args) {

class Shape {

public Shape() {
System.out.println("Shape - default 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 Shape1 {
  
public Rectangle() {
System.out.println("Rectangle - default 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 Shape1 {
  
public Ellipse() {
System.out.println("Ellipse - default 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 Shape1 {

public Triangle() {
System.out.println("Triangle - default 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 - default constructor");
}
@Override
public void draw(){
System.out.println("Square draw method");
}
@Override
public void move(){
System.out.println("Square move method");
}
@Override
public void erase(){
System.out.println("Square erase method");
}
}

class Circle extends Ellipse
{
public Circle() {
System.out.println("Circle - default constructor");
}
@Override
public void draw(){
System.out.println("Circle draw method");
}
@Override
public void move(){
System.out.println("Circle move method");
}
@Override
public void erase(){
System.out.println("Circle erase method");
}
}

public class ShapeTest {

public static void main(String[] args) {


Shape1 s1 = new Shape1();
s1.draw();
s1.erase();
s1.move();
  
Rectangle r1 = new Rectangle();
r1.draw();
r1.erase();
r1.move();

Ellipse e1 = new Ellipse();
e1.draw();
e1.erase();
e1.move();

Triangle t1 = new Triangle();
t1.draw();
t1.erase();
t1.move();

Square sq1 = new Square();
sq1.draw();
sq1.erase();
sq1.move();

Circle c1 = new Circle();
c1.draw();
c1.erase();
c1.move();
  
Shape1[] shapeArr = {new Shape1(), new Rectangle(), new Ellipse(),
new Triangle (), new Square(), new Circle()};

for (Shape1 shapeArr1 : shapeArr) {
System.out.println("**************************************");
shapeArr1.draw ();
shapeArr1.erase ();
shapeArr1.move ();
System.out.println("**************************************");
}
}
}

Explanation / Answer

Modified Complete Program:

// File: ShapeTest.java
class Shape
{
public Shape()
{
  System.out.println("Shape - default 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 - default 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 - default 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 - default 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 - default constructor");
}

@Override
public void draw()
{
  System.out.println("Square draw method");
}

@Override
public void move()
{
  System.out.println("Square move method");
}

@Override
public void erase()
{
  System.out.println("Square erase method");
}
}

class Circle extends Ellipse
{
public Circle()
{
  System.out.println("Circle - default constructor");
}

@Override
public void draw()
{
  System.out.println("Circle draw method");
}

@Override
public void move()
{
  System.out.println("Circle move method");
}

@Override
public void erase()
{
  System.out.println("Circle erase method");
}
}

public class ShapeTest
{
public static void main(String[] args)
{
  Shape s1 = new Shape();
  s1.draw();
  s1.erase();
  s1.move();
  Rectangle r1 = new Rectangle();
  r1.draw();
  r1.erase();
  r1.move();
  Ellipse e1 = new Ellipse();
  e1.draw();
  e1.erase();
  e1.move();
  Triangle t1 = new Triangle();
  t1.draw();
  t1.erase();
  t1.move();
  Square sq1 = new Square();
  sq1.draw();
  sq1.erase();
  sq1.move();
  Circle c1 = new Circle();
  c1.draw();
  c1.erase();
  c1.move();
  Shape[] shapeArr = {new Shape(), new Rectangle(), new Ellipse(),
    new Triangle(), new Square(), new Circle()};
  for(Shape shapeArr1 : shapeArr)
  {
   System.out.println("**************************************");
   shapeArr1.draw();
   shapeArr1.erase();
   shapeArr1.move();
   System.out.println("**************************************");
  }
  
  System.out.println(" Array of objects - s1, r1, e1, t1, sq1, c1:");
  Shape[] shapeArr101 = {s1, r1, e1, t1, sq1, c1};
  for(Shape shapeArr2 : shapeArr101)
  {
   System.out.println("**************************************");
   shapeArr2.draw();
   shapeArr2.erase();
   shapeArr2.move();
   System.out.println("**************************************");
  }
}
}


Sample Output:

Shape - default constructor
Shape draw method
Shape erase method
Shape move method
Shape - default constructor
Rectangle - default constructor
Rectangle draw method
Rectangle erase method
Rectangle move method
Shape - default constructor
Ellipse - default constructor
Ellipse draw method
Ellipse erase method
Ellipse move method
Shape - default constructor
Triangle - default constructor
Triangle draw method
Triangle erase method
Triangle move method
Shape - default constructor
Rectangle - default constructor
Square - default constructor
Square draw method
Square erase method
Square move method
Shape - default constructor
Ellipse - default constructor
Circle - default constructor
Circle draw method
Circle erase method
Circle move method
Shape - default constructor
Shape - default constructor
Rectangle - default constructor
Shape - default constructor
Ellipse - default constructor
Shape - default constructor
Triangle - default constructor
Shape - default constructor
Rectangle - default constructor
Square - default constructor
Shape - default constructor
Ellipse - default constructor
Circle - default constructor
**************************************
Shape draw method
Shape erase method
Shape move method
**************************************
**************************************
Rectangle draw method
Rectangle erase method
Rectangle move method
**************************************
**************************************
Ellipse draw method
Ellipse erase method
Ellipse move method
**************************************
**************************************
Triangle draw method
Triangle erase method
Triangle move method
**************************************
**************************************
Square draw method
Square erase method
Square move method
**************************************
**************************************
Circle draw method
Circle erase method
Circle move method
**************************************

Array of objects - s1, r1, e1, t1, sq1, c1:
**************************************
Shape draw method
Shape erase method
Shape move method
**************************************
**************************************
Rectangle draw method
Rectangle erase method
Rectangle move method
**************************************
**************************************
Ellipse draw method
Ellipse erase method
Ellipse move method
**************************************
**************************************
Triangle draw method
Triangle erase method
Triangle move method
**************************************
**************************************
Square draw method
Square erase method
Square move method
**************************************
**************************************
Circle draw method
Circle erase method
Circle move method
**************************************