`Crossword`

is #3 from the from the 2016 AP Computer Science A Free Response problems.

https://secure-media.collegeboard.org/digitalServices/pdf/ap/ap16_frq_computer_science_a.pdf

## Part (a) `toBeLabeled`

method

```
private boolean toBeLabeled(int r, int c, boolean[][] blackSquares)
{
if(blackSquares[r][c])
return false;
if(r == 0 || blackSquares[r - 1][c])
return true;
if(c == 0 || blackSquares[r][c - 1])
return true;
return false;
}
```

It is not acceptable to write the opposite checks and return false. The problem specifies that either or both of the conditions could be true for the square to be labeled.

Outside of an exam setting I would write this as:

```
return ! blackSquares[r][c] && (r == 0 || blackSquares[r-1][c] || c == 0 || blackSquares[r][c-1]);
```

## Part (b) `CrossWord`

constructor

```
public Crossword(boolean[][] blackSquares)
{
puzzle = new Square[blackSquares.length][blackSquares[0].length];
int num = 1;
for(int r = 0; r < puzzle.length; r++)
{
for(int c = 0; c < puzzle[0].length; c++)
{
if(toBeLabeled(r, c, blackSquares))
{
puzzle[r][c] = new Square(false, num);
num++;
}
else
puzzle[r][c] = new Square(blackSquares[r][c], 0);
}
}
}
```

It is very common for students to miss the first line of this constructor. Setting elements in an array (or adding to a list) is not the same thing as initializing the variable.

## 2016 AP CS Exam Free Response Solutions

- RandomStringChooser Free Response Solution
- LogMessage Free Response Solution
- StringFormatter Free Response Solution

## Additional 2D array resources

## Help & comments

Get help from AP CS Tutor Brandon Horn