Unit 4C - Toggle Code Practice 4c 2D Arrays (4.11-4.13)
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java2 class periods10 exercises14 graded tests
Files
Unit 4C - Toggle Code Practice 4c 2D Arrays (4.11-4.13)
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.
For each of the coding problems below, write your own solution.
Exercise 1
Fix the errors (marked by comments) in the code so that it correctly creates a 10x10 array called table filled with numbers from 0 to 99 in left-right top-bottom order and prints the output (in row-column order) with the numbers separated by tabs. Most of the errors are syntactical, but one is logical.
Open the
01-u4c-toggle1folder and editTest1.java. Press Run to try it; Run Tests checks it (1 test).
Exercise 2
This program is supposed to create an 8x8 two-dimensional int array that is filled with a checkered patterns of 0s and 1s, starting with a 1 in the top left corner. It should also print the output in row-column order, separating each element with a space. The only missing part is the if statement that decides if a specific cell should be a 0 or a 1. Fill that in to finish the problem.
Hint: Try drawing out a smaller version (around 4x4) of a checkered two-dimensional array. Can you notice a pattern in the row numbers and column numbers of the 1s?
Open the
02-u4c-toggle2folder and editTest1.java. Press Run to try it; Run Tests checks it (1 test).
Exercise 3
The sumVals method in the below program should iterate through a two-dimensional int array nums and return its sum. Fill in the headers for the for loops such that the method iterates through the entirety of nums.
Open the
03-u4c-toggle3folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 4
Fill in the flipImage method. This should accept a two-dimensional String array image and reverse each row from left to right. For example, { {"green", "red", "blue"}, {"cat", "dog", "yellow"} } would become { {"blue", "red", "green"}, {"yellow", "dog", "cat"} }.
Open the
04-u4c-toggle4folder and editTest1.java. Press Run to try it; Run Tests checks it (1 test).
Exercise 5
Write the makeEvenNumsZero method such that it iterates through the two-dimensional int array nums and replaces each instance of an even number with 0. For example, { {3, 4, 5}, {6, 7, 8} } would become { {3, 0, 5}, {0, 7, 0} }.
Open the
05-u4c-toggle5folder and editTest1.java. Press Run to try it; Run Tests checks it (1 test).
Exercise 6
Fill in the numOccurrences method. It should take in a two-dimensional int array nums and an int desired and return the number of times that desired appears in nums. E.g., with { {3, 1, 2}, {3, 4, 1} } as nums, numOccurrences(nums, 1) should return 2.
Open the
06-u4c-toggle6folder and editTest1.java. Press Run to try it; Run Tests checks it (1 test).
Exercise 7
Fill in the averageCols method. It should accept a two-dimensional int array nums and return a one-dimensional (normal) int array containing the integer average of each of the columns (NOT the rows). E.g., with nums as { {3, 5, 2}, {1, 1, 6} }, averageCols(nums) should return {2, 3, 4} as that is (3 + 1) / 2, (5 + 1) / 2, and (2 + 6) / 2.
Open the
07-u4c-toggle7folder and editTest1.java. Press Run to try it; Run Tests checks it (1 test).
Exercise 8
Write the oddRows method. This should take in a two-dimensional int array nums and return a new two-dimensional int array containing only the odd-index rows. For example, with nums equal to { {3, 2, 1}, {4, 5, 6}, {1, 5, 7} }, oddRows(nums) would return { {4, 5, 6} } as that was the row at index 1, which is the only odd index present.
Open the
08-u4c-toggle8folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 9
Write the breakIntoLetters method. It should accept a two-dimensional String array, in which each row contains the characters of a word. It should then return a single-dimensional (normal) String array containing the words in each row of the two-dimensional array. For example, calling breakIntoLetters on { {“b”, “a”, “t”, “h”}, {“t”, “e”, “n”, “s”}, {“j”, “a”, “c”, “k”}, {“l”, “a”, “z”, “y”} } should return {“bath”, “tens”, “jack”, “lazy”}.
Open the
09-u4c-toggle9folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 10
Write the maxEachRow method. It should accept a two-dimensional int array nums, and return a single-dimensional (normal) int array containing the max of each row. For example, the returned array for { {3}, {4, 9, 6, -1}, {45, 1} } should be {3, 9, 45}.
Open the
10-u4c-toggle10folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
For teachers
Unit 4C - Toggle Code Practice 4c 2D Arrays (4.11-4.13)
Students read CSAwesome2's “Unit 4C - Toggle Code Practice 4c 2D Arrays (4.11-4.13)” in CS Room and work through its 10 Java coding exercises.
- Time: 2 class periods
- Tests: 14 in 10 graded exercises, set up for you when you import the lesson
Goals
- Practice for topics 4.11–4.13: 4.11 2D Array Creation and Access, 4.12 2D Array Traversals: Nested Loops, 4.13 Implementing 2D Array Algorithms.
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-practice-toggle-code.html
- CS Awesome's own teacher materials: the CS Awesome teacher site
In class
- Students read
README.md. - Students do exercises 01–10, 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-u4c-toggle1 | Test1.java | Autograded | 1 |
02-u4c-toggle2 | Test1.java | Autograded | 1 |
03-u4c-toggle3 | Test1.java | Autograded | 2 |
04-u4c-toggle4 | Test1.java | Autograded | 1 |
05-u4c-toggle5 | Test1.java | Autograded | 1 |
06-u4c-toggle6 | Test1.java | Autograded | 1 |
07-u4c-toggle7 | Test1.java | Autograded | 1 |
08-u4c-toggle8 | Test1.java | Autograded | 2 |
09-u4c-toggle9 | Test1.java | Autograded | 2 |
10-u4c-toggle10 | Test1.java | Autograded | 2 |
Teachers can download a reference solution after signing in.