Lab 2: 2D Array Practice
In the 2D Array Arcade, you practiced finding locations, tracing traversals, recognizing boundaries, and seeing how a hidden 2D array can represent a visible world. In this lab, you will implement those ideas in Java.
You will work in a scaffolded Grid.java. Each task contains a method with a
TODO, examples, and hints. After completing a task, run
GridTester.java. The tester works in order, so it will help you focus on the
first task that still needs work.
The backing grid
A 2D array is the model: the data stored by the program. Code can read that model, modify it, transform it, or create a user-facing view of it.
grid[row][column] first index = row second index = column
Keep asking yourself two questions: Which location am I visiting? and Am I reading the original grid, changing it, or creating something new?
Setup
Download both files into the same folder. Do not rename them.
Testing Your Own Code with main()
GridTester.java checks your work, but you should also test your methods yourself.
The main() method in Grid.java is your testing space. You may add code
there to create a grid, call one of your methods, and examine the result.
For example:
public static void main(String[] args) {
int[][] grid = {
{1, 0, 1},
{0, 1, 0},
{1, 1, 0}
};
printGrid(grid);
int count = countValue(grid, 1);
System.out.println("Number of 1s: " + count);
}
Compile and run Grid.java directly:
javac Grid.java java Grid
Passing values from the command line
You can also pass values to main() when you run your program. For example:
java Grid 2 7
Those two values arrive in the args array:
args[0] // "2" args[1] // "7"
Command-line arguments are Strings. Convert them to integers before passing
them to a method that expects an int:
int row = Integer.parseInt(args[0]); int value = Integer.parseInt(args[1]); setRow(grid, row, value); printGrid(grid);
java Grid 2 7 does not call
setRow() automatically. The terminal passes "2" and "7"
to main(String[] args). Your main() converts those values and then
passes them to setRow(grid, row, value).
Experiment Before Running the Tester
Do not use the tester as your first way of finding out whether your method works.
Before running the tester, experiment with the method you are writing by calling it from
main(). Try different grid values and different inputs. Print the grid before
and after a modification when that helps you see what your code is doing.
Your goal is to predict what should happen, run your own test, and compare the result with your prediction. Use these experiments to verify that you understand the problem and that your method behaves correctly in more than one situation.
When you are convinced that your method is working properly, then run the tester.
Run the Tester
Compile Grid.java and GridTester.java together, then run the tester:
javac Grid.java GridTester.java java GridTester
The tester is a final check for each task. When a task is correct, it will mark that task
complete and move on. If the tester shows that a task still needs work, return to
main(), experiment again, revise your method, and then rerun the tester.
main() → predict and inspect the results → run GridTester.
Core Lab Tasks
Lab 2 has six required tasks. Complete all six tasks in order and verify each one with
GridTester. After Task 6 is complete, show your work to a TA or instructor
for the Lab 2 checkpoint.
Task 1 — printGrid()
READ — examine the grid without changing it.
Complete printGrid(int[][] grid) so that it visits every location and prints one row per line.
Input grid: Output: 0 2 4 0 2 4 1 3 5 1 3 5
This is the fundamental full-grid traversal. You need one loop to move through the rows
and another loop to move through the columns in each row. The Javadoc in
Grid.java gives you the exact output format.
When finished: compile and run GridTester.
Task 2 — countValue()
READ — traverse the entire grid and accumulate a result.
Complete countValue(int[][] grid, int value).
1 0 1 0 1 2 countValue(grid, 1) → 3 countValue(grid, 2) → 1 countValue(grid, 7) → 0
You still visit every location, but now each visit has a purpose: compare the value at
grid[row][col] with the value you are looking for and update a counter when they match.
Task 3 — setRow() and setColumn()
MODIFY — change values in the existing backing grid.
These two methods look similar, but they require different traversals. In
setRow(), the row stays fixed while the column changes. In
setColumn(), the column stays fixed while the row changes.
Original: setRow(grid, 1, 7): 1 2 3 4 1 2 3 4 5 6 7 8 7 7 7 7 9 0 1 2 9 0 1 2 Original: setColumn(grid, 2, 9): 1 2 3 4 1 2 9 4 5 6 7 8 5 6 9 8 9 0 1 2 9 0 9 2
Before coding: identify which index stays fixed and which index changes in each method.
Task 4 — clearBorder()
MODIFY — use row and column locations to decide what should change.
Set every location on the outside border to 0, while leaving all interior values unchanged.
Before: After: 5 5 5 5 5 0 0 0 0 0 5 1 2 3 5 0 1 2 3 0 5 4 5 6 5 0 4 5 6 0 5 5 5 5 5 0 0 0 0 0
A location is on the border if it is in the:
- first row,
- last row,
- first column, or
- last column.
The challenge is no longer just traversing the array. You must use the current
row and col to decide whether that location belongs to the border.
Task 5 — displayGrid()
READ / VIEW — use a backing array to create a user-facing representation.
This task connects directly to the final level of the 2D Array Arcade. The values in the array are the model. What the user sees is the view.
BACKING ARRAY (MODEL) USER-FACING VIEW 0 1 0 . # . 1 1 0 ----> # # .
For this method:
0displays as.1displays as#
Do not replace the values in the array. The model remains an int[][]
containing 0s and 1s. Your method simply chooses what to print for each value.
This model/view distinction will return immediately in Project 2: the program will have a backing grid representing the world, while a separate display shows that world to the user.
Grid Transformations
The final task asks you to transform a grid in place. Read the method documentation carefully before you code.
Task 6 — transpose()
TRANSFORM IN PLACE — rearrange the values in the original square grid.
This is the most challenging traversal in the lab. You may assume the grid is square. Do not create a new grid. Instead, swap values across the main diagonal.
Before: After: 1 2 3 1 4 7 4 5 6 ----> 2 5 8 7 8 9 3 6 9
Think about these pairs:
grid[0][1] <--> grid[1][0] grid[0][2] <--> grid[2][0] grid[1][2] <--> grid[2][1]
Values on the main diagonal do not move. More importantly, each pair must be swapped
only once. If you later visit the same pair in the opposite direction, you will undo
the first swap. The scaffold in Grid.java gives you additional guidance.
Lab 2 complete
When the tester reports all six required tasks complete, show your work to a TA or instructor. All six tasks are required for the Lab 2 checkpoint. You have now practiced full-grid traversal, row and column traversal, location-based modification, model/view representation, and an in-place transformation.
After your checkpoint and submission, you are ready to begin Project 2.
Submission
Before the end of the lab period, navigate to your personal Google Drive folder. Inside your
Projects folder, create a new folder named Project2. Copy your completed
Grid.java file into the Project2 folder.
Your Project 2 folder should begin like this
Projects/
└── Project2/
└── Grid.java
Your Grid.java must be in this folder for grading. As you begin Project 2,
add the Conway's Game of Life files to this same Project2 folder. It is your
responsibility to make sure Grid.java is copied there before you leave lab.
Ready for Project 2?
Return to the CS231 course page, navigate to the Projects page, and open Project 2: Conway's Game of Life.