FRQ 4 - Lightboard

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.
*/
import java.lang.Math;

public LightBoard(int numRows, int numCols) {
    lights = new boolean lights[numRows][numCols];
    for(r=0; r < numRows; r++){
        for(c=0; c < numCols; c++){
            int probability = 100 * Math.random();
            if(probability <= 40){
                lights[r][c] = true;
            }
            else{
                lights[r][c] = false;
            }
        }

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.

  1. If the light is on, return false if the number of lights in its column that are on is even, including the current light.
  2. If the light is off, return true if the number of lights in its column that are on is divisible by three.
  3. 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.

lights |---| 0 | 1 | 2 | 3 | 4 | |---|---|---|---|---|---| |0| true | true | false | true | true | |1| true | false | false | true | false | |2| true | false | false | true | true | |3| true | false | false | false | true | |4| true | false | false | false | true | |5| true | true | false | true | true | |6| false | false | false | false | false |

Sample calls to evaluateLight are shown below. | Call to evaluateLight | Value Returned | Explanation | |-----------------------|----------------|-------------| |sim.evaluateLight(0, 3);| false | The light is on, and the number of lights that are on in its column is even.| |sim.evaluateLight(6, 0);| true | The light is off, and the number of lights that are on in its column is divisible by 3.| |sim.evaluateLight(4, 1);| false |Returns the light’s current status.| |sim.evaluateLight(5, 4);| true | Returns the light’s current status. |


Class information for this question

  • public class LightBoard
  • private boolean[][] lights
  • public LightBoard(int numRows, int numCols)
  • public boolean evaluateLight(int row, int col)

Complete the evaluateLight method below.

/** 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){
    iif (lights[row][col] == true) {
        int count = 0;
        for (int r = 0; r < lights.length; r++){
           if (lights[r][col] == true){
               count++;
           }
        }
        if (count % 2  == 1){
           return true;
        }
        else{
           return false;
        }

    } 
    else if (lights[row][col] == false) {
        int count = 0;
        for (int r = 0; r < lights.length; r++){
            if (lights[r][col] == true){
                count++;
            }
        }
        if (count % 3 == 0) {
            return true;
        }
        else {
            return false;
        }
    }
}