FRQ 4
a) Write the constructor for the LightBoard class, which initializes lights so that each light is set to on with a 40% probability. The notation lights[r][c] represents the array element at row r and column c
(b) Write the method evaluateLight, which computes and returns the status of a light at a given row and column based on the following rules.
- If the light is on, return false if the number of lights in its column that are on is even, including the current light.
- If the light is off, return true if the number of lights in its column that are on is divisible by three.
- Otherwise, return the light’s current status.
public class LightBoard {
private boolean[][] lights;
public LightBoard(int numRows, int numCols) {
lights = new boolean[numRows][numCols];
for(int i = 0; i < lights.length; i++) {
for(int j = 0; j < lights[i].length; j++){
if (Math.random() < 0.4) {
lights[i][j] = true;
}
else {
lights[i][j] = false;
}
}
}
}
public boolean evaluateLight(int row, int col) {
int counter = 0;
for(int i = 0; i < lights.length; i++)
if(lights[i][col] == true){
counter++;
}
if( counter % 2 == 0){
return false;
}
else if(counter % 3 == 0){
return true;
}
else{
return lights[row][col];
}
}
}
LightBoard x = new LightBoard(4,4);
System.out.println(x.evaluateLight(0,0));
System.out.println(x.evaluateLight(0,1));
System.out.println(x.evaluateLight(1,2));