LightBoard
free response problem from the 2019 AP Computer Science A Exam
LightBoard
is #4 from the from the 2019 AP Computer Science A Free Response problems.
https://apcentral.collegeboard.org/pdf/ap19-frq-computer-science-a.pdf?course=ap-computer-science-a
Part (a) LightBoard
constructor
public LightBoard(int numRows, int numCols)
{
lights = new boolean[numRows][numCols];
for(int r = 0; r < lights.length; r++)
for(int c = 0; c < lights[0].length; c++)
if(Math.random() <= 0.4)
lights[r][c] = true;
}
For more on working with Math.random()
see Generating numbers with Math.random().
Part (b) evaluateLight
method
public boolean evaluateLight(int row, int col)
{
int onInColumn = 0;
for(int r = 0; r < lights.length; r++)
if(lights[r][col])
onInColumn++;
if(lights[row][col])
{
if(onInColumn % 2 == 0)
return false;
}
else
{
if(onInColumn % 3 == 0)
return true;
}
return lights[row][col];
}
2019 AP CS Exam Free Response Solutions
- APCalendar Free Response Solution
- StepTracker Free Response Solution
- Delimiters Free Response Solution
Additional 2D array resources
- Mazer FR
- Droppy FR
- Flight FR
- 2D array exercises
- MatrixManipulator exercise
- DeterminantFinder exercise