Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming for Java using Eclipse: Consider a class that could be used to play

ID: 3667152 • Letter: P

Question

Programming for Java using Eclipse: 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 when the disguise word would be ab?a?a?ab?a.

The number of guesses made

The number of incorrect guesses made

It will have the following methods:

madeGuess (c) guesses that character c is in the word

getDisguisedWord returns a string containing correctly guessed letters in their correct position and unknown letters replaced with a ?.

getSecretWord returns the secret word

getGuessCount returns the number of guesses made

isFound returns true if the hidden word has been discovered

1. Write a method heading for each method

2. Write preconditions and post conditions for each method

3. Write some java statements that test the class

4. Implement the class

5. 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.

6. Write a program that implements the game of hangman using the class you wrote for part 4.

Explanation / Answer

import java.util.Scanner;
public class Hangman
{
char letter;
String s1;
String s2;
private String secret;
private String disguisedWord;
private String charactersGuessed = "";
int count=0;
int countwrong=0;
boolean isFound;
Scanner keyboard = new Scanner(System.in);
public static void main( String[] args)
{
Hangman game = new Hangman();
game.initialize("happiness");
System.out.println("Lets play a round of hangman.");
game.playGame();
}
public void initialize(String s1)
{
secret = s1;
disguisedWord = "";
for (int count=0; count < secret.length(); count++)
{
disguisedWord += "?";
isFound = false;
}
}
public void playGame()
{
while (! isFound)
{
System.out.println("the disguised word is " + disguisedWord);
System.out.println("guess a letter");
makeGuess();
}
}
private void makeGuess()
{
s2 = keyboard.next();
letter = s2.charAt(0);
if (s2.compareTo(charactersGuessed)<0)
{
charactersGuessed = letter + charactersGuessed;
}
else
{
charactersGuessed = charactersGuessed + letter;
}
if (secret.indexOf(letter) < 0)
{
countwrong++;
System.out.println("Guesses made " + count + " with " + countwrong + " wrong and letters guessed:" + charactersGuessed);
}
else
{
count++;
System.out.println("Guesses made " + count + " with " + countwrong + " wrong and letters guessed:" + charactersGuessed);
replaceGuessedLetter(letter);
}
}
public void replaceGuessedLetter(char guessedLetter)
{
if (secret.indexOf(letter) >= 0)
{
String temp= disguisedWord;
disguisedWord= "";
for (int i=0; i<secret.length(); i++)
{
//System.out.println("debugged " + secret.charAt(i));
if (secret.charAt(i)== guessedLetter)
{
disguisedWord+= guessedLetter;
}
else
{  
disguisedWord+= temp.charAt(i);
}
}
if (disguisedWord.indexOf('?')<0)
{
isFound=true;
System.out.println(" congratulations, you found the secret word:");
System.out.println(secret);
}
}
}
}