Chapter 7: Strings and String Processing

Ch 7 Lab: Secret Word

Authors: Ralph Morelli and Bronzell Dinkins
Trinity College, Hartford, CT

Brief description

This lab emphasizes use of the String class and string processing algorithms. It provides good practice with coding loop algorithms. It uses a provided applet interface. Successful completion of this lab could be used as a preliminary assignment to completing Exercise 7.15 at the end of Chapter 7 in Java, Java, Java 3E.

Objectives

The objectives of this lab are:

Problem Statement: Secret Word

Write a class that manages the String processing for an applet that lets the user make repeated guesses at the letters contained in a secret word. The secret word is shown in masked form. For example, if the secret word is hello, it would be displayed as *****. Each time the user makes a guess, the applet reports whether the guess is correct or not and displays the updated version of the secret word. For example, if on the first guess the user guesses 'e', the secret word would be displayed as *e***.

GUI Specifications

As shown in the demo, this program will have a typical applet user interface.

Design Specifications

You need two Java classes for this project.

Implementation: Step-wise Refinement

  1. Draw a UML diagram for the SecretWordApplet and SecretWord classes and have it checked. (REQUIRED)

  2. A good first step for this problem is to create a project containing the SecretWordApplet and a stub version of the SecretWord class. The stub version must contain stub definitions of all of the SecretWord methods that are called from SecretWordApplet.

  3. Study the algorithm used in the SecretWordApplet. Initially the applet displays the masked secret word in its text area. Each time the user types a return in the input text field, the applet inputs the user's guess and calls the SecretWord.makeGuess() method. The applet then displays an appropriate message and the updated secret word.

  4. Implement and test the replaceChars(String, char) method.

  5. Implement and test the makeGuess() method.

  6. Implement and test the replaceChars(String, String, char) method. This method is the most challenging one. Before starting to work on this method, you should develop the algorithm and get it okayed by the instructor or a TA.

Program Documentation

Your Java source code files should be documented according to the specifications given in documentation guidelines.

Hand In

At the end of lab hand in your properly documented source code for SecretWord.java.

You're done. Good work!