heres whats required of me:
Write a Java class, Hangman, that can be used to play the game of Hangman. See a sample run of this game in
the file attached to this assignment named, GameOutput.pdf. Output from your application should be very
similar. Here is what your Hangman class should contain:
Five Attributes (instance variables):
• the secret word, a private String variable
• the disguised word, a private String variable, 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, then the disguised word is displayed as ab?a?a?ab?a.
• the letters, in alphabetical order, that have been incorrectly guessed; a private String variable
• the number of guesses made so far, an int variable
• the number of incorrect guesses made so far, an int variable
Five Methods:
• initialize: a public void method that takes one String parameter as the secret word and initializes the
secret word and other instance variables to begin playing a round of Hangman.
• makeGuess: a private void method that accepts a parameter of type, char, and responds appropriately
to the guess that character, c, is in the secret word. This involves counting the guess, counting
incorrect guesses, recalculating the disguised word if the guess is correct, adding the character to the
letters guessed incorrectly (alphabetically) if the guess is incorrect, etc.
• accessor methods for the secret word and the disguised word: getSecretWord and getDisguisedWord
• boolean‐returning method, isFound, returns true if the secret word has been guessed
You may add other methods and/or instance variables to assist in implementing the game of Hangman. Be
sure that the output of you game looks much like the same output in GameOutput.pdf, except that your
application should work for any word chosen as the secret word. Your Hangman class should use a main
method as follows in order to play the game:
public static void main(String[] args) {
Hangman game = new Hangman();
game.initialize("Happiness");
System.out.println("Lets play a round of hangman.");
game.playGame();
}
Note that this application does not pick a truly secret word since the programmer chooses the word ahead of
time. This problem can easily be solved once we know how to read files. For now, having the programmer
choose the word this way will be fine, just make sure your code works for many different words.
When comparing your output to the sample, be sure to note the prompts made by the program, the format of
the disguised word, the display of the number of guesses, the number of incorrect guesses, the letters guessed
incorrectly (given in alphabetical order), the error messages for entering more than one letter and for entering
letters already guessed (both correct and incorrect), and the congratulations remark.
heres an example of what the output is supposed to look like
Lets play a round of hangman.
The disguised word is <?????????>
Guess a letter: e
Guesses made 1 with 0 wrong ()
The disguised word is <??????e??>
Guess a letter: o
Guesses made 2 with 1 wrong (o)
The disguised word is <??????e??>
Guess a letter: s
Guesses made 3 with 1 wrong (o)
heres my code:
public class Hangman
{
public static void main(String[] args)
{
char letter;
String s1;
private String secret;
private String disguisedWord;
private String guesses;
int count;
int countwrong;
boolean isFound;
/* main class **/
Scanner keyboard = new Scanner(System.in);
Hangman game = new Hangman();
game.initialize("happiness");
System.out.println("lets play a round of hangman.");
game.playGame();
}
/* intialize class = changes secret word to all question marks, sets boolean to false so game loops **/
public void initialize (String s1)
{
secret = s1;
diguisedWord = "";
for (int count=0; count < secret.length(); count++)
diguisedWord =+ "?";
isFound = false;
}
/* executes game: starts loop, guesses characters and runs method makeGuess **/
public void playGame()
{
System.out.println(" the disguised word is " + disguisedWord);
System.out.println(" guess a letter");
makeGuess();
}
private void makeGuess (char letter)
{
letter = keyboard.nextChar();
letter = guess.charAt(0);
guesses += letter;
if (secret.indexOf(letter) < 0)
{
countwrong++;
System.out.println("Guesses made " + count + " with " + countwrong + " wrong" + guesses);
}
else // letter is correct(process letter.)
count++;
getDisguisedWord();
{
if (secret.indexOf(letter) > 0)
{
disguisedWord.indexOf(letter) = letter;
if (disguisedWord.indexOf('?')<0)
{
isFound=true;
System.out.println(" congratulations, you found the secret word:");
return secret;
}
}
return diguisedWord;
}
}
}
}
}
heres the errors im getting while compiling:
----jGRASP exec: javac -g Hangman.java
Hangman.java:29: error: illegal start of expression
private String secret;
^
Hangman.java:30: error: illegal start of expression
private String disguisedWord;
^
Hangman.java:31: error: illegal start of expression
private String guesses;
^
Hangman.java:88: error: class, interface, or enum expected
}
^
4 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
and when i attempt to run, i get :
The following file could not be parsed
"c:documents and settings\user\my documents\hangman.java"
proceeding before correcting is not recommended
then
no main methods, applets, or MIDlets found in file;
This post was edited by ouch617 on Mar 25 2013 09:13am