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: 3767184 • 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
Modified Complete Program:

class 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("**************************************");
}
}
}

*** I have completed the above program, below is the next assignment extended from the previous program. ***

Polymorphism:

PROBLEM: Develop the Shape application such that:

• Implements an array of object of various types (all SIX classes) in any order.
• In some type of a looping structure, demonstrate polymorphism by calling all three of technology methods, draw, move, and erase. That is, withing the curly braces, there will be only THREE method calls.
• Verify that the output messages come from all three methods, from all seven classes.

• The only class that you should ha e to develop for this class will be the test application. the six shape classes should remain unchanged.

Explanation / Answer

import java.io.*;

import java.util.*;

public class TestShape

{

    public static void main(String[] args) throws IOException

{

    Scanner sc = new Scanner(System.in);

    int a,b,c,d,e,f,g,h;

    int length;

    String inp;

    System.out.println("Please enter the path name of the input file:");

    inp = sc.nextLine();

    try

{

    Scanner sf = new Scanner (new FileReader(inp));

    a = sf.nextInt();

    b = sf.nextInt();

    c = sf.nextInt();

    d = sf.nextInt();

    e = sf.nextInt();

    f = sf.nextInt();

    g = sf.nextInt();

    h = sf.nextInt();

    length = sf.nextInt();

    

    Ellipse normal = new Ellipse(a,b);

    Hexagon normal1 = new Hexagon(c);

    Rectangle normal2 = new Rectangle(d,e);

    Sphere normal3 = new Sphere(f);

    Triangle normal4 = new Triangle(g,h,length);

    System.out.println("Area of an ellipse is: "+normal.area());

    System.out.println("Perimeter of an ellipse is: "+normal.totalSides());

    System.out.println("Area of hexagon is: "+normal1.area());

    System.out.println("Perimeter of hexagon is: "+normal1.totalSides());  

    System.out.println("Rectangle area is: "+normal2.area());

    System.out.println("Perimeter of rectangle is: "+normal2.totalSides());

    System.out.println("Sphere surface area is: "+normal3.area());

    System.out.println("Right-angled triangle area is: "+normal4.area());

    System.out.println("Right-angled triangle perimeter is: "+normal4.totalSides());

    }

    catch (IOException x)

{

        System.out.println("Input Error!");

    } //end catch statement

    }

    }

public class Ellipse extends TestShape

{

    public int length;

    public int width;

    Ellipse(int a, int b)

    {

        this.length=a;

        this.width=b;

    }

    Ellipse()

    {

     super();

    }

    public int totalSides()

    {

        return (int) (2*Math.PI*(Math.sqrt(0.5*(length*length)+(width*width))));

    }

    public int area()

    {

        return (int) (Math.PI*length*width);

    }

    }

public class Rectangle extends TestShape

{

    public int length;

    public int width;

    Rectangle(int d,int e)

    {

        this.length=d;

        this.width=e;

    }

    Rectangle()

    {

     super();

    }

    public int totalSides()

    {

        return 2*(length*width);

    }

    public int area()

    {

        return length*width;

    }

    }

public class Triangle extends Shape

{

    public int base;

    public int height;

    public int length;

    Triangle(int g,int h,int length)

    {

        this.base=g;

        this.height=h;

        this.length=length;

    }

    Triangle()

    {

     super();

    }

    public int area()

    {

        return (int) (0.5*base*height);

    }

    public int totalSides()

    {

        return base+height+length;

    }

}