Unit 4C Projects
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java2 class periods2 exercises20 graded tests
Files
Unit 4C Projects
Each coding exercise has its own folder. Open its .java file and press Run to try it. Run Tests checks every graded exercise in this lesson at once.
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.
Unit 4C Project 1 - Random 2D Array
Exercise 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
Open the
01-unit4c-project1-random2darrayfolder and editMain.java. Press Run to try it; Run Tests checks it (10 tests). Run feeds it the input ininput.txt; change that file to try other input.
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
Scannerobject, and declare abooleanvariable to control the loop. - Main Program Loop: Use a
whileloop (controlled by the boolean variable) to allow repeated ticket purchases.- Display Seating: Print the 2D array using nested
forloops, 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 aString. Note: Usescan.next()instead ofscan.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 to0and 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."
- If available:
- 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 to0and returns:"You purchased the seat at row __, column __ for $__". - If unavailable: 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.
- If available:
- Continue or Exit: Ask the user if they want to buy another seat (
YorN). If"N", update the boolean variable to terminate the loop.
- Display Seating: Print the 2D array using nested
findPriceMethod:- 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.
findSeatMethod- 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 $__- Remember to adjust the row and column numbers again.
- This method should check whether or not the requested seat is available.
Exercise 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
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 above to help you write your code.
Open the
02-unit4c-project2-theaterseatsfolder and editMain.java. Press Run to try it; Run Tests checks it (10 tests). Run feeds it the input ininput.txt; change that file to try other input.
For teachers
Unit 4C Projects
Students read CSAwesome2's “Unit 4C Projects” in CS Room and work through its 2 Java coding exercises.
- Time: 2 class periods
- Tests: 20 in 2 graded exercises, set up for you when you import the lesson
Goals
- Practice for Unit 4: Data Collections.
Before class
- Import the lesson into your class from the Lessons page, or with New assignment → Use a lesson. It arrives as a draft with its tests already set up.
- Publish it.
- The original page is on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/unit4c-projects.html
- CS Awesome's own teacher materials: the CS Awesome teacher site
In class
- Students read
README.md. - Students do exercises 01–02, each in its own folder, pressing Run to try their code.
- Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.
Exercises
| Folder | File | Grading | Tests |
|---|---|---|---|
01-unit4c-project1-random2darray | Main.java | Autograded | 10 |
02-unit4c-project2-theaterseats | Main.java | Autograded | 10 |
Teachers can download a reference solution after signing in.