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

2013/3/31

Illegal start of expression

blueberry blueberry

2013/3/31

#
Hello everyone! I was hoping someone could assist me with this error that I am receiving every time I try to compile. The assignment stated to write the source code for Velocity into the actor class Velocity. It made it clear that it must be entered exactly the way it is written. I did that however; I keep getting an error no matter which way I try to enter the code. Could someone help me with this matter. Here's a copy of the source code that's having the issue. Thanks a bunch:-)! /** * Write a description of class Velocity here. * * @author Jacinta Wilson * @version March 30, 2013 */ public class Velocity extends Actor { /** * Act - do whatever the Velocity wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { } public Velocity() { // instance variables private int xSpeed = 0; this is where the error "illegal start of expression" is located private int ySpeed = 0;
JetLennit JetLennit

2013/3/31

#
private int xSpeed = 0; this is where the error "illegal start of expression" is located private int ySpeed = 0; should be private int xSpeed = (0); and private int ySpeed = (0); (i think)
Phytrix Phytrix

2013/3/31

#
What error message were you getting?
danpost danpost

2013/3/31

#
The instance variables need to be moved outside of any methods (but still within the class code).
public class Velocity extends Actor
{
    private int xSpeed = 0; 
    private int ySpeed = 0;
    // other instance fields you may have

    public Velocity()  
    {
        // constructor code, if any
    }

    // any methods you may have
}
danpost danpost

2013/3/31

#
I noticed that you have this class extending Actor. If there is no image that this object is to display (if you are not using the World class method 'addObject' on instances of this class), then it should probably not extend anything. For an example of what I am talking about, see the Scroll class of my Infinite Fixed Side-Scrolling SuperWorld scenario. It has three 'hidden' classes that do not extend anything at the end of the Scroll class code. They could have been written as individual classes, but are better as they are for this scenario.
blueberry blueberry

2013/3/31

#
Thanks so much for replying to my question. I was able to fix the issue with some patience and careful scrutiny of the code but, now I have another problem ( I know, it's not good). I am getting an error message that says "cannot find symbol variable x". I understand that it needs to be declared but I am bit confused on how to do this in this situation. I truly appreciate any feedback you all can provide. I am going to post the whole source code so the issue can be easier understood, thanks soooo much:-)! Write a description of class Velocity here. * * @author Jacinta Wilson * @version March 30, 2013 */ public class Velocity { // instance variables private int xSpeed = 0; private int ySpeed = 0; /** * Constructor for objects of class Velocity * */ public Velocity(int initXSpeed, int initYSpeed) { xSpeed = initXSpeed; ySpeed = initYSpeed; } /** * Get speed in the X direction * * @return the speed in X direction */ public int getXSpeed() { return xSpeed; } /** * Get speed in the Y direction * * @return the speed in Y direction */ public int getYSpeed() { return ySpeed; } /** * reverseX - reverse the speed in X direction */ public void reverseX() { xSpeed = -xSpeed; } /** * reverseY - reverse the speed in X direction */ public void reverseY() { ySpeed = -ySpeed; } /** * Constructor for objects of class Velocity */ public Velocity() { } /** * An example of a method - replace this comment with your own * * @param y a sample parameter for a method * @return the sum of x and y */ public int sampleMethod(int y) { // put your code here return x + y; this is where the error "cannot find symbol - variable x" is located } }
danpost danpost

2013/3/31

#
Remove the following code from the class (the last part)
    /**
     * Constructor for objects of class Velocity
     */
    public Velocity()
    {
    }

    /**
     * An example of a method - replace this comment with your own
     * 
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y 
     */
    public int sampleMethod(int y)
        
    {
        // put your code here
        return x + y;  this is where the error "cannot find symbol - variable x" is located
    }
blueberry blueberry

2013/3/31

#
Thank you so much Danpost!! I didn't realize that the solution was as simple as delete it! When it is time to do the addition of parameter, I will figure it out then. Thanks again!!
You need to login to post a reply.