Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Airplane {
- public static void main (String args[]) {
- String seat[] = {"X","X","X","X","X"};
- int old[] = {0, 0, 0, 0, 0};
- String empty = "X";
- while ((seat[0].equals(empty))||(seat[1].equals(empty))||(seat[2].equals(empty))||(seat[3].equals(empty))||(seat[4].equals(empty))){
- Scanner in = new Scanner(System.in);
- System.out.println("Hello. Enter seat choice. Between 1 and 5.");
- int choice = in.nextInt();
- if ((choice<1)||(choice>5)) {
- System.out.println("Error.");
- }
- else if (seat[choice-1].equals(empty)) {
- System.out.println("Seat available. Enter name.");
- String name = in.next();
- seat[choice-1] = name;
- System.out.println("Enter age.");
- int age = in.nextInt();
- old[choice-1] = age;
- }
- else {
- System.out.println("Seat unavailable. Enter seat choice again.");
- int choice2 = in.nextInt();
- if (seat[choice2-1].equals(empty)) {
- System.out.println("Seat available. Enter name.");
- String name2 = in.next();
- seat[choice2-1] = name2;
- System.out.println("Enter age.");
- int age2 = in.nextInt();
- old[choice2-1] = age2;
- }
- else {
- System.out.println("Seat unavailable. Failure.");
- }
- }
- System.out.println("");
- }
- System.out.println("** All seats reserved. **");
- System.out.println("");
- System.out.println("Seat 1 : " + seat[0] + " (" + old[0] + ")");
- System.out.println("Seat 2 : " + seat[1] + " (" + old[1] + ")");
- System.out.println("Seat 3 : " + seat[2] + " (" + old[2] + ")");
- System.out.println("Seat 4 : " + seat[3] + " (" + old[3] + ")");
- System.out.println("Seat 5 : " + seat[4] + " (" + old[4] + ")");
- for (int i=0; i<=3; i++) {
- for (int j=0; j<=3-i; j++) {
- if (old[j] > old[j+1]) {
- int temp = old[j];
- old[j] = old[j+1];
- old[j+1] = temp;
- String temp2 = seat[j];
- seat[j] = seat[j+1];
- seat[j+1] = temp2;
- }
- }
- }
- System.out.println("");
- System.out.println("Sorted by Age.");
- for (int e=0; e<=4; e++){
- System.out.println(seat[e] + " (" + old[e] + ")");
- }
- int target = 15;
- int min = 0;
- int high = 4;
- boolean found = false;
- int answer = 0;
- int mid = 0;
- while ((found==false)&&(min<=high)) {
- mid = ((min+high)/2);
- if (old[mid]==target) {
- found = true;
- answer = min;
- }
- else if (target>old[mid]) {
- min = mid+1;
- }
- else {
- high = mid-1;
- }
- }
- System.out.println("");
- if (found == true) {
- System.out.println("Age " + target + " found at array index " + answer + ".");
- }
- else {
- System.out.println("Age " + target + " was not found.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement