FRQ 4 2D Array
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.
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.
public LightBoard(int numRows, int numCols) {
lights = new boolean[numRows][numCols];
for (int r = 0; r < lights.length; r++) {
for(int c = 0; c < lights.[r].length; c++){
if (Math.random() <= .4) {
lights[r][c] = true;
}
}
}
}
Write the method evaluateLight, which computes and returns the status of a light at a given row and column based on the following rules.
need a counter to keep track of on lights in a column
public boolean evaluateLight(int row, int col) {
int lightsOn = 0;
for(int r = 0; r < lights.length; r++) {
if (lights[r][col]){ // if this reveal to true - on; not need to do if (lights[r][c] = on)
lightsOn++;
}
if (light[row][col]) {
if (lightsOn % 2 ==0){
return false;
}
if (lightsOn % 3 ==0)
return true;
}
}
return lights[row][col];
}

In FRQ 4 (nested for loops) for print print single character, except at midpoint print color code
String c = (i == (int) (ROWS / 2) && j == (int) (COLS / 2) )
? lights[row][col].getRGB()
: (j == (int) (COLS / 2)) // nested ternary
? " ".repeat(lights[row][col].getRGB().length())
: " ";
the only conditional operator that takes three operands (condition, expression1, expression2)
liner replacement for the if-then-else statement
Why use Ternary Operator?
can use the ternary operator in place of if-else conditions or even switch conditions using nested ternary operators
takes less space and helps to write the if-else statements in the shortest way possible

use if-else statements
class Main {
public static void main(String[] args) {
// create a variable
int number = 24;
if(number > 0) {
System.out.println("Positive Number");
}
else {
System.out.println("Negative Number");
}
}
}
Main.main(null);
use ternary operator
class Main {
public static void main(String[] args) {
// create a variable
int number = 24;
String result = (number > 0) ? "Positive Number" : "Negative Number"; //shorter
System.out.println(result);
}
}
Main.main(null);
nested ternary operator
class Main {
public static void main(String[] args) {
// create a variable
int n1 = 2, n2 = 9, n3 = -11;
// nested ternary operator
// to find the largest number
int largest = (n1 >= n2) ? ((n1 >= n3) ? n1 : n3) : ((n2 >= n3) ? n2 : n3);
System.out.println("Largest Number: " + largest);
}
}
Main.main(null);
(n1 >= n2) - first test condition that checks if n1 is greater than n2
(n1 >= n3) - second test condition that is executed if the first condition is true
(n2 >= n3) - third test condition that is executed if the first condition is false
However, it is not recommended to use nested ternary operators. This is because it makes our code more complex.