lab 4

Description:

Create a program call MovableApp that display the coordinates of car, plane and ship. In addition, allow the user to change the coordinates until the user decides to stop. The program will contain 3 classes call Car, Plane and Ship and a movable interface to track the coordinates x and y.

Code:

package lab4;

import java.util.Scanner;


public class MovableAppthat {

	private static Car car=new Car(0, 0);
	private static Plane plane=new Plane(0, 0);
	private static Ship ship=new Ship(0, 0);
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);//Read whatever value entered
		boolean flag=true;
		while(flag){
			System.out.println("1-car--2-Plane--3-Ship--4-Coordinate for 3 of them");
			switch (sc.nextInt()) {
			case 1:
				opeCar();
				break;
			case 2:
				opePlane();
				break;
			case 3:
				opeShip();
				break;
			case 4:
				car.displayCoordinates();//display coordinates for car,plane and ship
				plane.displayCoordinates();
				ship.displayCoordinates();
				break;
			
			default:
				break;
			}
		}
	}
	//get coordinate for car
	public static void opeCar(){
		Scanner sc=new Scanner(System.in);//read whatever user enter
		
		while(true){
			System.out.println("1-Move forward--2-Forward and change coordinate--3-Move left--4-Move left and change coordinate--5-Stop");
			switch (sc.nextInt()) {
			case 1:
				car.moveForward();
				car.displayCoordinates();
				break;
			case 2:
				System.out.println("Enter your coordinate");
				int i=sc.nextInt();
				car.moveForward(i);
				car.displayCoordinates();//add the coordinate user enter with the orginal
				break;
			case 3:
				car.moveLeft();
				car.displayCoordinates();
				break;
			case 4:
				System.out.println("Enter your coordinate");
				int j=sc.nextInt();
				car.moveLeft(j);
				car.displayCoordinates();//add the coordinate user enter with the orginal
				break;
			case 5:
				
				return;
			default:
				break;
		}
	
	}
	}
	
	
	//get coordinate for plane
	public static void opePlane(){
		Scanner sc=new Scanner(System.in);
		
		while(true){
			System.out.println("1-Move forward--2-Forward and change the coordinate--3-Move left--4-Move left and change the coordinate--5-Stop");
			switch (sc.nextInt()) {
			case 1:
				plane.moveForward();
				plane.displayCoordinates();
				break;
			case 2:
				System.out.println("Please enter your coordinate");//add the coordinate user enter with the orginal
				int i=sc.nextInt();
				plane.moveForward(i);
				plane.displayCoordinates();
				break;
			case 3:
				plane.moveLeft();
				plane.displayCoordinates();
				break;
			case 4:
				System.out.println("Please Enter your coordinate");//add the coordinate user enter with the orginal
				int j=sc.nextInt();
				plane.moveLeft(j);
				plane.displayCoordinates();
				break;
			case 5:
				
				return;
			default:
				break;
		}
		}
	}

	//get coordinate for ship
	public static void opeShip(){
		Scanner sc=new Scanner(System.in);
		
		while(true){
			System.out.println("1-Move forward--2-Forward and change the coordinate--3-Move left--4-Move left and change coordinate--5-Stop");
			switch (sc.nextInt()) {
			case 1:
				ship.moveForward();
				ship.displayCoordinates();
				break;
			case 2:
				System.out.println("Please enter your coordinate");//add the coordinate user enter with the orginal
				int i=sc.nextInt();
				ship.moveForward(i);
				ship.displayCoordinates();
				break;
			case 3:
				ship.moveLeft();
				ship.displayCoordinates();
				break;
			case 4:
				System.out.println("Please enter your coordinate");//add the coordinate user enter with the orginal
				int j=sc.nextInt();
				ship.moveLeft(j);
				ship.displayCoordinates();
				break;
			case 5:
				
				return;
			default:
				break;
		}
		}
	}
	
}


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();
}


public class Car implements Movable{
	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

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

	@Override
	public void moveForward() {
		// TODO Auto-generated method stub
		x=x+1;
	}

	@Override
	public void moveForward(int x) {
		// TODO Auto-generated method stub
		this.x=x+this.x;
	}

	@Override
	public void moveBackward() {
		// TODO Auto-generated method stub
		x=x-1;
	}

	@Override
	public void moveBackward(int x) {
		// TODO Auto-generated method stub
		this.x=this.x-x;
	}

	@Override
	public void moveLeft() {
		// TODO Auto-generated method stub
		y=y-1;
	}

	@Override
	public void moveLeft(int y) {
		// TODO Auto-generated method stub
		this.y=this.y-y;
	}

	@Override
	public void moveRight() {
		// TODO Auto-generated method stub
		y=y+1;
	}

	@Override
	public void moveRight(int y) {
		// TODO Auto-generated method stub
		this.y=this.y+y;
	}

	@Override
	public void displayCoordinates() {
		// TODO Auto-generated method stub
		System.out.println("Coordinate x:"+x+"---y:"+y);
	}
}


public class Plane implements Movable{
	private int x=0;
	private int y=0;
	
	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}
	
	public Plane(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}
	
	@Override
	public void moveForward() {
		// TODO Auto-generated method stub
		x=x+1;
	}

	@Override
	public void moveForward(int x) {
		// TODO Auto-generated method stub
		this.x=x+this.x;
	}

	@Override
	public void moveBackward() {
		// TODO Auto-generated method stub
		x=x-1;
	}

	@Override
	public void moveBackward(int x) {
		// TODO Auto-generated method stub
		this.x=this.x-x;
	}

	@Override
	public void moveLeft() {
		// TODO Auto-generated method stub
		y=y-1;
	}

	@Override
	public void moveLeft(int y) {
		// TODO Auto-generated method stub
		this.y=this.y-y;
	}

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

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

	@Override
	public void displayCoordinates() {
		// TODO Auto-generated method stub
		System.out.println("Coordinate x:"+x+"---y:"+y);
	}
}


public class Ship implements Movable{
	private int x=0;
	private int y=0;
	
	
	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

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

	@Override
	public void moveForward() {
		// TODO Auto-generated method stub
		x=x+1;
	}

	@Override
	public void moveForward(int x) {
		// TODO Auto-generated method stub
		this.x=x+this.x;
	}

	@Override
	public void moveBackward() {
		// TODO Auto-generated method stub
		x=x-1;
	}

	@Override
	public void moveBackward(int x) {
		// TODO Auto-generated method stub
		this.x=this.x-x;
	}

	@Override
	public void moveLeft() {
		// TODO Auto-generated method stub
		y=y-1;
	}

	@Override
	public void moveLeft(int y) {
		// TODO Auto-generated method stub
		this.y=this.y-y;
	}

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

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

	@Override
	public void displayCoordinates() {
		// TODO Auto-generated method stub
		System.out.println("Coordinate x:"+x+"---y:"+y);
	}
}

Screenshots;
1

Leave a Reply

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