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;
}
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];
}
Hey Mr. Horn,
I’m curious as to why in part (a) you would have to write “less than or equal to .4” because I thought that the random number generator would generate a random double with a value of 0.0 up to but not including 1.0. If you included .4, wouldn’t you be giving the light a 41% chance of being turned on instead of a 40% chance or does the random generator include the value of 1.0?
Hi Richard,
I’m not convinced that < vs <= matters. Less than would catch 0.399… and less than or equal to would also catch 0.4. I think the difference is insignificant.
Hello Mr. Horn,
I actually have the same answer as yours (including equality) and experienced a variety of answers from my students (as well as from other teachers). I look forward to the AP reading this coming week to hear more about this.
Best regards,
Mr. Perez