Part A
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++) {
lights[i][j] = (Math.random() < 0.4);
}
}
}
public boolean evaluateLight(int row, int col)
{ return false; }
public void printBoard() {
for (int r = 0; r < lights.length; r++) {
for (int c = 0; c < lights[0].length; c++) {
System.out.print((lights[r][c] ? "T" : "F") + " ");
}
System.out.println();
}
}
}
public class Main {
public static void main(String[] args) {
LightBoard board = new LightBoard(5, 5);
System.out.println("Part A - Initial Board (T = true/on, F = false/off):");
board.printBoard();
}
}
Main.main(null)
Part A - Initial Board (T = true/on, F = false/off):
F F T F F
T T F F T
F F F T F
F F T T F
T F T F F
Part B
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++) {
lights[i][j] = (Math.random() < 0.4);
}
}
}
public boolean evaluateLight(int row, int col)
{
int count = 0;
for (int i = 0; i < lights.length; i++) {
if (lights[i][col]) {
count ++;
}
}
boolean light = lights[row][col];
if (light) {
return count % 2 == 0 ? false : light;
} else {
return count % 3 == 0 ? true : light;
}
}
public void printBoard() {
for (int r = 0; r < lights.length; r++) {
for (int c = 0; c < lights[0].length; c++) {
System.out.print((lights[r][c] ? "T" : "F") + " ");
}
System.out.println();
}
}
}
public class Main {
public static void main(String[] args) {
LightBoard board = new LightBoard(5, 5);
System.out.println("Part A - Initial Board (T = true/on, F = false/off):");
board.printBoard();
System.out.println("Part B (row 1 col 1, row 2 col 2, ... row 5 col 5):");
for (int i = 0; i < 5; i++) {
System.out.println(board.evaluateLight(i, i));
}
}
}
Main.main(null)
Part A - Initial Board (T = true/on, F = false/off):
F T F F F
F T F F T
F T T F F
F T T T F
T T F F F
Part B (row 1 col 1, row 2 col 2, ... row 5 col 5):
false
true
false
true
false
My process
Part A
- initialize the 2D array, then populate with booleans
- array created with preset row and column counts
- nested for loop to iterate through each row and column
- with Math.random(), get a random number from 0 to 1
Part B
-
Identified the core requirement of the problem, to count the “on” lights in a column regardless of the specific light’s initial status.
-
for loop iterated through the rows to calculate the total number of true values in the column.
-
Stored the total in a count variable.
-
Simplified the code by assigning lights[row][col] to a new boolean variable to avoid repetitive lookups.
-
Evaluated the status of the light using an if/else structure.
-
Implemented ternary operators within those conditions to check if the count was divisible by 2 or 3.
Struggles
Again, I struggled mostly with understanding what the problem was asking me to do. In order to get through the problem, I identified the core tasks and changed little bits of code at a time.