COM1204 Summer Quiz #2 Version B - 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 B.

The Dog Sam

Your problem involves the State Design Pattern. The subject is a dog that can have three states, Lying, Sitting and Standing.

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

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

Part B. Write the code for the class DogSit which extends DogState.
The no-arg constructor sets status to "Sitting".
The overridden command() returns the STAND field of AllDogStates.
(Note that there are two other similar classes, DogLie and DogStand, which you are not being asked to write.)

Part C. Write the code for the interface AllDogStates.
It contains a public static final DogState field, SIT, which is initialized to an instance of DogSit.
It contains a similar field LIE, initialized to an instance of DogLie and a field STAND to an instance of DogStand.

Part D. Write the code for the class Dog.
It contains a field state, of type DogState.
The no-arg constructor sets state to the LIE field of AllDogStates and then calls showStatus() on state.
The command() method first updates state to be the return value of state.command() and then calls showStatus on state.

Part E. Finally, write the code for the class DogTest.
main() in this class creates a instance of Dog assigned to a field dog.
It then calls command() on dog six times.

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

 

Question 2 (25 points): Draw the static class diagram for just DogState and the three classes DogLie, DogSit and DogStand.

 

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