COM1204 Summer Quiz #2 Version A- Prof. Futrelle

Quiz #2 - Thursday 14 August - Closed book/notes


Be sure to write complete information in your blue exam book: Name, course, date, exam version A.

A Lamp

Your problem involves the State Design Pattern. The subject is a lamp that can have three states, Off, Dim and Bright.

Question 1 (75 points): The following figure shows the state transitions of a Lamp:

Part A. First write the code for the abstract class LampState.
It contains a String, status.
It contains an abstract click() which should be declared, not defined, and which returns a LampState.
It contains the void showStatus() which prints the status string.

Part B. Write the code for the class LampDim which extends LampState.
The no-arg constructor sets status to "Dim".
The overridden click() returns the BRIGHT field of AllLampStates.
(Note that there are two other similar classes, LampOff and LampBright, which you are not being asked to write.)

Part C. Write the code for the interface AllLampStates.
It contains a public static final LampState field, DIM, which is initialized to an instance of LampDim.
It contains a similar field OFF, initialized to an instance of LampOff and a field BRIGHT to an instance of LampBright.

Part D. Write the code for the class Lamp.
It contains a field state, of type LampState.
The no-arg constructor sets state to the OFF field of AllLampStates and then calls showStatus() on state.
The click() method first updates state to be the return value of state.click() and then calls showStatus on state.

Part E. Finally, write the code for the class LampTest.
main() in this class creates a instance of Lamp assigned to a field lamp.
It then calls click() on lamp six times.

Part F. What is the output of a run of LampTest? Explain your reasoning in arriving at your answer.

 

Question 2 (25 points): Draw the static class diagram for just LampState and the three classes LampOff, LampDim and LampBright.

 

EXTRA CREDIT (15 points): Draw the static class diagram including the four classes in Question 2 plus the remaining ones: Lamp, LampTest and the interface AllLampStates.