Lab 4
MovableApp!
Description
In this lab we were required to create a interface as the core class for 3 vehicles objects: Car , Plane , and Ship. In this program the user is asked which vehicle to start and if they would like to give it an initial coordinate. If no coordinates are input the initial value would be set to (0, 0). The user controls the movement of the vehicles with integer digits displayed by the menu. The user can always end that vehicle and initiate a new vehicle by inputting 0. The main point of this lab is to learn how to use interfaces and polymorphism.
CODE:
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(); } ______________________________________________________________________________________________ import java.util.Scanner; //allows the program to read inputs from the keyboard public class MovableApp { private static Scanner input = new Scanner(System.in); //to use the scanner class private static Movable vehicle; //my polymorphism object to take the form of Ship Plane and Car private static int selection=0, movement=0, moveBy = 0, X=0, Y=0; //User inputs private static char resp; //user input for Y or N private static boolean Try = false; //Keep track or try catch blocks private static boolean initial; //to allow user to initialize the object's coordinates public static void main(String[] args) { String[] vehicles = new String[3]; //hold objects name vehicles[0] = "Car"; vehicles[1] = "Plane";vehicles[2] = "Ship"; //objects number do { System.out.println("Movable Interface"); //title of app do { try { menuSel(); //displays the menu selection= input.nextInt(); Try = false; //if no exception will end loop } catch(Exception e) { System.out.println("!Error: You have Entered and Invalid Entry!"); input.nextLine(); Try = true; //send loop again } } while(Try); if(selection>=1 && selection <=3) //check to only allow 3 selections { System.out.println("You have Selected A "+ vehicles[selection - 1]); do{ System.out.println("Would you like to initialized a Starting point for "+ vehicles[selection - 1]+"? y/n"); input.nextLine(); resp = input.findInLine(".").charAt(0); input.nextLine(); //clears input so extra characters from flow over to variables if(resp == 'y' || resp =='Y'){ do{ try{ System.out.println("Enter Value for X"); X = input.nextInt(); System.out.println("Enter Value for Y"); Y = input.nextInt(); Try = false; }catch(Exception e){ input.nextLine(); System.out.println("Invalid Input Format!"); Try = true; } }while(Try); initial = true; }else if(resp == 'N' || resp =='n'){ initial = false; } }while((resp !='y')&&(resp !='Y')&&(resp!='N')&&(resp!='n')); switch(selection) { case 1: if(initial) vehicle = new Car(X, Y); else vehicle = new Car(); break; case 2: if(initial) vehicle = new Plane(X, Y); else vehicle = new Plane(); break; case 3: if(initial) vehicle = new Ship(X, Y); else vehicle = new Ship(); break; } moveDis(); do{ do{ try{ vehicle.displayCoordinates(); movement = input.nextInt(); Try = false; }catch(Exception e){ System.out.println("Your entry is of an invalid format!"); input.nextLine(); Try = true; } }while(Try); if(movement>0 && movement<=8){ switch(movement){ case 1: System.out.println("Moving Forward"); vehicle.moveForward(); break; case 2: System.out.println("Moving Back"); vehicle.moveBackward(); break; case 3: System.out.println("Moving Left"); vehicle.moveLeft(); break; case 4: System.out.println("Moving Right"); vehicle.moveRight(); break; case 5: vehicle.moveForward(moveBySo()); System.out.println("Moving Forward"); break; case 6: vehicle.moveBackward(moveBySo()); System.out.println("Moving Back"); break; case 7: vehicle.moveLeft(moveBySo()); System.out.println("Moving Left"); break; case 8: vehicle.moveRight(moveBySo()); System.out.println("Moving Right"); break; default: break; } } } while(movement != 0); }else if( selection3){ System.out.println("Invalid Menu option request, Please refer back to Menu Options"); } } while(selection != 0); System.out.println("Movable Interface Exiting!"); for(int i = 0; i<30; i++){ System.out.print("."); try { Thread.sleep(100); } catch (InterruptedException e) { } } System.out.println("\nGoodbye"); } static void menuSel() { System.out.println("Please make your Selection from the Following Menu Options."); System.out.println("\t[1]Car\n\t[2]Plane\n\t[3]Ship\n\t[0]Exit\n"); } static void moveDis() { System.out.println("In Which Direction Would you like to move?"); System.out.println("[1]Move Forward\t\t[2]Move Back\t\t[3]Move Left\t\t[4]Move Right"); System.out.println("[5]Specified Forward\t[6]Specified Back\t[7]Specified Left\t[8]Speficied Right"); System.out.println("[0]Return to Vehicle Selection\n"); } static int moveBySo(){ do { try{ System.out.println("Move by how much: "); moveBy = input.nextInt(); Try = false; }catch(Exception e){ input.nextLine(); System.out.println("Please only input integer values"); Try = true; } }while(Try); return moveBy; } } _______________________________________________________________________________________ public class Car implements Movable{ private int x; private int y; Car(){ setX(0); setY(0); } Car(int x, int y){ setX(x); setY(y); } public void setX(int x){ this.x = x; } public void setY(int y){ this.y = y; } @Override public void moveForward() { moveForward(1); } @Override public void moveForward(int x) { setX(this.x+x); } @Override public void moveBackward() { moveBackward(1); } @Override public void moveBackward(int x) { setX(this.x - x); } @Override public void moveLeft() { moveLeft(1); } @Override public void moveLeft(int y) { setY(this.y + y); } @Override public void moveRight() { moveRight(1); } @Override public void moveRight(int y) { setY(this.y-y); } @Override public void displayCoordinates() { System.out.println("Car is currently at ("+x+", "+y+")"); } }
OpenLap Often Crops the codes and deletes special characters In case this happens the full code can be downloaded by clicking this link
Code: Click Here
ScreenShot
Leave a Reply