Consider a class that could be used to play a game of hangman. The class has the following attributes:

• The secret word.
• The disguised word, in which each unknown letter in the secret word is replaced with a question mark (?). For example, if the secret word is abracadabra and the letters a, b, and e have been guessed, the disguised word would be ab?a?a?ab?a.
• The number of guesses made.
• The number of incorrect guesses.
It will have the following methods:
• makeGuess(c) guesses that character c is in the word.
• getDisguisedWord returns a string containing correctly guessed letters in
their correct positions and unknown letters replaced with ?.
• getSecretWord returns the secret word.
• getGuessCount returns the number of guesses made.
• isFound returns true if the hidden word has been discovered.
a. Write a method heading for each method.
b. Write preconditions and postconditions for each method.
c. Write some Java statements that test the class.
d. Implement the class.
e. List any additional methods and attributes needed in the implementation that were not listed in the original design. List any other changes made to the original design.
f. Write a program that implements the game of hangman using the class you wrote for Part d.

This project considers the development of a hangman game. First, we focus on domain methods for the class that encapsulates the operations required by such a game. This class is tested and then is completed by adding in interface methods. To make the code simpler, the class converts all input to lower case. The code takes a particularly straightforward approach to creating the disguised word by just replacing every possible character by ‘?’. This could be accomplished by using Scanner with patterns but would require a discussion of their use that may not be appropriate for all classes.


a) ```
public void makeGuess(Character c)
public String getDisguisedWord()
public String getSecretWord()
public int getGuessCount()
public boolean isFound()
```

b)
public void makeGuess(Character c)
Precondition: The character is one of the 26 alpha characters.
Postcondition: The number of guesses and incorrect guesses is updated, the disguised word is updated.

public String getDisguisedWord()
Precondition: none.
Postcondition: The disguised string was returned.

public String getSecretWord()
Precondition: none.
Postcondition: The secret word was returned.

public int getGuessCount()
Precondition: none.
Postcondition: The number of correct guesses was returned.

public boolean isFound()
Precondition: none.
Postcondition: True was returned if the disguised word has become the secret word.

d) We will add an attribute that will be the secret word with letters that have been guessed replaced with #.

We will add the methods
public void initialize(String word) {
Precondition: none.
Postcondition: All of the attributes were initialized. The secret word was initialized to the argument. The letters remaining attribute was set to the secret word. The disguised word was set to the secret word with all the letters replaced by question mark. The number of guesses made was set to zero. The number of incorrect guesses was set to zero.



public String createDisguisedWord(String word)
Precondition: none.
Postcondition: Returned a string of the same length as the argument, but with all the alpha characters replaced by ?.

public void playGame()
Precondition: The word has not already been guessed.
Postcondition: Letters were obtained one at a time until the secret word was guessed. The number of guesses and incorrect guesses where displayed.

See the code in Hangman.java.

Computer Science & Information Technology

You might also like to view...

In Excel, if comments are hiding text you want to see, you can move the comments box

Indicate whether the statement is true or false

Computer Science & Information Technology

Windows/File _______ is the most common Windows application used to move a file or folder

Fill in the blank(s) with correct word

Computer Science & Information Technology