Part A

  • creates an array using the boolean [numRows][numCols]
  • Access elements in 2d array that was created
  • Computes 40% probability
  • Sets all values of 2D array based on the computed probability
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 onoff = Math.random();
            lights[r][c] = onoff < 0.4;
        }
    }
}

Part B

  • Accesses an element of lights as a boolean
  • Traverses the columns of 2D array and counts the number of trues (lights are off & number of lights is divisible by 3)
  • Performs division calculation
  • Returns true or false according to three rules
public boolean evaluateLight(int row, int col){
    int lightsOn = 0;
    for (int r = 0; r < lights.length; r++){
        if (lights[r][col]){
            lightsOn++;
        }
    }
    if (lights[row][col] && lightsOn % 2 == 0){
        return false;
    }
    if (!lights[row][col] && lightsOn % 3 == 0){
        return true;
    }
    return lights[row][col];
}