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

2013/1/28

Correct this error for me

avocasso avocasso

2013/1/28

#
Hey Guys, something different form your discussion. Can someone help me here?I have this: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MyDog here. * * @author (your name) * @version (a version number or a date) */ public class MyDog extends Dog { private String image = "dog.png"; private int energy = 100; private boolean amSleepy = false; private int countSteps; /** * Act - do whatever the MyDog wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { //All code relating to the player-controlled dog goes here walkMyDog(); } } /** * This dog gets sleepy when its energy hits 20 or lower. */ public boolean isSleepy(){ if ( energy <= 20 ) { amSleepy = true; } else { amSleepy = false; } return amSleepy; } Now every time I try to compile this, I have this error displayed: ' class, interface, or enum expected ' with emphasis in the source code highlighting this whole method in yellow: ' public boolean isSleepy() ' , also marking a redline before ' |boolean '. What am I doing wrong, please tell me?
vonmeth vonmeth

2013/1/28

#
If that is the entirety of your code, then it is probably because you have a stray bracket:
    public void act() 
    {
       //All code relating to the player-controlled dog goes here
       walkMyDog();
    }
   
    } // remove this bracket
    
    /**
     * This dog gets sleepy when its energy hits 20 or lower.
     */
danpost danpost

2013/1/28

#
Instead of removing it, move it to the end of the class code.
avocasso avocasso

2013/1/28

#
So this is the entirety of my code: Even though I removed } or move it to the end of the class code, I still have the same error : import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MyDog here. * * @author (your name) * @version (a version number or a date) */ public class MyDog extends Dog { private String image = "dog.png"; private int energy = 100; private boolean amSleepy = false; private int countSteps; /** * Act - do whatever the MyDog wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { //All code relating to the player-controlled dog goes here walkMyDog(); } { checkKeypress(); amSleepy(); countSteps = countSteps + 1; if (countSteps == 30) { energy--; } } } /** * This dog gets sleepy when its energy hits 20 or lower. */ public boolean isSleepy() { if ( energy <= 20 ) { amSleepy = true; } else { amSleepy = false; } return amSleepy; } /** /** * Check whether a key has been pressed and if so move the dog accordingly. * Animate dog by switching between images. * Dog moves north, south, east, west, northeast, northwest, southeast, and southwest. */ public void checkKeypress() { if (Greenfoot.isKeyDown("up")) { setRotation(90); move(-5); setImage("dog-walk.png"); wait(2); setImage("dog-walk-2.png"); } if (Greenfoot.isKeyDown("down")) { setRotation(-90); move(-5); setImage("dog-walk.png"); wait(2); setImage("dog-walk-2.png"); } if (Greenfoot.isKeyDown("left")) { setRotation(0); move(-5); setImage("dog-walk.png"); wait(2); setImage("dog-walk-2.png"); } if (Greenfoot.isKeyDown("right")) { setRotation(0); move(5); setImage("dog-walk-right.png"); wait(2); setImage("dog-walk-right-2.png"); } } /** * Return true if we can see another dog. * Return false if there is no such object here. */ public boolean canSee(Class clss) { Actor Mydog = getOneObjectAtOffset(0, 0, clss); return Mydog != null; } /** * Look for other dogs and greet them. */ public void lookForDog() { if (canSee(Dog.class)) { Greenfoot.playSound("chasdog.wav"); setImage("dog-bark-1.png"); setImage("dog-bark-2.png"); setImage("dog.png"); } }
danpost danpost

2013/1/28

#
import greenfoot.*;	

public class MyDog extends Dog
{
    private String image = "dog.png";
    private int energy = 100;
    private boolean amSleepy = false;
    private int countSteps;
         
    public void act() 
    {
       //All code relating to the player-controlled dog goes here
       walkMyDog();
    }
    
    {
       checkKeypress();
       
        amSleepy();
        countSteps = countSteps + 1;
            if (countSteps == 30)
            { 
            energy--;
            }
    }
}

     /**
     * This dog gets sleepy when its energy hits 20 or lower.
     */
        public boolean isSleepy()
        {
            if ( energy <= 20 )
            {
                amSleepy = true;
            }
            else
            {
                amSleepy = false;
            }
        return amSleepy;
    }

    /** 
    * Check whether a key has been pressed and if so move the dog accordingly. 
    * Animate dog by switching between images. 
    * Dog moves north, south, east, west, northeast, northwest, southeast, and southwest. 
    */ 
   public void checkKeypress() 
   { 
        if (Greenfoot.isKeyDown("up"))
        {
            setRotation(90);
            move(-5);
            setImage("dog-walk.png"); 
            wait(2); 
            setImage("dog-walk-2.png");
        } 
        if (Greenfoot.isKeyDown("down")) 
        {
            setRotation(-90); 
            move(-5); 
            setImage("dog-walk.png"); 
            wait(2);
            setImage("dog-walk-2.png");
        }
        if (Greenfoot.isKeyDown("left"))
        {
            setRotation(0);
            move(-5);
            setImage("dog-walk.png");
            wait(2);
            setImage("dog-walk-2.png");
        }
        if (Greenfoot.isKeyDown("right"))
        {
            setRotation(0);
            move(5);
            setImage("dog-walk-right.png");
            wait(2);
            setImage("dog-walk-right-2.png");
        }
    }
   
    /** 
     * Return true if we can see another dog. 
     * Return false if there is no such object here. 
     */ 
    public boolean canSee(Class clss) 
    { 
        Actor Mydog = getOneObjectAtOffset(0, 0, clss); 
        return Mydog != null; 
    } 
     
     /** 
     * Look for other dogs and greet them. 
     */ 
    public void lookForDog() 
    { 
        if (canSee(Dog.class)) 
        { 
            Greenfoot.playSound("chasdog.wav"); 
            setImage("dog-bark-1.png"); 
            setImage("dog-bark-2.png"); 
            setImage("dog.png"); 
        }  
    }
Line 14 ends the act method. Line 16 starts an unidentified block. Line 26 appears to close out the class code. So, line 31 begins code that is outside the class and confuses the compiler. So, line 31 needs to be cut and pasted at the end of the class code; and the code around line 16 needs to be fixed. As I could not say what is supposed to be there or what needs changed, I cannot help in that matter. You would be wise to press Ctrl-Shift-I to properly indent your code so you can see how the block brackets match up. Also, when posting code on the site, please prefix it with and postfix it with .
You need to login to post a reply.