Problem: Develop the ‘Shape’ application such that: • Implement an array of obje
ID: 3634267 • Letter: P
Question
Problem: Develop the ‘Shape’ application such that:• Implement an array of objects of various types (all five 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 seven classes from previous program should remain unchanged.
I need help with setting the Array up and making it pick three random classes to display.
package shapetester;
public class ShapeTester {
public static void main(String[] args) {
{
Shape shape;
shape = new Rectangle();
test(shape);
shape = new Square();
test(shape);
shape = new Ellipse();
test(shape);
shape = new Circle();
test(shape);
shape = new Triangle();
test(shape);
}
}
static void test
(Shape shape) {
shape.draw();
shape.move();
shape.erase();
}
}
Here are the rest of the classes
package shapetester;
public class Circle extends Ellipse {
// circle takes the properties of ellipse
public Circle() {
super();
System.out.println("Circle-constructor");
}
}
package shapetester;
class Ellipse extends Shape {
//ellipse has inheritance from shape
public Ellipse() {
super();
System.out.println("Ellipse-constructor");
}
public void draw() {
System.out.println("Draw Ellipse");
}
public void erase() {
System.out.println("Erase Ellipse");
}
public void move() {
System.out.println("Move Ellipse");
}
}
package shapetester;
class Rectangle extends Shape {
//retangle has inheritance from shape
public Rectangle() {
super();
System.out.println("Rectangle-constructor");
}
public void draw() {
System.out.println("Draw rectangle");
}
public void erase() {
System.out.println("Erase rectangle");
}
public void move() {
System.out.println("Move rectangle");
}
}
package shapetester;
public class Shape {
{
{
System.out.println("Initialize shape");
}
}
//draw move erase methods
public void draw(){};
public void move(){};
public void erase(){};
}
package shapetester;
public class Square extends Rectangle {
// square takes the properties of rectangle
public Square() {
super();
System.out.println("Square-constructor");
}
}
package shapetester;
class Triangle extends Shape {
//triangle has inheritance from shape
public Triangle() {
super();
System.out.println("Triangle-constructor");
}
public void draw() {
System.out.println("Draw triangle");
}
public void erase() {
System.out.println("Erase triangle");
}
public void move() {
System.out.println("Move triangle");
}
}
Explanation / Answer
Dear,
// ShapeTester.java (Test Program)
package shapetester;
public class ShapeTester {
public static void main(String[] args) {
// array of five shape object
Shape shapes[] = new Shape[5];
// instantiating the array objects
shapes[0] = new Rectangle();
shapes[1] = new Square();
shapes[2] = new Ellipse();
shapes[3] = new Circle();
shapes[4] = new Triangle();
// for each object in the array calling thier
// methods
for ( Shape s : shapes )
{
s.draw();
s.move();
s.erase();
}
}
}
I hope this will helps you...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.