Skip to main content

Section 4.73 Unit 4C Projects

90 minutes
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.

Subsection 4.73.1 Unit 4C Project 1 - Random 2D Array

Activity 4.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).
Example output:
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

Subsection 4.73.2 Unit 4C Project 2 - Theater Seats

Create a program about Theater Seating. A theater seating chart is implemented as a two-dimensional array of ticket prices, like this:
10	10	10	10	10	10	10	10	10	10
10	10	10	10	10	10	10	10	10	10
10	10	10	10	10	10	10	10	10	10
10	10 	20 	20 	20 	20 	20 	20 	10 	10
10 	10 	20 	20 	20 	20 	20 	20 	10 	10
10 	10 	20 	20 	20 	20 	20 	20 	10 	10
20 	20 	30 	30 	40 	40 	30 	30 	20 	20
20 	30 	30 	40 	50 	50 	40 	30 	30 	20
30 	40 	50 	50 	50 	50 	50 	50 	40 	30
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.
Use the outline below to help you create this program.
  • Setup & Initialization: Create a 2D array using an initializer list, initialize a Scanner object, and declare a boolean variable to control the loop.
  • Main Program Loop: Use a while loop (controlled by the boolean variable) to allow repeated ticket purchases.
    • Display Seating: Print the 2D array using nested for loops, putting a tab between each entry.
    • 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.
    • Option A β€” Purchase by Price: Ask for the target price and pass it along with the 2D array to a static method named findPrice().
      • 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).
      • If unavailable: Returns "There is no seat available at that price."
    • Option B β€” Purchase by Location: Ask for the row and column numbers and pass them along with the 2D array to a static method named findSeat().
      • If available: findSeat() updates the seat location to 0 and returns: "You purchased the seat at row __, column __ for $__".
      • Returns: "That seat is not available."
      • Note: Subtract 1 from the user’s row and column inputs to map 1-based seat choices to 0-based array indices.
    • Ask the user if they want to buy another seat (Y or N). If "N", update the boolean variable to terminate the loop.
  • findPrice Method:
    • This static method should take two parameters: the 2D array and the price, and return a String that will be printed in the if statement.
    • 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 $__
    • Remember that even though the 2D array starts at row 0, column 0, the theater would start at row 1, column 1.
    • If there are no seats left at that price, the method should return the String: There is no seat available at that price.
  • findSeat Method
    • This method should check whether or not the requested seat is available.
      • Remember that the user will enter a seat thinking that the first row and column are 1, not 0, so be sure to adjust the values for the 2D array.
    • 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.
    • If the seat is not available, the method should return the String: That seat is not available.
    • 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 $__

Activity 4.73.2.

Create a program about Theater Seating. A theater seating chart is implemented as a two-dimensional array of ticket prices, like this:
10	10	10	10	10	10	10	10	10	10
10	10	10	10	10	10	10	10	10	10
10	10	10	10	10	10	10	10	10	10
10	10 	20 	20 	20 	20 	20 	20 	10 	10
10 	10 	20 	20 	20 	20 	20 	20 	10 	10
10 	10 	20 	20 	20 	20 	20 	20 	10 	10
20 	20 	30 	30 	40 	40 	30 	30 	20 	20
20 	30 	30 	40 	50 	50 	40 	30 	30 	20
30 	40 	50 	50 	50 	50 	50 	50 	40 	30
Use the outline above to help you write your code.
You have attempted of activities on this page.