Answer to Question #286119 in Java | JSP | JSF for Heather

Question #286119

Bus Seat Reservation

Please do exactly like this:

1. The program should ask the user to enter what row and seat number to reserve. Indicate

an ‘X’ mark if the seat is reserved.

2. Repeat Step 1; the program will stop until the user enters a negative number.

Please do exactly like this:

Screen/layout

Bus Seat Reservation:

Col 1 Col 2 Col 3 Col 4

row 1 | * * * *

row 2 | * * * *

row 3 | * * * *

row 4 | * * * *

row 5 | * * * *

row 6 | * * * *

row 7 | * * * *

up to row 10

Enter row and column no. to reserve separated by space (enter negative no. to exit): 4 4

Second Seat reservation sample output

Col 1 Col 2 Col 3 Col 4

row 1 | * * * *

row 2 | * * * *

row 3 | * * * *

row 4 | * * X X

up to row 10



1
Expert's answer
2022-01-10T00:24:54-0500
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[][] seats = new int[10][4];
        System.out.println("Bus Seat Reservation:");
        while (true) {
            System.out.println("Col 1 Col 2 Col 3 Col 4");
            for (int i = 0; i < seats.length; i++) {
                System.out.print("row " + (i + 1) + " | ");
                for (int j = 0; j < seats[i].length; j++) {
                    System.out.print(seats[i][j] == 0 ? "* " : "X ");
                }
                System.out.println();
            }
            System.out.print("Enter row and column no. to reserve separated by space (enter negative no. to exit): ");
            int row = in.nextInt();
            int column = in.nextInt();
            if (row < 0 || column < 0) {
                break;
            }
            seats[row - 1][column - 1]++;
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog