I have a Polymorhpism Project that builds upon an Inheritance Project. I have ad
ID: 3572221 • Letter: I
Question
I have a Polymorhpism Project that builds upon an Inheritance Project. I have added the main file of my Inheritance project below (it's the only file that needs to be changed) and I was hoping I could get some help with these requirements:
Problem: Develop the ‘Shape’ application such that:
Implement an array of objects of various types (all SIX classes), in any order.
In some type of a looping structure, demonstrate polymorphism by calling all three of the methods, draw, move, and erase. That is, within 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.
Inheritance Project (main file):
import java.util.*;
import java.lang.*;
import java.io.*;
public class Inheritance {
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();
}
}
Explanation / Answer
Hi Friend, Please find my code. I have put all objects in array of Shapes and iterated useing foreach loop.
I can not test because you have not posted Lan-7 classes.
import java.util.*;
import java.lang.*;
import java.io.*;
public class Inheritance {
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();
// putting all these objects in an Shape Array
Shape[] shapes = {s, r, e, t, sq, c};
// now iterating
for(Shape s : shapes){
s.draw();
s.move();
s.erase();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.