2019 FRQ 4
The LightBoard class models a two-dimensional display of lights, where each light is either on or off, as represented by a Boolean value. You will implement a constructor to initialize the display and a method to evaluate a light.
public class LightBoard
{
/** The lights on the board, where true represents on and false represents off.
*/
private boolean[][] lights;
/** Constructs a LightBoard object having numRows rows and numCols columns.
* Precondition: numRows > 0, numCols > 0
* Postcondition: each light has a 40% probability of being set to on.
*/
public LightBoard(int numRows, int numCols)
{ /* to be implemented in part (a) */ }
/** Evaluates a light in row index row and column index col and returns a status
* as described in part (b).
* Precondition: row and col are valid indexes in lights.
*/
public boolean evaluateLight(int row, int col)
{ /* to be implemented in part (b) */ }
// There may be additional instance variables, constructors, and methods not shown.
}
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.
Complete the LightBoard constructor below.
/** Constructs a LightBoard object having numRows rows and numCols columns.
- Precondition: numRows > 0, numCols > 0
- Postcondition: each light has a 40% probability of being set to on. */ public LightBoard(int numRows, int numCols)
public LightBoard(int numRows, int numCols)
{
lights = new boolean[numRows][numCols]; // assigngs instance variable to "lights"
for (int r = 0; r < numRows; r++) // for loop for the elements in the 2D array
{
for (int c = 0; c < numCols; c++)
{
double rnd = Math.random(); //using the Math.random function
lights[r][c] = rnd < 0.4; //40% probanbility of the light being set on
}
}
}
Part 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.
For example, suppose that LightBoard sim = new LightBoard(7, 5) creates a light board with the initial state shown below, where true represents a light that is on and false represents a light that is off. Lights that are off are shaded.
public boolean evaluateLight(int row, int col)
{
int numOn = 0;
for (int r = 0; r < lights.length; r++) // for loops for finding the status of the light
{
if (lights[r][col])
{
numOn++;
}
}
if (lights[row][col] && numOn % 2 == 0) // checking if the light is on
{
return false; // returns false if the light is on
}
if (!lights[row][col] && numOn % 3 == 0) // checking if the light is off
{
return true; // returns true if the light is off
}
return lights[row][col]; // if none of the conditions are met, returns the light's current status
}