In this case we need to create the matrix and randomly fill it with true and false values so that each cell has a 40% probability of being true.

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++) {
            lights[r][c] = Math.random() < 0.4;
    }
}
}

The first line instantiates and sizes lights to numRows rows and numCols columns. Going off of previous problems, I assume that many students forgot this line. If it’s not there lights would be null.

Next the code iterates through each cell in the matrix and uses Math.random to take care of the 40% probability.

public boolean evaluateLight(int row, int col) {
    int onInCol = 0;
    for (int r=0; r<lights.length; r++) {
        if (lights[r][col]) {
            onInCol++;
        }
    }
    if (lights[row][col] && onInCol % 2 == 0) {
        return false;
    } else if (!lights[row][col] && onInCol % 3 == 0) {
        return true;
    } else {
        return lights[row][col];
    }
}

Since we’ll need the number of on lights in col several times it’s best to go ahead and calculate that first. I did that and stored it in the onInCol variable.

With that we can now build a set of if statements that takes care of returning the right value based on the conditions above.