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

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 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)
        { 
            this.numRows = numRows;
            this.numCols = numCols;
            lights = new boolean[numRows][numCols];
            for(int i = 0; i < lights.length;i++){
                for(int j = 0; j <lights[i].length;j++){ 
                    double random = Math.random();
                    lights[i][j] = random < 0.4;
                    System.out.print(numbers[i][j]+" ");
                }
        }

    /** 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 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.
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)
        { 
            this.numRows = numRows;
            this.numCols = numCols;
            lights = new boolean[numRows][numCols];
            for(int i = 0; i < lights.length;i++){
                for(int j = 0; j <lights[i].length;j++){ 
                    double random = Math.random();
                    lights[i][j] = random < 0.4;
                    System.out.print(numbers[i][j]+" ");
                }
        }
    }

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

            else-if (lights[row][col] == false) {
                for(int j = 0; j <lights[row].length;j++){ 
                    if (lights[row][j]) {
                        numColOn += 1;
                    }
                    if (numColOn % 3 == 0) {
                        return true;
                    } 
            }
            else {
                return light[row][column];
            }
            
        }
    // There may be additional instance variables, constructors, and methods not shown.
} 
}
}

Unit 8 Vocab and Code Examples

  • array of arrays, type of multidimensional array
// initialize array
int[][] numbers;
String[][] names;
char[][] letters;

int[][] numbers1 = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};//method 1:
int[][] numbers2 = new int[4][3]; //method 2: Creates array with four rows and 3 columns

Iteration

  • use a for loop to loop through each element
  • nested loop to print each value by going through row and then column within that row
String[][] alphabet = {{"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "="},
{"q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", "\\"},
{"a", "s", "d", "f", "g", "h", "j", "k", "l"},
{"z", "x", "c", "v", "b", "n", "m", ",", ".", "/"}};
for(int i = 0;i<alphabet.length;i++){
    for(int j = 0; j < alphabet[i].length;j++){ //nested for loops
        System.out.print(alphabet[i][j]+" ");
    }
    System.out.println(" ");
}
1 2 3 4 5 6 7 8 9 0 - =  
q w e r t y u i o p [ ] \  
a s d f g h j k l  
z x c v b n m , . /  

Accessing elements

  • use row, column
System.out.println(alphabet[1][2])
e