Unit 4C - More Code Practice with 2D Arrays
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java1 class period4 exercises8 graded tests
Files
Unit 4C - More Code Practice with 2D Arrays
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.
Exercise 1
Replace the “ADD CODE HERE” below to finish a method String[][] findAndReplaceFirst(String[][] sentence, String target, String replacement) that takes a 2D array of strings, a target string to search for, and a replacement string. The method searches the 2D array for the first occurrence of the target string and replaces it with the replacement string.
Open the
01-SentenceModifier-Writtenfolder and editSentenceModifier.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 2
Replace the “ADD CODE HERE” below to finish a method findMaxOnes(matrix) that accepts a two-dimensional integer array matrix as input. The method should determine the row with the maximum number of occurrences of the integer 1 and return its index. If multiple rows have the same maximum number of 1s, return the index of the first such row encountered. For example: if the input matrix is: {[ 0, 1, 1 ], [ 1, 1, 1 ], [ 1, 0, 1 ]}. The method should return 1, as the second row contains the maximum number of 1s (3 in total).
Open the
02-FindMaxOnes-Writtenfolder and editMaxOnesRowFinder.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 3
You are given a 2D array representing the seating arrangement in a theater. Each cell in the array contains either 0 (indicating an available seat) or 1 (indicating an occupied seat). Additionally, each row represents a different row in the theater, and each column represents a different seat in that row. Your task is to determine whether a specific seat at a given row and column is available while ensuring that social distancing guidelines are followed. The social distancing guideline specifies that no two occupied seats should be adjacent horizontally, vertically, or diagonally. Thus, a seat is considered available only if all adjacent seats (including diagonals) are empty. Replace the “ADD CODE HERE” below to finish a method isSeatAvailable(int[][] theater, int row, int column) that takes the theater seating arrangement and the row and column indices of the seat as input and returns true if the seat is available while adhering to social distancing guidelines, and false otherwise.
Open the
03-isSeatAvailable-Writtenfolder and editSocialDistancingSeating.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 4
Replace the “ADD CODE HERE” to finish a method diagonalSum(int[][] matrix) that takes the matrix as input and returns an array containing the sums of elements along both diagonals. The first element of the array should represent the sum of elements along the main diagonal, and the second element should represent the sum of elements along the opposite diagonal. In a square matrix, the main diagonal refers to the diagonal line of elements from the top left to the bottom right. Conversely, the opposite diagonal runs from the top right to the bottom left corner of the matrix. For example if the input matrix is { {1, 1, 1},{2, 2, 2},{1, 1, 2} }. The method should return [5,4]
Open the
04-DiagonalSum-Writtenfolder and editDiagonalSum.java. Press Run to try it; Run Tests checks it (2 tests).
For teachers
Unit 4C - More Code Practice with 2D Arrays
Students read CSAwesome2's “Unit 4C - More Code Practice with 2D Arrays” in CS Room and work through its 4 Java coding exercises.
- Time: 1 class period
- Tests: 8 in 4 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/Array2dMoreCodePractice.html
- CS Awesome's own teacher materials: the CS Awesome teacher site
In class
- Students read
README.md. - Students do exercises 01–04, 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-SentenceModifier-Written | SentenceModifier.java | Autograded | 2 |
02-FindMaxOnes-Written | MaxOnesRowFinder.java | Autograded | 2 |
03-isSeatAvailable-Written | SocialDistancingSeating.java | Autograded | 2 |
04-DiagonalSum-Written | DiagonalSum.java | Autograded | 2 |
Teachers can download a reference solution after signing in.