FRQ 4

import java.lang.Math;

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) */
        // initialize the 2D array
        lights = new boolean[numRows][numCols];

        // Using two enhanced for loops to iterate through the 2D array
        for (int i = 0; i < lights.length; i++) {
            for (int j = 0; j < lights[i].length; j++){
                // use math.random to generate a random number, less than 0.4, to determine if the light is on or off
                if (Math.random() < 0.4){
                    lights[i][j] = true;
                }
                else{
                    lights[i][j] = false;
                }
            }
        }

     }
    /** 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) */ 

        // CHECK IF LIGHT IS ON
        if (lights[row][col] == true) {
            // return false if the number of lights in its column are even
            int counter = 0;

            // iterate through every row and check for value of the column, if true increment counter
           for (int i = 0; i < lights.length; i++){
               if (lights[i][col] == true){
                   counter++;
               }
           }

           // check if counter is even or odd
           if (counter % 2 == 0){
               return false;
           }
           else{
               return true;
           }

        }
        // If light is off
        else {
            int counter = 0;
            // Count number of lights in the column
            for (int i = 0; i < lights.length; i++){
                if (lights[i][col] == true){
                    counter++;
                }
            }

            // return true if the number of lights in the column is divisible by 3
            if (counter % 3 == 0) {
                return true;
            }
            else {
                return false;
            }
        }
    }
    // There may be additional instance variables, constructors, and methods not shown. 
}

LightBoard a = new LightBoard(5,5);
System.out.println(a.evaluateLight(0,0));
System.out.println(a.evaluateLight(0,1));
System.out.println(a.evaluateLight(0,2));
false
true
false