Consider a class that could be used to play a game of hangman. The class has the
ID: 3650803 • Letter: C
Question
Consider a class that could be used to play a game of hangman. The class has the following attributes:1.The secret word
2.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.
3.The number of guesses made.
4.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 repalced 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.
(Please explain as well)
Explanation / Answer
Please rate...
Program Hangman.java
=================================================
class Hangman
{
String secretWord;
String disguisedWord;
int numberOfGuesses;
int numberOfIncorrectGuesses;
Hangman(String s)
{
secretWord=s;
int i;
char ar[]=new char[s.length()];
for(i=0;i<s.length();i++)
{
ar[i]='?';
}
String str=new String(ar);
disguisedWord=str;
}
public void makeGuess(char c)
{
char ar[]=new char[secretWord.length()];
int i;
for(i=0;i<secretWord.length();i++)
{
ar[i]=disguisedWord.charAt(i);
}
for(i=0;i<secretWord.length();i++)
{
if(secretWord.charAt(i)==c)
{
ar[i]=c;
}
}
String str=new String(ar);
disguisedWord=str;
numberOfGuesses++;
}
public String getDisguisedWord()
{
return disguisedWord;
}
public String getSecretWord()
{
return secretWord;
}
public int getNumberOfGuesses()
{
return numberOfGuesses;
}
public boolean isFound()
{
if(secretWord.equalsIgnoreCase(disguisedWord))
return true;
else return false;
}
}
======================================================
Program HangmenTest.java
======================================================
import java.util.Scanner;
class HangmanTest
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
Hangman h=new Hangman("aabcxxyzertyui");
System.out.println("The disguised word is: "+h.getDisguisedWord());
while(!h.isFound())
{
System.out.print("Enter your guessed character: ");
String t=s.next();
char c=t.charAt(0);
System.out.println(c);
h.makeGuess(c);
System.out.println("Your guess: "+h.getDisguisedWord());
}
System.out.println("You guessed the word in "+h.getNumberOfGuesses()+" guesses");
}
}
=================================================
Sample output:
The disguised word is: ??????????????
Enter your guessed character: a
a
Your guess: aa????????????
Enter your guessed character: x
x
Your guess: aa??xx????????
Enter your guessed character: y
y
Your guess: aa??xxy????y??
Enter your guessed character: z
z
Your guess: aa??xxyz???y??
Enter your guessed character: e
e
Your guess: aa??xxyze??y??
Enter your guessed character: r
r
Your guess: aa??xxyzer?y??
Enter your guessed character: t
t
Your guess: aa??xxyzerty??
Enter your guessed character: u
u
Your guess: aa??xxyzertyu?
Enter your guessed character: i
i
Your guess: aa??xxyzertyui
Enter your guessed character: c
c
Your guess: aa?cxxyzertyui
Enter your guessed character: b
b
Your guess: aabcxxyzertyui
You guessed the word in 11 guesses
Process finished with exit code 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.