Lab # 4

Lab Description:

In this lab we were required to allow the user to select a vehicle ( car, plane or ship) and then be able to utilize a battleship type coordinate system to move the selected vehicles in an (x,y) coordinate plane and then display its location. Additionally this was to be done via a provided interface file and then the methods of the interface file were to be implemented in each vehicle class.

Code:

//CET 3640 Yevgeniy Babkin Lab 4
//Interfaces and implementation
******************************************
//CAR
//Car class implementing methods of the Movable interface
// Note*** There will be no comments on the other two classes
// Plane and Ship due to the fact they are almost identical to this one.

public class Car implements Movable {
	//variables which will be used for the coorinates
	int a,b;

	//implementing the moveForward method of interface
	// and supplying it with an incrementation task
	@Override
	public void moveForward() 
	{
		a++;
	}
	//implementing the moveBackward method of interface
	// and supplying it with a decrementation task
	@Override
	public void moveBackward() 
	{
		a--;
	}

	//implementing the moveLeft method of interface
	// and supplying it with a decrementation task
	@Override
	public void moveLeft() 
	{
		b--;
	}

	//implementing the moveRight method of interface
	// and supplying it with an incrementation task
	@Override
	public void moveRight() 
	{
		b++;;
	}

	//Below are the parameter filled versions of the above
	//methods where a user defined incrementatio or decremantation
	//can be achieved
	@Override
	public void moveForward(int x) {

		a = a + x;

	}

	@Override
	public void moveBackward(int x) {

		a = a - x;
	}

	@Override
	public void moveLeft(int y) {

		b = b - y;
	}

	@Override
	public void moveRight(int y) {

		b = b + y;
	}
	// the method to display the cars position
	@Override
	public void displayCoordinates() {

		System.out.println(" current car position: (" +a+","+b+")" );

	}

}

public class Plane implements Movable {
	int a,b;

	@Override
	public void moveForward() 
	{
		a++;
	}

	@Override
	public void moveBackward() 
	{
		a--;
	}

	@Override
	public void moveLeft() 
	{
		b--;
	}

	@Override
	public void moveRight() 
	{
		b++;;
	}

	@Override
	public void moveForward(int x) {

		a = a + x;

	}

	@Override
	public void moveBackward(int x) {

		a = a - x;
	}

	@Override
	public void moveLeft(int y) {

		b = b - y;
	}

	@Override
	public void moveRight(int y) {

		b = b + y;
	}

	@Override
	public void displayCoordinates() {

		System.out.println(" current plane position: (" +a+","+b+")" );

	}

}

public class Ship implements Movable {
	int a,b;

	@Override
	public void moveForward() 
	{
		a++;
	}

	@Override
	public void moveBackward() 
	{
		a--;
	}

	@Override
	public void moveLeft() 
	{
		b--;
	}

	@Override
	public void moveRight() 
	{
		b++;;
	}

	@Override
	public void moveForward(int x) {

		a = a + x;

	}

	@Override
	public void moveBackward(int x) {

		a = a - x;
	}

	@Override
	public void moveLeft(int y) {

		b = b - y;
	}

	@Override
	public void moveRight(int y) {

		b = b + y;
	}

	@Override
	public void displayCoordinates() {

		System.out.println(" current ship position: (" +a+","+b+")" );

	}

}

//this interface code was given

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

//CET 3640 Yevgeniy Babkin Lab 4
//Interfaces and implementation
//since we were not required 

// first scanner is imported for user input
import java.util.Scanner;

public class MovableApp {

//main method
	public static void main(String[] args) {

		//variables for loopin the program
		int stop = 1;
		int change =1;

        //main do while loop 
		do{
			//create objects of the vehicle classes
			Car car = new Car();
			Plane plane = new Plane();
			Ship ship = new Ship();

            //create object of scanner
			Scanner choice = new Scanner(System.in);
            //1st menu allows user to select vehicle
			System.out.println("Please select which type of Vehicle you would like to use!!!");

			System.out.println("\nCar = 1");
			System.out.println("Plane = 2");
			System.out.println("Ship = 3");

			int vehicle = choice.nextInt();

			//vehicle appropriate print out and next menu question
			if (vehicle == 1){
				System.out.println("\nYou have selected to drive a car!");
				System.out.println("\nWhich direction would you like to drive in?");
			}
			if (vehicle == 2){
				System.out.println("\nYou have selected to fly a Plane!");
				System.out.println("\nWhich direction would you like to fly in?");
			}
			if (vehicle == 3){
				System.out.println("\nYou have selected to sail a Ship!");
				System.out.println("\nWhich direction would you like to sail in?");
			}

			//next loop which allows continuous movement
			do {

				// asks user direction to travel in
				System.out.println("\nforward  = 1");
				System.out.println("backward = 2");
				System.out.println("left     = 3");
				System.out.println("right    = 4");

				int direction = choice.nextInt();

				//once again vehicle appropriate step size question
				if(vehicle==1)
					System.out.println("How many miles would you like to drive?");
				if(vehicle==2)
					System.out.println("How many miles would you like to fly?");
				if(vehicle==3)
					System.out.println("How many miles would you like to sail?");

				int miles = choice.nextInt();

				//loops for vehicles which utilize the interface and 
				//car,plane, and ship classes to update and display coordinates
				if(vehicle==1)
				{
					if (direction == 1)
					{car.moveForward(miles);
					car.displayCoordinates();}
					if (direction == 2)
					{car.moveBackward(miles);
					car.displayCoordinates();}
					if (direction == 3)
					{car.moveLeft(miles);
					car.displayCoordinates();}
					if (direction == 4)
					{car.moveRight(miles);
					car.displayCoordinates();}

				}

				//plane
				if(vehicle==2)
				{
					if (direction == 1)
					{plane.moveForward(miles);
					plane.displayCoordinates();}
					if (direction == 2)
					{plane.moveBackward(miles);
					plane.displayCoordinates();}
					if (direction == 3)
					{plane.moveLeft(miles);
					plane.displayCoordinates();}
					if (direction == 4)
					{plane.moveRight(miles);
					plane.displayCoordinates();}

				}

				//ship
				if(vehicle==3)
				{
					if (direction == 1)
					{ship.moveForward(miles);
					ship.displayCoordinates();}
					if (direction == 2)
					{ship.moveBackward(miles);
					ship.displayCoordinates();}
					if (direction == 3)
					{ship.moveLeft(miles);
					ship.displayCoordinates();}
					if (direction == 4)
					{ship.moveRight(miles);
					ship.displayCoordinates();}

				}
				// menu to allow exit and continue functions as well
				// as a change vehicle option
				System.out.println("\nkeep moving        = 1  ");
				System.out.println("change vehicles    = 2, ");
				System.out.println("exit program       = 0, ");

				stop = choice.nextInt();

				if (stop == 0)
				{
					change = 0;
				}

			}while(stop == 1);
      System.out.println(change);

		}while (change == 1);
		System.out.println("\nHave a nice day!!!");
	}
}

Screenshot:

Lab4

Leave a Reply

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