Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
150 views
Welcome To Ask or Share your Answers For Others

1 Answer

I suppose you are taking Stanford CS106A course and that is causing the init final issue. The problem is the Karel.jar library u must have imported during the first few lectures. I suppose that library has init() method as final and that is the root cause. So the simple solution is to remove Karel.jar from the reference library as :

  1. In package explorer choose Reference Libraries.
  2. Right click on Karel.jar.
  3. Chose Build Path from the context menu.
  4. Chose Remove from Build Path.

But in case u need to do Karel Programming again using Eclipse you'll have to import the Reference Library again from the Assignment 1 Package by follwing a similar course but choosing to import the library. To make sure you keep on using the acm methods, please make sure to import acm.jar by downloading it from jtf.acm.org. To add libraries you make use Eclipse Documentation or just Google it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...