Unit 4C - Code Practice with 2D Arrays
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java2 class periods13 exercises34 graded tests
Files
Unit 4C - 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 with the code to declare and create a 3 by 3 two-dimensional int array named table. The finished code will print the values 0 to 8.
Open the
01-arrays2dpractice1folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 2
Replace the “ADD CODE HERE” below with the code to declare and initialize a two-dimensional String array called students with the names “Brice, Marvin, Anna” in the first row and “Kamal, Maria, Elissa” in the second. The finished code will print all the names in the array starting with all in the first row followed by all in the second row.
Open the
02-arrays2dpractice2folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 3
Print the values 47, 51, and 20 by accessing them in the given two-dimensional array.
Open the
03-arrays2dpractice3folder and editTest1.java. Press Run to try it; Run Tests checks it (3 tests). Its tests check your code, not what it prints: “using arr to access 47”, “using arr to access 51”, “using arr to access 20”.
Exercise 4
Print the values 8, 3, 87, and 34 by accessing them from the given two-dimensional array.
Open the
04-arrays2dpractice4folder and editTest1.java. Press Run to try it; Run Tests checks it (5 tests).
Exercise 5
Print the number of rows in the given two-dimensional array, or the length of the outer array. Then print the number of columns, or the length of each inner array.
Open the
05-arrays2dpractice5folder and editTest1.java. Press Run to try it; Run Tests checks it (3 tests).
Exercise 6
Loop through the given two-dimensional array, printing out the values in the first row followed by those in the second row and so on.
Open the
06-arrays2dpractice6folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 7
Declare and create a two-dimensional array of strings named colors. Put the colors (“red”, “yellow”, “blue”) in the first row, and the colors (“orange”, “green”, “purple”) in the second row. Then print every value in the array.
Open the
07-arrays2dpractice7folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 8
Replace the “ADD CODE HERE” below with the code to count and print the number of 7’s that are in the 2d array. It should print 2.
Open the
08-arrays2dpractice8folder and editTest1.java. Press Run to try it; Run Tests checks it (3 tests).
Exercise 9
Replace the “ADD CODE HERE” below with the code to print out the sum of the numbers in the second row of the “table” array. It should print 18.
Open the
09-arrays2dpractice9folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 10
Replace the “ADD CODE HERE” below with the code to find the sum of the values on the diagonal from [0][0] to [num rows - 1][num rows - 1]. Print the sum. It should print 5.
Open the
10-arrays2dpractice10folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 11
Replace the “ADD CODE HERE” below with the code to declare and create a two-dimensional array of integers numbers with the numbers (1,2,3) in the first row, and the numbers (4,5,6) in the second row. Then loop through the two-dimensional array, printing out the values in the first row followed by those in the second row.
Open the
11-arrays2dpractice11folder and editTest1.java. Press Run to try it; Run Tests checks it (3 tests).
Exercise 12
Replace the “ADD CODE HERE” below with the code to declare and create a two-dimensional array of integers numbers with the numbers (1,2,3) in the first row, the numbers (4,5,6) in the second row, and the numbers (7,8,9) in the third row. Then loop through the two-dimensional array, printing out the values in the first row followed by those in the second row and so on.
Open the
12-arrays2dpractice12folder and editTest1.java. Press Run to try it; Run Tests checks it (3 tests).
Exercise 13
Given the following array, replace the “ADD CODE HERE” below with 1 line of code to replace the word “purple” with “yellow” using the correct array location.
Open the
13-arrays2dpractice13folder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
For teachers
Unit 4C - Code Practice with 2D Arrays
Students read CSAwesome2's “Unit 4C - Code Practice with 2D Arrays” in CS Room and work through its 13 Java coding exercises.
- Time: 2 class periods
- Tests: 34 in 13 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/Array2dCodePractice.html
- CS Awesome's own teacher materials: the CS Awesome teacher site
In class
- Students read
README.md. - Students do exercises 01–13, 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-arrays2dpractice1 | Test1.java | Autograded | 2 |
02-arrays2dpractice2 | Test1.java | Autograded | 2 |
03-arrays2dpractice3 | Test1.java | Checks code text only | 3 |
04-arrays2dpractice4 | Test1.java | Autograded | 5 |
05-arrays2dpractice5 | Test1.java | Autograded | 3 |
06-arrays2dpractice6 | Test1.java | Autograded | 2 |
07-arrays2dpractice7 | Test1.java | Autograded | 2 |
08-arrays2dpractice8 | Test1.java | Autograded | 3 |
09-arrays2dpractice9 | Test1.java | Autograded | 2 |
10-arrays2dpractice10 | Test1.java | Autograded | 2 |
11-arrays2dpractice11 | Test1.java | Autograded | 3 |
12-arrays2dpractice12 | Test1.java | Autograded | 3 |
13-arrays2dpractice13 | Test1.java | Autograded | 2 |
Teachers can download a reference solution after signing in.