FRQ 4
FRQ 4 homework questions and solutions
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++) {
if(Math.random() <= 0.4) {
lights[i][j] = true;
}
}
}
}
public Boolean evaluateLight(int row, int col) {
int count = 0;
if (lights[row][col] == true) {
for (int i =0; i < lights.length; i++) {
if (lights[i][col] == true) {
count++;
}
}
if (count % 2 == 1) {
return true;
} else {
return false;
}
} else if (lights[row][col] == false) {
int count = 0;
for (int i = 0; i< lights.length; i++) {
if (lights[i][col] == true) {
count++;
}
}
if (count % 3 == 0) {
return true;
} else {
return false;
}
}
}
import java.lang.Math;
public 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++) {
if(Math.random() <= 0.4) {
lights[i][j] = true;
}
}
}
}
public boolean evaluateLight(int row, int col) {
if (lights[row][col] == true) {
int counter = 0;
for (int i = 0; i < lights.length; i++){
if (lights[i][col] == true){
counter++;
}
}
if (counter % 2 == 0){
return false;
}
else{
return true;
}
}
else {
int counter = 0;
for (int i = 0; i < lights.length; i++){
if (lights[i][col] == true){
counter++;
}
}
if (counter % 3 == 0) {
return true;
}
else {
return false;
}
}
}
}
LightBoard a = new LightBoard(5,5);
System.out.println(a.evaluateLight(0,0));
System.out.println(a.evaluateLight(0,2));
System.out.println(a.evaluateLight(0,3));