Unit 4C - Code Practice with 2D Arrays

From CS Awesome 2CC BY-NC-SA 4.0Original page

Java2 class periods13 exercises34 graded tests

Files

README.mdDownload

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-arrays2dpractice1 folder and edit Test1.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-arrays2dpractice2 folder and edit Test1.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-arrays2dpractice3 folder and edit Test1.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-arrays2dpractice4 folder and edit Test1.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-arrays2dpractice5 folder and edit Test1.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-arrays2dpractice6 folder and edit Test1.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-arrays2dpractice7 folder and edit Test1.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-arrays2dpractice8 folder and edit Test1.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-arrays2dpractice9 folder and edit Test1.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-arrays2dpractice10 folder and edit Test1.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-arrays2dpractice11 folder and edit Test1.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-arrays2dpractice12 folder and edit Test1.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-arrays2dpractice13 folder and edit Test1.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

In class

  1. Students read README.md.
  2. Students do exercises 01–13, each in its own folder, pressing Run to try their code.
  3. 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

FolderFileGradingTests
01-arrays2dpractice1Test1.javaAutograded2
02-arrays2dpractice2Test1.javaAutograded2
03-arrays2dpractice3Test1.javaChecks code text only3
04-arrays2dpractice4Test1.javaAutograded5
05-arrays2dpractice5Test1.javaAutograded3
06-arrays2dpractice6Test1.javaAutograded2
07-arrays2dpractice7Test1.javaAutograded2
08-arrays2dpractice8Test1.javaAutograded3
09-arrays2dpractice9Test1.javaAutograded2
10-arrays2dpractice10Test1.javaAutograded2
11-arrays2dpractice11Test1.javaAutograded3
12-arrays2dpractice12Test1.javaAutograded3
13-arrays2dpractice13Test1.javaAutograded2

Teachers can download a reference solution after signing in.