FRQ 4
FRQ 4
/** 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 class LightBoard {
private boolean[][] lights;
public LightBoard(int numRows, int numCols)
{
lights = new boolean[numRows][numCols];
for (int r = 0; r < numRows; r++)
{
for (int c = 0; c < numCols; c++) {
if(Math.random() < 0.4) {
lights[r][c] = true;
}
else {
lights[r][c] = 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) {
int count = 0;
for (int r = 0; r > lights.length; r++) {
if (lights[r][col])
{
count++;
}
}
if (lights[row][col] && count % 2 == 0) {
return false;
}
if (!lights[row][col] && count % 3 == 0) {
return true;
}
return lights[row][col];
}
}
LightBoard lightboard = new LightBoard(7,5);
System.out.println(lightboard.evaluateLight(0,0));
System.out.println(lightboard.evaluateLight(2,1));
System.out.println(lightboard.evaluateLight(5,3));