Eclipse SDK v3.2.1 is rejecting my public void init method.
I'm using the following imports:
import acm.graphics.*;
import acm.program.*;
import acm.util.*;
My program has a run() method and an init() method but the init() is causing these errors
- overrides acm.program.Program.init
- Cannot override the final method from Program
Note, this is not a stand-alone application yet. Just running it from the Eclipse editor.
Apparently there is a an init method in the acm.program library. How do I implement my own initization method without attempting to override the acm.program built-in one? I've tried making my init method private with private void init but then I get:
- Cannot reduce the visibility of the inherited method from Program
- Cannot override the final method from Program
Here is the code so far. The error is with the init().
public class Hangman extends GraphicsProgram {
//CONSTANTS
private static int NUMBER_OF_INCORRECT_GUESSES = 8;
//Initialize the program
public void init() { //this gives compiler a problem
HangmanCanvas canvas = new HangmanCanvas();
add(canvas);
}
public void run() {
/* When the user plays Hangman, the computer first selects a secret word at random from a list built into the program. The program then prints out a row of dashes—one for each letter in the secret word—and asks the user to guess a letter. If the user guesses a letter that is in the word, the word is redisplayed with all instances of that letter shown in the correct positions, along with any letters correctly guessed on previous turns. If the letter does not appear in the word, the user is charged with an incorrect guess. The user keeps guessing letters until either (1) the user has correctly guessed all the letters in the word or (2) the user has made eight incorrect guesses. */
HangmanLexicon lexicon = new HangmanLexicon();
RandomGenerator rgen = new RandomGenerator();
int wordIndex = rgen.nextInt(0, lexicon.getWordCount()-1);
while (true) { //allows multi-play
int play = 0;
String answer = readLine ("Want to play?");
if(answer.equals("Y") || answer.equals("y") || answer.equals("yes") || answer.equals("Yes")) {play = 1;}
if(play == 0) {break;}
//Initialize some stuff
//get new random word
secretWord = lexicon.getWord(rgen.nextInt(0,wordIndex));
println("Welcome to Hangman.");
secretWord = secretWord.toUpperCase(); // convert secret word to upper case
int length = secretWord.length();
//reset game variables
String guess = "";
//clear the canvas
canvas.reset();
//reset the wrong answer count
wrong = 0;
// build the blank status word
currentWord = ""; //reset the word for multi-play
// build the dashes in status word as long as the secret word.
for (int i = 0; i < length; i++) {
currentWord += "-";
}
println("The word looks like this " + currentWord);
while (wrong<NUMBER_OF_INCORRECT_GUESSES && !currentWord.equals(secretWord)) {
guess = ".";
char g = guess.charAt(0);
while (!Character.isLetter(g)){ //if input is not a character, keep asking
guess = readLine("Guess a letter: ");
guess = guess.toUpperCase();
g = guess.charAt(0);
if (!Character.isLetter(g)){println("Your guess is not a single letter. Guess again: ");}
}
if(secretWord.indexOf(guess) < 0) {/*if guess is not in the secret word, increment wrong answer count and print message
to that effect. */
wrong++;
println("Threre are no " + guess + "\'s in the word.");
println("You have " + (NUMBER_OF_INCORRECT_GUESSES - wrong) + " guesses left.");
}
else {
println("That guess is correct.");
currentWord = wordBuild(guess);
if (currentWord.equals(secretWord)) { //if win print win but don't bother with the update to display
println("You win! You guessed the word: " + secretWord);}
else { println("The word now looks like this " + currentWord); //print the updated dash word
println("You have " + (NUMBER_OF_INCORRECT_GUESSES - wrong) + " guesses left.");
}
}
}
if(!currentWord.equals(secretWord)) {println("You lose.");} //out of guesses and word is not good.
}
}
//Build and/or update the dash word ------ displayed
public String wordBuild(String guess) {
String dashWord = "";
for (int i = 0; i < secretWord.length(); i++) {
if(secretWord.charAt(i) == guess.charAt(0)) {
dashWord = dashWord + guess;
}
else {dashWord = dashWord + currentWord.charAt(i);
}
}
return dashWord;
}
//INSTANCE VARS
int wrong = 0;
String currentWord = "";
String secretWord = "";
private HangmanCanvas canvas;
} //end of class