/** * Starter Code and Tester for 2019 # 4: LightBoard * In Part (a) you write the constructor * In part (b) you write evaluateLight method * * run the main method to test your code. * * @author Chris Thiel OFMCap * @version March 14, 2020 */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). * 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. * * Precondition: row and col are valid indexes in lights. */ public boolean evaluateLight(int row, int col) { /* to be implemented in part (b) */ return false; } // There may be additional instance variables, constructors, and methods not shown that are below // implementation by CT. public String toString() { if (lights == null) return "null"; String dots="\n"; int on =0; double total = lights.length * lights[0].length; for (int r=0; r< lights.length; r++) { for (boolean bulbOn: lights[r]) { if (bulbOn) { dots += " * "; on++; } else dots += " . "; } dots += "\n"; } return dots + "\n" + (100.0*on/total) + " percent lit"; } public double percentLit() { int on = 0; int total = 0; for (boolean[] row: lights) for (boolean bulbOn:row) { total ++; if (bulbOn) on++; } return (100.0*on/total); } public LightBoard(boolean[][] arr) { lights = arr; } public static void main(String[] args) { LightBoard sim = new LightBoard(5,5); System.out.print("Test the LightBoard calss, FRQ 4 in 2019\nPart (a): \n Testing constructor: "+ sim + " (Should be 5 rows, 5 cols)"); String result = " Fail :("; if (sim.lights == null) return; if (sim.lights.length ==5 && sim.lights[0].length == 5) result = " Pass :)"; System.out.println( result +"\n"); double sum=0.0; for (int i=0; i< 1000; i++) { sim = new LightBoard(10,10); sum += sim.percentLit(); } System.out.print("Your constuctor should have 40% lit. Yours on the average makes " + sum/1000 + "%"); result = " Fail :("; if (Math.abs(40 - sum/1000) < .5) result = " Pass :)"; System.out.println( result +"\n\nPart (b):\n"); boolean[][] example = { {true,true,false,true,true}, {true,false,false,true,false}, {true,false,false,true,true}, {true,false,false,false,true}, {true,false,false,false,true}, {true,true,false,true,true}, {false,false,false,false,false} }; sim = new LightBoard(example); System.out.println("Question example should look like CS in stars: "+ sim + " "); System.out.print("\nsim.evaluateLight(0,3) should be false, \n\tThe light is on, and the number of lights \n\tthat are on in its column is even."); result = " Fail :("; if (sim.evaluateLight(0,3) == false) result = " Pass :)"; System.out.println( result +"\n"); System.out.print("\nsim.evaluateLight(6,0) should be true, \n\tThe light is off, and the number of lights \n\tthat are on in its column is divisible by 3."); result = " Fail :("; if (sim.evaluateLight(6,0) == true) result = " Pass :)"; System.out.println( result +"\n"); System.out.print("\nsim.evaluateLight(4,1) should be false, \n\tReturns the light’s current status.."); result = " Fail :("; if (sim.evaluateLight(4,1) == false) result = " Pass :)"; System.out.println( result +"\n"); System.out.print("\nsim.evaluateLight(5,4) should be true, \n\tReturns the light’s current status."); result = " Fail :("; if (sim.evaluateLight(5,4) == true) result = " Pass :)"; System.out.println( result +"\n"); } }