2019 Question 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.
}

(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)
// Answering the Question
public LightBoard(int numRows, int numCols){
    lights = new boolean[numRows][numCols];
    for(int i = 0; i < numRows; i++){
        for(int j = 0; j < numCols; j++){
            boolean temp = Math.random() < 0.4;
            lights[i][j] = temp;
        }
    }
}
// What Collegeboard did

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

Collegeboard also iterated using two variables, however they used a double (float) type to decide the on and off status of the light while I used a boolean instead.

// Printing an output
public class LightBoard {
    private boolean[][] lights;

    public LightBoard(int numRows, int numCols) {
        lights = new boolean[numRows][numCols];
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < numCols; j++) {
                boolean temp = Math.random() < 0.4;
                lights[i][j] = temp;
            }
        }
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < lights.length; i++) {
            for (int j = 0; j < lights[0].length; j++) {
                if (lights[i][j]) {
                    sb.append("on ");
                } else {
                    sb.append("off ");
                }
            }
            sb.append("\n");
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        LightBoard lightBoard = new LightBoard(5, 5);
        System.out.println(lightBoard.toString());
    }
}

LightBoard.main(null)
on on off on off 
off off off off off 
off on off off on 
on off on off off 
off off off off on 

(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.

image

// Answering the Question

public boolean evaluateLight(int row, int col){
    int numLights = 0;
    for(int i = 0; i < lights.length; i++){
        for(int j = 0; j < lights.length; j++){
            if(lights[i][j]){
                numLights++;
            }
        }
    }
    if(lights[row][col]){
        return (numLights %2 == 1);
    }
    else{
        return (numLights %3 == 0);
    }
}
// What Collegeboard did
public boolean evaluateLight(int row, int col)
{
 int numOn = 0;
 for (int r = 0; r < lights.length; r++)
 {
 if (lights[r][col])
 {
 numOn++;
 }
 }

 if (lights[row][col] && numOn % 2 == 0)
 {
 return false;
 }
 if (!lights[row][col] && numOn % 3 == 0)
 {
 return true;
 }
 return lights[row][col];
}
// Printing an Output
public class LightBoard {
    private boolean[][] lights; 
    private boolean[][] LightResults;

    public LightBoard(int numRows, int numCols) {
        lights = new boolean[numRows][numCols];
        LightResults = new boolean[numRows][numCols];
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < numCols; j++) {
                boolean temp = Math.random() < 0.4; // Makes the probability of light being on equal to 0.4
                lights[i][j] = temp; // Does it for a cell in the table
            }
        }
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < numCols; j++) { // Ensures that all of the cells in the table are evaluated
                LightResults[i][j] = evaluateLight(i, j);
            }
        }
    }

    public boolean evaluateLight(int row, int col) {
        int numLights = 0;
        for (int i = 0; i < lights.length; i++) {
            for (int j = 0; j < lights[0].length; j++) {
                if (lights[i][j]) {
                    numLights++;
                }
            }
        }
        if (lights[row][col]) {
            return (numLights % 2 == 1);
        } else {
            return (numLights % 3 == 0);
        }
    }

    
    public String getLightResults() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < LightResults.length; i++) {
            for (int j = 0; j < LightResults[0].length; j++) {
                sb.append(LightResults[i][j]).append(" ");
            }
            sb.append("\n");
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        LightBoard lightBoard = new LightBoard(4, 6);
        System.out.println(lightBoard.getLightResults());
    }
}

LightBoard.main(null)
false true true false false false 
false true true true true true 
false false true false true false 
true true false false true false 

Scoring

Part A

Intent: Define implementation of a constructor that initializes a 2D array of lights

+1 Creates a new boolean[numRows][numCols] and assigns to instance variable lights

+1 Accesses all elements in the created 2D array (no bounds errors)

+1 Computes the 40% probability

+1 Sets all values of 2D array based on computed probability

Part B

Intent: Evaluate the status of a light in a 2D array of lights

+1 Accesses an element of lights as a boolean value in an expression

+1 Traverses specified col of a 2D array (no bounds errors)

+1 Counts the number of true values in the traversal

+1 Performs an even calculation and a multiple of three calculation

+1 Returns true or false according to all three rules

Penalties

-1 (z) Constructor returns a value

-1 (y) Destruction of persistent data

My Score: 9/9

Main Takeaways

Similarities