Advertisement
glee20

Airplane

Oct 9th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1. import java.util.*;
  2. class Airplane {
  3.     public static void main (String args[]) {
  4.         String seat[] = {"X","X","X","X","X"};
  5.         int old[] = {0, 0, 0, 0, 0};
  6.         String empty = "X";
  7.         while ((seat[0].equals(empty))||(seat[1].equals(empty))||(seat[2].equals(empty))||(seat[3].equals(empty))||(seat[4].equals(empty))){
  8.             Scanner in = new Scanner(System.in);
  9.             System.out.println("Hello. Enter seat choice. Between 1 and 5.");
  10.             int choice = in.nextInt();
  11.             if ((choice<1)||(choice>5)) {
  12.                 System.out.println("Error.");
  13.             }
  14.             else if (seat[choice-1].equals(empty)) {
  15.                 System.out.println("Seat available. Enter name.");
  16.                 String name = in.next();
  17.                 seat[choice-1] = name;
  18.                 System.out.println("Enter age.");
  19.                 int age = in.nextInt();
  20.                 old[choice-1] = age;
  21.             }
  22.             else {
  23.                 System.out.println("Seat unavailable. Enter seat choice again.");
  24.                 int choice2 = in.nextInt();
  25.                 if (seat[choice2-1].equals(empty)) {
  26.                     System.out.println("Seat available. Enter name.");
  27.                     String name2 = in.next();
  28.                     seat[choice2-1] = name2;
  29.                     System.out.println("Enter age.");
  30.                     int age2 = in.nextInt();
  31.                     old[choice2-1] = age2;
  32.                 }
  33.                 else {
  34.                     System.out.println("Seat unavailable. Failure.");
  35.                 }
  36.             }
  37.             System.out.println("");
  38.         }
  39.         System.out.println("** All seats reserved. **");
  40.         System.out.println("");
  41.         System.out.println("Seat 1 : " + seat[0] + " (" + old[0] + ")");
  42.         System.out.println("Seat 2 : " + seat[1] + " (" + old[1] + ")");
  43.         System.out.println("Seat 3 : " + seat[2] + " (" + old[2] + ")");
  44.         System.out.println("Seat 4 : " + seat[3] + " (" + old[3] + ")");
  45.         System.out.println("Seat 5 : " + seat[4] + " (" + old[4] + ")");
  46.         for (int i=0; i<=3; i++) {
  47.             for (int j=0; j<=3-i; j++) {
  48.                 if (old[j] > old[j+1]) {
  49.                     int temp = old[j];
  50.                     old[j] = old[j+1];
  51.                     old[j+1] = temp;
  52.                     String temp2 = seat[j];
  53.                     seat[j] = seat[j+1];
  54.                     seat[j+1] = temp2;
  55.                 }
  56.             }
  57.         }
  58.         System.out.println("");
  59.         System.out.println("Sorted by Age.");
  60.         for (int e=0; e<=4; e++){
  61.             System.out.println(seat[e] + " (" + old[e] + ")");
  62.         }
  63.         int target = 15;
  64.         int min = 0;
  65.         int high = 4;
  66.         boolean found = false;
  67.         int answer = 0;
  68.         int mid = 0;
  69.         while ((found==false)&&(min<=high)) {
  70.             mid = ((min+high)/2);
  71.             if (old[mid]==target) {
  72.                 found = true;
  73.                 answer = min;
  74.             }
  75.             else if (target>old[mid]) {
  76.                 min = mid+1;
  77.             }
  78.             else {
  79.                 high = mid-1;
  80.             }
  81.         }
  82.         System.out.println("");
  83.         if (found == true) {
  84.             System.out.println("Age " + target + " found at array index " + answer + ".");
  85.         }
  86.         else {
  87.             System.out.println("Age " + target + " was not found.");
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement