Part 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.
The first line instantiates and sizes lights to numRows rows and numCols columns. The code then iterates through each cell in the matrix and uses Math.random to meet the 40% probability requirement.
public LightBoard(int numRows, int numCols) {
lights = new boolean[numRows][numCols];
for (int r = 0; r < numRows; r++) {
for (int c = 0; c < numCols; c++) {
double rnd = Math.random();
lights[r][c] = rnd < 0.4;
}
}
}
public boolean evaluateLight(int row, int col) {
int OnCount = 0;
for (int r = 0; r < lights.length; r++) {
if (lights[r][col]) {
OnCount++;
}
}
if (lights[row][col] && OnCount % 2 == 0) {
return false;
}
if (!lights[row][col] && onCount % 3 == 0) {
return true;
}
return lights[row][col];
}