Appendix B. Solutions
State (Chapter 22)......................................................................................................................................
SOLUTION 22.1....................................................................................................................................
As the state machine shows, when the door is open, a click will take the door to
the StayOpen state, and a second click will start the door closing.
SOLUTION 22.2....................................................................................................................................
Your code should look something like:
public void complete()
{
if (state == OPENING)
{
setState(OPEN);
}
else if (state == CLOSING)
{
setState(CLOSED);
}
}
public void timeout()
{
setState(CLOSING);
}
SOLUTION 22.3....................................................................................................................................
The code for status() relies on the state's class name and will report, for example,
the status of an open door as "DoorOpen", instead of "Open". You can, of course, trim off
the "Door" prefix, if you like.
SOLUTION 22.4....................................................................................................................................
Your code should look something like:
package com.oozinoz.carousel;
public class DoorClosing extends DoorState
{
public DoorClosing(Door_2 door)
{
super(door);
}
public void click()
{
door.setState(door.OPENING);
}