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

Question #297759

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.

  1. Ask the user which cart of laptops they wish to book. For simplicity sake assume there are 6 laptop carts available for booking.
  2. Ask the user which period they wish to book. Assume there are 4 periods available for when they can book out laptops.
  3. Ask the user the name of the teacher who is booking and store that name into the 2D array at the appropriate location.
  4. Display the 2D array to the user in a friendly way clearly showing where the teacher has booked their laptops. I recommend initializing the 2D array at the beginning of the program so your output will be user friendly.
  5. 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-14T22:17:29-0500
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[][] array = new String[6][4];
        for (String[] strings : array) {
            Arrays.fill(strings, "Empty");
        }
        do {
            System.out.println("Which cart of laptops(1-6):");
            int cart = Integer.parseInt(in.nextLine());
            System.out.println("Which period(1-4):");
            int period = Integer.parseInt(in.nextLine());
            System.out.println("The name of the teacher:");
            array[cart - 1][period - 1] = in.nextLine();
            for (int i = 0; i < array.length; i++) {
                for (int j = 0; j < array[i].length; j++) {
                    System.out.print(array[i][j] + " ");
                }
                System.out.println();
            }
            System.out.println("Continue (Y|N):");
        } while (in.nextLine().equalsIgnoreCase("y"));


    }
}

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