Description:
Based on the lectures introducing Polymorphism, this lab allows the practice of using polymorphism in java programming. More specifically, to create a java program that will polymorphically process an array of a given interface by calling each of the interface methods. But before doing this, three different classes had to be created of which implemented the interface. The classes had to be created in order to create an object for each one of the classes and to be able to process the array of the interface afterwards. The classes will then implement the interface in such a way that a given output is correctly displayed.
Code:
package edu.cuny.citytech.CET3640.f13.Lab4; public interface Movable { public void moveForward(); public void moveBackward(); public void stop(); public void moveLeft(); public void moveRight(); }
package edu.cuny.citytech.CET3640.f13.Lab4; public class Car implements Movable { public void moveForward() { // TODO Auto-generated method stub System.out.println("Car drives forward"); } public void moveBackward() { // TODO Auto-generated method stub System.out.println("Car drives backward"); } public void stop() { // TODO Auto-generated method stub System.out.println("Car parked"); } public void moveLeft() { // TODO Auto-generated method stub System.out.println("Car turns left"); } public void moveRight() { // TODO Auto-generated method stub System.out.println("Car turns right"); } }
package edu.cuny.citytech.CET3640.f13.Lab4; public class Plane implements Movable { public void moveForward() { // TODO Auto-generated method stub System.out.println("Plane flying forward"); } public void moveBackward() { // TODO Auto-generated method stub System.out.println("Plane taxiing backward"); } public void stop() { // TODO Auto-generated method stub System.out.println("Plane landed"); } public void moveLeft() { // TODO Auto-generated method stub System.out.println("Plane flying left"); } public void moveRight() { // TODO Auto-generated method stub System.out.println("Plane flying right"); } }
package edu.cuny.citytech.CET3640.f13.Lab4; public class Ship implements Movable { public void moveForward() { // TODO Auto-generated method stub System.out.println("Ship navigates forward"); } public void moveBackward() { // TODO Auto-generated method stub System.out.println("Ship navigates backward"); } public void stop() { // TODO Auto-generated method stub System.out.println("Ship docked"); } public void moveLeft() { // TODO Auto-generated method stub System.out.println("Ship navigates left"); } public void moveRight() { // TODO Auto-generated method stub System.out.println("Ship navigates right"); } }
package edu.cuny.citytech.CET3640.f13.Lab4; public class MovableTest { public static void main(String[] args) { // TODO Auto-generated method stub Movable[] movableobject = new Movable[3]; movableobject[0] = new Plane(); movableobject[0].moveForward(); movableobject[0].moveBackward(); movableobject[0].stop(); movableobject[0].moveLeft(); movableobject[0].moveRight(); System.out.println("---------------------"); movableobject[1] = new Car(); movableobject[1].moveForward(); movableobject[1].moveBackward(); movableobject[1].stop(); movableobject[1].moveLeft(); movableobject[1].moveRight(); System.out.println("---------------------"); movableobject[2] = new Ship(); movableobject[2].moveForward(); movableobject[2].moveBackward(); movableobject[2].stop(); movableobject[2].moveLeft(); movableobject[2].moveRight(); } }
Screenshots: