Lab4

Description: This lab is about making a program using interface. Which is often used to share common methods and constants between unrelated classes and allows them to respond to the same method calls. In this lab we create an interface “movable interface” that will be implemented by three classes: car, plane, and ship. each class should have two int fields to keep track of the coordinates and will have an initial value of 0. Also the user can initialize them to other values. the values of x and y will increase or decrease depending on the movement of the car, plane or ship, in which the x and y coordinate will increase by the number of movements the user will choose by using the overloaded methods. Also the x coordinate will increase by one when the object moves forward and decrease by one when the object moves backward. The y coordinate will increase by one will increase by one when the object moves to the right and decrease by one when the object moves to the left.

 

Code:

Movable Class:
package Lab4;

public interface Movable {

	public void moveForward();

	public void moveForward(int x);

	public void moveBackward();

	public void moveBackward(int x);

	public void moveLeft();

	public void moveLeft(int y);

	public void moveRight();

	public void moveRight(int y);

	public void displayCoordinates();
}
Car Class:
package Lab4;

	public class Car implements Movable 

	{ 
	private int x=0, y=0; 
	private static final String classname = "Car"; 

	public Car() {} 

	public Car(int x, int y) { 
	this.x = x; 
	this.y=y; 
	} 

	public void displayCoordinates() { 
	System.out.println(classname + "(" + x + "," + y + ")"); 
	} 

	public void moveForward() { 
		System.out.println("the Car moves forward");
	x += 1; 
	displayCoordinates(); 
	}

	public void moveBackward() {
		System.out.println("the Car moves backward");
		x -= 1; 
		displayCoordinates(); 
		}
	public void moveRight() { 
		System.out.println("the Car moves right");
		y += 1; 
		displayCoordinates(); 
		}
	public void moveLeft() { 
		System.out.println("the Car moves left");
		y -= 1; 
		displayCoordinates(); 
		}

	@Override
	public void moveForward(int x) {
		// TODO Auto-generated method stub

	}

	@Override
	public void moveBackward(int x) {
		// TODO Auto-generated method stub

	}

	@Override
	public void moveLeft(int y) {
		// TODO Auto-generated method stub

	}

	@Override
	public void moveRight(int y) {
		// TODO Auto-generated method stub

	}
}    

Plane Class:
package Lab4;

public class Plane implements Movable {
	private int x = 0, y = 0;
	private static final String classname = "Plane";

	public Plane() {
	}

	public Plane(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public void displayCoordinates() {

		System.out.println(classname + "(" + x + "," + y + ")");
	}

	public void moveForward() {
		System.out.println("the Plane moves forward");

		x += 1;
		displayCoordinates();
	}

	public void moveBackward() {
		System.out.println("the Plane moves backward");

		x -= 1;
		displayCoordinates();
	}

	public void moveRight() {
		System.out.println("the Plane moves right");

		y += 1;
		displayCoordinates();
	}

	public void moveLeft() {
		System.out.println("the Plane moves Left");

		y -= 1;
		displayCoordinates();
	}

	@Override
	public void moveForward(int x) {
		// TODO Auto-generated method stub

	}

	@Override
	public void moveBackward(int x) {
		// TODO Auto-generated method stub

	}

	@Override
	public void moveLeft(int y) {
		// TODO Auto-generated method stub

	}

	@Override
	public void moveRight(int y) {
		// TODO Auto-generated method stub

	}
}
Ship Class:
package Lab4;

public class Ship implements Movable {
	private int x = 0, y = 0;
	private static final String classname = "Ship";

	public Ship() {
	}

	public Ship(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public void displayCoordinates() {
		System.out.println(classname + "(" + x + "," + y + ")");
	}

	public void moveForward() {
		System.out.println("the Ship moves Forward");

		x += 1;
		displayCoordinates();
	}

	public void moveBackward() {
		System.out.println("the Ship moves backward");

		x -= 1;
		displayCoordinates();
	}

	public void moveRight() {
		System.out.println("the Ship moves right");

		y += 1;
		displayCoordinates();
	}

	public void moveLeft() {
		System.out.println("the Ship moves left");

		y -= 1;
		displayCoordinates();
	}

	@Override
	public void moveForward(int x) {
		// TODO Auto-generated method stub

	}

	@Override
	public void moveBackward(int x) {
		// TODO Auto-generated method stub

	}

	@Override
	public void moveLeft(int y) {
		// TODO Auto-generated method stub

	}

	@Override
	public void moveRight(int y) {
		// TODO Auto-generated method stub

	}
}
MovableClient Class:
package Lab4;

public class MovableClient {

	public static void main(String[] args) {
		Movable[] myMovables = new Movable[3];
		myMovables[0] = new Plane();
		myMovables[1] = new Car();
		myMovables[2] = new Ship();

		for (Movable m : myMovables) {
			m.moveForward();
			m.moveBackward();
			m.moveLeft();
			m.moveRight();
			System.out.println("---------------------");
		}
	}
}

Screenshot:

4

Leave a Reply

Your email address will not be published. Required fields are marked *