These projects were created by Leigh Anne Fitz and Lisa Ferran to provide engaging, standards-aligned learning experiences for AP Computer Science A students. The autograding tests were designed by Kate McDonnell.
Subsection4.73.1Unit 4C Project 1 - Random 2D Array
Activity4.73.1.
Create a program involving a random 2D Array. In the main method, write a program that randomly fills in 0s and 1s into an n-by-n matrix (ask the user to enter a single value for the number of rows and columns represented by n), prints the matrix using nested for loops, prints out the amount of 1s in the row with the most amount and the amount of 1s in the column with the most amount, and finds the index of the rows with the most 1s and the index of the columns with the most 1s. Be sure to list all the rows and columns with the most 1s if there is a tie (use a space between the row or column numbers if there are more than one). (Hint: Use two ArrayLists to store the number of 1s in each row and each column).
Enter the array size n: 4
The random array is:
0 0 1 1
0 0 1 1
1 1 0 1
1 0 1 0
The max 1s in any row: 3
The max 1s in any column: 3
The largest row index: 2
The largest column index: 2 3
In main, create a 2D array using the values above. Write a program that prompts users to pick either a seat (by specifying the row and column) or a price. Mark sold seats by changing the price to 0. When a user specifies a seat, make sure it is available. When a user specifies a price, find any seat with that price. If a user picks a seat that is not available, notify them as such. After the user has successfully picked a seat, print out the seat location and price and the updated 2D array. Use a while loop with a sentinel value to allow the user to pick as many seats as they would like until they indicate they are done.
Prompt Purchase Method: Ask the user to purchase by price ("p") or seat location ("s") and store the input in a String. Note: Use scan.next() instead of scan.nextLine() to avoid Scanner buffering issues when mixing Strings and ints.
If available: findPrice() updates the seat location to 0 and returns: "You purchased the seat at row __, column __ for $__" (adjust array 0-indices to 1-based theater numbers).
This method should find the first seat at that price, change that location to 0 to indicate it is now purchased, and return a String in the form: You purchased the seat at row __, column __ for $__
This static method should have three parameters: the 2D array, the row choice, and the column choice, and it should return a String that will be printed.
Otherwise, the method should change that location to 0 to indicate it is now purchased, and return a String in the form: You purchased the seat at row __, column __ for $__