COMP 212 Lab 4: State Pattern

This tutorial covers two applications of the state pattern as preparation for the Hangman project.


"Smart" Words and Characters

Create a directory for this lab, and copy the files ~comp212/public_html/01-spring/labs/04/*.java. These are the Java stub code for the design of a "smart word" given by the following UML diagram.

SmartWord.png (23143 bytes)

In the above diagram, AWord is an abstract class to represent the input word in the Hangman game. An AWord can be empty or non-empty:

Each character in a non-empty AWord can be hidden or visible:

See the provided AWord.java for its definition.

The characters in a concrete non-empty AWord are "smart" and know how to compare themselves with other characters. They are implemented using the state pattern.

See the provided WordChar.java for its definition.

Exercises:

  1. Write the code for the Hidden class.
  2. Write the code for the Visible class.
  3. Write a test or two for WordChar and its derived classes.
  4. Write the code for the EmptyWord class.
  5. Write the code for the NonEmptyWord class.
  6. Write a test or two for AWord and its derived classes.

Your labbies will give you guidance, but will not help you write the code. These exercises constitute a non-trivial part of the current Hangman project.