Programming Fundamentals II – JAVA Lab #8 - Polymorphism NAME Problem: Develop t
ID: 3827901 • Letter: P
Question
Programming Fundamentals II – JAVA Lab #8 - Polymorphism NAME 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. Due Date: TBD Note: There will be no late labs!!
Explanation / Answer
Hi, You have not posted Lab7 file, so i do not know about all classes.
Please find my implementation.
//Shape class:
class Shape{
public Shape(){
System.out.println("Shape class - 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");
}
}
//Triangle class:
class Triangle extends Shape{
public Triangle(){
System.out.println("Triangle class - constructor");
}
@Override
public void draw() {
System.out.println("Triangle - draw method");
}
@Override
public void move() {
System.out.println("Triangle - move method");
}
@Override
public void erase() {
System.out.println("Triangle - erase method");
}
}
//Square class:
class Square extends Rectangle{
public Square(){
System.out.println("Square class - 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");
}
}
//Circle Class:
class Circle extends Ellipse{
public Circle(){
System.out.println("Circle class - 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");
}
}
//Ellipse class:
class Ellipse extends Shape{
public Ellipse(){
System.out.println("Ellipse class - constructor");
}
@Override
public void draw() {
System.out.println("Ellipse - draw method");
}
@Override
public void move() {
System.out.println("Ellipse - move method");
}
@Override
public void erase() {
System.out.println("Ellipse - erase method");
}
}
//Rectangle class:
class Rectangle extends Shape{
public Rectangle(){
System.out.println("Rectangle class - constructor");
}
@Override
public void draw() {
System.out.println("Rectangle - draw method");
}
@Override
public void move() {
System.out.println("Rectangle - move method");
}
@Override
public void erase() {
System.out.println("Rectangle - erase method");
}
}
#######
public class ShapeTest {
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();
Shape[] shapes = {sq, c, s, e, r, t};
for(Shape shape : shapes){
shape.draw();
shape.move();
shape.erase();
}
}
}
/*
Sample Run:
Shape class - constructor
Shape class - constructor
Rectangle class - constructor
Shape class - constructor
Ellipse class - constructor
Shape class - constructor
Triangle class - constructor
Shape class - constructor
Rectangle class - constructor
Square class - constructor
Shape class - constructor
Ellipse class - constructor
Circle class - constructor
Square - draw method
Square - move method
Square - erase method
Circle - draw method
Circle - move method
Circle - erase method
Shape - draw method
Shape - move method
Shape - erase method
Ellipse - draw method
Ellipse - move method
Ellipse - erase method
Rectangle - draw method
Rectangle - move method
Rectangle - erase method
Triangle - draw method
Triangle - move method
Triangle - erase method
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.