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:
- To practice with the String class and methods.
- To practice simple looping constructs.
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.
- SecretWordApplet provides the applet interface. To save
time and help focus on designing and implementing the
SecretWord class, you are given the applet interface. Add the applet to your Codewarrier
project. The applet's actionPerformed() method uses methods
of the SecretWord class.
- The class, SecretWord.java, will be
responsible for managing the secret word. You must design and
implement the following elements for the SecretWord class:
- A private String variable named secretWord
initialized to any word. This string will contain the secret word
against which the user's guesses will be compared.
- A private String variable named
displayedWord. This is the string that will be displayed in
the applet interface.
- A public method named getDisplayedWord() will return
the displayedWord. For example, if the game just started and
the secret word is "hello", this method would return "*****".
- A public method named getSecretWord() returns the
secretWord.
- The replaceChars() method takes two parameters, a
String and a char. It replaces every character in
the string with the char and returns the result. For example,
replaceChars("hello", '*') will return "*****".
- The SecretWord() constructor should assign a string
literal to the secretWord variable, and it should invoke the
replaceChars() method to replace all the letters in
secretWord with asterisks and assign the result to
displayedWord.
- The replaceChars(String s1, String s2, char ch) method
replaces in s2 every occurrence of ch in s1. For example,
replaceChars("hello", "*e***", 'l') would return "*ell*".
- The makeGuess(char ch) method tests whether its char
parameter is contained in secretWord. If so it updates the
displayedWord, revealing the position(s) of the guessed
letter, and returns true; otherwise it returns false. For example, if
the secret word is "hello", makeGuess('e') would return true and
would update the displayed word to "*e***".
Implementation: Step-wise Refinement
- Draw a UML diagram for the SecretWordApplet and
SecretWord classes and have it checked. (REQUIRED)
- 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.
- 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.
- Implement and test the replaceChars(String, char) method.
- Implement and test the makeGuess() method.
- 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!