Problem: Develop the \'Shape\' application such that: - Implement an array of ob
ID: 3698048 • Letter: P
Question
Problem: Develop the 'Shape' application such that:
- Implement an array of objects of various types ( all SIX classes), in anyorder.
- In some type of a looping structure, demostrate polumorphism by calling all three of the 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 have to develop for this class will be the test application. The six shape classes from Lab-7 should remain unchanged.
***In reference to this code (LAB 7)****
public class Shape {
//Default constructor
public Shape() {
System.out.println(":: Shape Object is created ::");
}
//draw method
public void draw()
{
System.out.println("Shape - draw method");
}
//Move method
public void move()
{
System.out.println("Shape - move method");
}
//erase method
public void erase()
{
System.out.println("Shape - move method");
}
}
___________________________________
public class Rectangle extends Shape
{
//Default constructor
public Rectangle()
{
System.out.println(":: Rectangle Object is created ::");
}
//Overriding the draw method
@Override
public void draw()
{
System.out.println("Rectangle - draw method");
}
//Overriding the move method
@Override
public void move()
{
System.out.println("Rectangle - move method");
}
//Overriding the erase method
@Override
public void erase()
{
System.out.println("Rectangle - move method");
}
}
______
public class Ellipse extends Shape
{
//Default constructor
public Ellipse()
{
System.out.println(":: Ellipse Object is created ::");
}
//Overriding the draw method
@Override
public void draw()
{
System.out.println("Ellipse - draw method");
}
//Overriding the move method
@Override
public void move()
{
System.out.println("Ellipse - move method");
}
//Overriding the erase method
@Override
public void erase()
{
System.out.println("Ellipse - move method");
}
}
______________________________
public class Triangle extends Shape {
//Default constructor
public Triangle()
{
System.out.println(":: Triangle Object is created ::");
}
//Overriding the draw method
@Override
public void draw()
{
System.out.println("Triangle - draw method");
}
//Overriding the move method
@Override
public void move()
{
System.out.println("Triangle - move method");
}
//Overriding the erase method
@Override
public void erase()
{
System.out.println("Triangle - move method");
}
}
______
public class Square extends Rectangle
{
//Default constructor
public Square()
{
System.out.println(":: Square Object is created ::");
}
//Overriding the draw method
@Override
public void draw()
{
System.out.println("Square - draw method");
}
//Overriding the move method
@Override
public void move()
{
System.out.println("Square - move method");
}
//Overriding the erase method
@Override
public void erase()
{
System.out.println("Square - move method");
}
}
__________________________________________
public class Circle extends Ellipse {
//Default constructor
public Circle()
{
System.out.println(":: Circle Object is created ::");
}
//Overriding the draw method
@Override
public void draw()
{
System.out.println("Circle - draw method");
}
//Overriding the move method
@Override
public void move()
{
System.out.println("Circle - move method");
}
//Overriding the erase method
@Override
public void erase()
{
System.out.println("Circle - move method");
}
}
________________________________________
public class ShapeTest {
public static void main(String[] args) {
//Creating the Shape class Object
Shape s=new Shape();
s.draw();
s.erase();
s.move();
//Creating the Rectangle class Object
Rectangle r=new Rectangle();
r.draw();
r.erase();
r.move();
//Creating the Ellipse class Object
Ellipse e=new Ellipse();
e.draw();
e.erase();
e.move();
//Creating the Triangle class Object
Triangle t=new Triangle();
t.draw();
t.erase();
t.move();
//Creating the Square class Object
Square sq=new Square();
sq.draw();
sq.erase();
sq.move();
//Creating the Circle class Object
Circle c=new Circle();
c.draw();
c.erase();
c.move();
}
}
Explanation / Answer
Hi, I have modified the code as per your requirement.
ShapeTest.java
public class ShapeTest {
public static void main(String[] args) {
//Creating the Shape class Object Array
Shape shapesArr[] = new Shape[6];
shapesArr[0] = new Shape();
shapesArr[1] = new Rectangle();
shapesArr[2] = new Ellipse();
shapesArr[3] = new Triangle();
shapesArr[4] = new Square();
shapesArr[5] = new Circle();
for(int i=0; i<shapesArr.length; i++){
Shape s = shapesArr[i];
s.draw();
s.erase();
s.move();
}
}
}
Output:
:: Shape Object is created ::
:: Shape Object is created ::
:: Rectangle Object is created ::
:: Shape Object is created ::
:: Ellipse Object is created ::
:: Shape Object is created ::
:: Triangle Object is created ::
:: Shape Object is created ::
:: Rectangle Object is created ::
:: Square Object is created ::
:: Shape Object is created ::
:: Ellipse Object is created ::
:: Circle Object is created ::
Shape - draw method
Shape - move method
Shape - move method
Rectangle - draw method
Rectangle - move method
Rectangle - move method
Ellipse - draw method
Ellipse - move method
Ellipse - move method
Triangle - draw method
Triangle - move method
Triangle - move method
Square - draw method
Square - move method
Square - move method
Circle - draw method
Circle - move method
Circle - move method
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.