"LightBoard" is a problem from the College Board's 2019 AP Computer Science A released exam. Let's take a look at it.
Get a copy of the problem from the College Board here. We're looking at problem #4 from that exam.
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.
(a) 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.
Complete the LightBoard constructor below.
(b) Write the method evaluateLight, which computes and returns the status of a light at a given row and column based on the following rules.
For example, suppose that LightBoard sim = new LightBoard(7, 5) creates a light board with the initial state shown below, where true represents a light that is on and false represents a light that is off. Lights that are off are shaded.
lights:
0 | 1 | 2 | 3 | 4 | |
0 | true | true | false | true | true |
1 | true | false | false | true | false |
2 | true | false | false | true | true |
3 | true | false | false | false | true |
4 | true | false | false | false | true |
5 | true | true | false | true | true |
6 | false | false | false | false | false |
Sample calls to evaluateLight are shown below.
Call to evaluateLight | Value Returned | Explanation |
sim.evaluateLight(0, 3); | false | The light is on, and the number of lights that are on in its column is even |
sim.evaluateLight(6, 0); | true | The light is off, and the number of lights that are on its column is divisible by 3. |
sim.evaluateLight(4, 1); | false | Returns the light's current status. |
sim.evaluateLight(5, 4); | true | Returns the light's current status. |
Class information for this question
Complete the evaluateLight method below.