[Texas PLT logo]

COMP 202: Principles of Object-Oriented Programming II

  Lab 01  

Introduction

This tutorial covers:


Programming project #1 asks you to write a GUI program to play hangman.  One of the key data structures for this program is a linear recursive structure similar to that of a list to store the characters of a word to be guessed.  A detailed description of this data structure is given on the wordlist package page of the assignment. Go to this link and follow the directions that are there. By completing the exercises in this link, you will have completed a non-trivial part of the programming project.

Your TAs will give you guidance but will not help you write the code.   

During this lab, a discussion of encapsulation came up. There are different ways to implement the concrete classes for IEmptyWord and INEWord, and different places to put AWordState and its two concrete subclasses, the one for the visible state and the one for the invisible state.

Here is a UML class diagram depicting a solution that places the implementations of IEmptyWord and INEWord inside the word factory, and the AWordState class inside the concrete non-empty word class. It furthermore uses an anonymous inner class for the invisible state, and a second anonymous inner class, nested inside the first, for the visible state.

Whenever possible, you should follow the rules of encapsulation: If something does not need to be accessed from the outside, it should not be accessible. If something does not need to have a name, because it is used in only one place, it should be anonymous.

Unfortunately, Java's language design does not allow you to put code for one class into separate source files, and nested classes must be defined in the same source file as the enclosing top-level class. In some cases, following the above rules can lead to code that is hard to read, and you need to use your own judgment to decide if encapsulation is more important than readability. Note that the concrete implementations of IEmptyWord and INEWord could have been implemented as anonymous inner classes as well!

  Lab 01  

URL: http://www.clear.rice.edu/comp202/08-fall/labs/hangman1/index.shtml
Copyright © 2008-2010 Mathias Ricken and Stephen Wong