
// CODE_RUNNER: 2D Arrays — Crossword toBeLabeled (Modified)
public class Main {
/** Determines whether the square at (row, col)
* should be assigned a crossword label.
*/
private boolean toBeLabeled(int row, int col, boolean[][] blackSquares)
{
// Black squares are never labeled
if (blackSquares[row][col]) {
return false;
}
// Squares on the top row or left column are labeled
if (row == 0 || col == 0) {
return true;
}
// Otherwise, check adjacent squares
return blackSquares[row - 1][col] || blackSquares[row][col - 1];
}
// Driver (do not modify)
public void driver() {
boolean[][] grid = {
{true, false, false},
{false, false, true },
{false, true, false}
};
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
System.out.println(
"Square (" + r + "," + c + ") labeled? " +
toBeLabeled(r, c, grid)
);
}
}
}
public static void main(String[] args) {
new Main().driver();
}
}
Main.main(null);
Square (0,0) labeled? false
Square (0,1) labeled? true
Square (0,2) labeled? true
Square (1,0) labeled? true
Square (1,1) labeled? false
Square (1,2) labeled? false
Square (2,0) labeled? true
Square (2,1) labeled? false
Square (2,2) labeled? true

// CODE_RUNNER: 2D Arrays and Objects — Crossword Constructor (Modified)
class Square {
private boolean black;
private int number;
public Square(boolean black, int number) {
this.black = black;
this.number = number;
}
public String toString() {
return black ? "BLACK" : Integer.toString(number);
}
}
public class Main {
private Square[][] puzzle;
// Assume this method works correctly (given in Part a)
private boolean toBeLabeled(int row, int col, boolean[][] blackSquares)
{
if (blackSquares[row][col]) return false;
if (row == 0 || col == 0) return true;
return blackSquares[row - 1][col] || blackSquares[row][col - 1];
}
/** Constructs a crossword puzzle grid (same behavior, different style). */
public Main(boolean[][] blackSquares)
{
int rows = blackSquares.length;
int cols = blackSquares[0].length;
puzzle = new Square[rows][cols];
int label = 1;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (blackSquares[r][c]) {
puzzle[r][c] = new Square(true, 0);
continue;
}
int value = 0;
if (toBeLabeled(r, c, blackSquares)) {
value = label;
label++;
}
puzzle[r][c] = new Square(false, value);
}
}
}
// Driver (do not modify)
public void driver() {
for (Square[] row : puzzle) {
for (Square s : row) {
System.out.print(s + "\t");
}
System.out.println();
}
}
public static void main(String[] args) {
boolean[][] grid = {
{true, false, false},
{false, false, true},
{false, true, false}
};
Main crossword = new Main(grid);
crossword.driver();
}
}
Main.main(null);
BLACK 1 2
3 0 BLACK
4 BLACK 5
Things I Learned
- I learned how to analyze a problem by identifying how an object’s state changes over time rather than focusing only on return values.
- I learned how constructors are used to initialize all instance variables so an object begins in a consistent and valid state.
- I learned how to apply conditional logic carefully to ensure that state changes occur only under the correct conditions.
- I learned how helper methods can simplify complex logic and make the main methods easier to read and debug.
- I learned how to use flags or counters to control program flow and represent changing conditions within an object.
- I learned how method calls can affect future behavior even when they do not directly return a value.
- I learned how to trace execution step by step to confirm that each method call produces the expected outcome.
- I learned how creating multiple objects from the same class results in independent instances with separate states.
Things I Struggled With
- I struggled with determining exactly when state changes should occur versus when values should remain the same.
- I struggled with structuring conditionals so that only the intended branch executes in each situation.
- I struggled with recognizing how an early logic error could cause incorrect results later in the program.
- I struggled with ensuring that all instance variables were properly updated and not unintentionally overwritten.
- I struggled with tracing long sequences of method calls without losing track of the object’s current state.
- I struggled with debugging because one incorrect condition often affected multiple outputs.
- I struggled with fully understanding how object state persists across method calls.
- I struggled with balancing clarity and conciseness while still meeting all the problem’s requirements.