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;
}

Missing the initialization of lights (the first line) is a common mistake. A constructor is responsible for setting the initial value of each instance variable. In the case of an array or list, making the array/list object is separate from assigning/adding values.

For more on working with Math.random() see Generating numbers with Math.random().

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];
}

2019 AP CS Exam Free Response Solutions

Additional 2D array resources

Help & comments

Get help from AP CS Tutor Brandon Horn

Comment on LightBoard