This site requires JavaScript, please enable it in your browser!
Greenfoot back
super_noob
super_noob wrote ...

2012/12/12

cannot find symbol

super_noob super_noob

2012/12/12

#
public void lookForWorm()
    {
        if ( canSee(Worm.class) ) 
        {
            if (Greenfoot.getRandomNumber(4) < 0) 
            {
             eat(Worm.class);
             wormsEaten = wormsEaten + 1;
            }
            
            if ( wormsEaten == 4) 
            {
                Greenfoot.playSound("ouch.wav");
                Greenfoot.stop();
            
        }

    }
It's saying it can't fand the variable wormsEaten. Any ideas?
danpost danpost

2012/12/12

#
Please post the whole class. Thanks.
super_noob super_noob

2012/12/12

#
public class Lobster extends Animal
{
    /**
     * Do whatever lobsters do.
     */
    public void act()
    {
        turnAtEdge();
        randomTurn();
        move();
        lookForCrab();
        lookForWorm();
    }

    /**
     * Check whether we are at the edge of the world. If we are, turn a bit.
     * If not, do nothing.
     */
    public void turnAtEdge()
    {
        if ( atWorldEdge() ) 
        {
            turn(17);
        }
    }

    /**
     * Randomly decide to turn from the current direction, or not. If we turn
     * turn a bit left or right by a random degree.
     */
    public void randomTurn()
    {
        if (Greenfoot.getRandomNumber(100) > 90) {
            turn(Greenfoot.getRandomNumber(90)-45);
        }
    }

    /**
     * Try to pinch a crab. That is: check whether we have stumbled upon a crab.
     * If we have, remove the crab from the game, and stop the program running.
     */
    public void lookForWorm()
    {
        if ( canSee(Worm.class) ) 
        {
            if (Greenfoot.getRandomNumber(4) < 0) 
            {
             eat(Worm.class);
             wormsEaten = wormsEaten + 1;
            }
            
            if ( wormsEaten == 4) 
            {
                Greenfoot.playSound("ouch.wav");
                Greenfoot.stop();
            
        }

    }
}
    public void lookForCrab()
    {
        if ( canSee(Crab.class) ) 
            {
            eat(Crab.class);
            }
}
}
danpost danpost

2012/12/12

#
Where do you declare the instance field 'wormsEaten' in the class?
super_noob super_noob

2012/12/12

#
I suppose I don't. How would I go about doing so?
super_noob super_noob

2012/12/12

#
Never mind, I realized that I need to declare it under where the class extends to animal. Thanks for helping me figure this out!
danpost danpost

2012/12/12

#
Insert at line 3 of the code provided above the following line:
private int wormsEaten;
By declaring a field within a class but outside any method like this, you are giving each instance created by that class a seperate field with that name to use. Of course, if you only create one instance of the class, there will only be one field with that name; but, it belongs to that object created (not to the class itself). EDIT: your welcome.
You need to login to post a reply.