Answer to Question #297758 in Java | JSP | JSF for Omar

Question #297758

Use a two-dimensional array in the following problem. . Your task is to create a simple Java program which would solve this problem.


You MUST use a 2D array of Strings to solve this problem.


Ask the user which cart of laptops they wish to book. For simplicity sake assume there are 6 laptop carts available for booking.

Ask the user which period they wish to book. Assume there are 4 periods available for when they can book out laptops.

Ask the user the name of the teacher who is booking and store that name into the 2D array at the appropriate location.


Allow the user to loop the program and book another teacher if they wish. If the user picks a slot already used by a teacher it's ok, allow them to overwrite the existing teacher's name in that spot.


1
Expert's answer
2022-02-14T15:04:34-0500
import java.util.Scanner;


class App {


	public static void main(String[] args) {
		Scanner keyBoard = new Scanner(System.in);


		String[][] teacherNames = new String[6][4];


		String answer = "y";


		while (answer.compareToIgnoreCase("y") == 0) {
			int cart = -1;
			int period = -1;
			while (cart < 1 || cart > 6) {
				System.out.print("Select the cart of laptops you wish to book [1-6]: ");
				cart = keyBoard.nextInt();


			}
			while (period < 1 || period > 4) {
				System.out.print("Select the period you wish to book [1-4]: ");
				period = keyBoard.nextInt();
			}
			cart--;
			period--;
			keyBoard.nextLine();
			System.out.print("Enter the name of the teacher who is booking: ");
			teacherNames[cart][period] = keyBoard.nextLine();
			System.out.print("Do you wish to book another teacher? (y/n): ");
			answer = keyBoard.nextLine();
		}


		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 4; j++) {
				System.out.printf("%-20s", teacherNames[i][j]);
			}
			System.out.println();
		}


		keyBoard.close();
	}
}

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
APPROVED BY CLIENTS