Code academy exercice

error
Droid.java:46: error: cannot find symbol
myDroid.hover(feet);
^
symbol: variable feet
location: class Droid
1 error

2 Likes

Haven’t done this exercise yet, but you have no variable called ‘feet’ in the main method. You must either create an int variable called feet, or pass in an integer for the function to use.

hi yes i have finished the exercise as indicated in the links the code is complete just the error is present i do not know what to do

Hmm, look at your main method:

public static void main (String[] args ){
    Droid myDroid = new Droid();
    myDroid.activate();
    myDroid.chargeBattery(5);
    myDroid.hover(feet); 
}

Java can’t figure out what value feet is referring to in the last line where you call myDroid.hover(feet). This is because there’s no variable in here called feet. Does that make sense?

1 Like

int feet is created l32 public void hover (int feet){

Ah I see. So you need to note the difference between those two functions.

First of all, functions do not share variables (unless they are nested, but here they are not). It’s because they all have different scopes, or their own “environments” where variables live. The environment for public static void main is not the same as public void hover, which is why you can’t use the feet from hover in main.

Instead, you need to create your own variable called feet in main that holds the value you want to use. Hopefully that makes sense :grimacing:.

This is about parameters and arguments.

when you declare a method:

public void hover (int feet)

you give the method a parameter (feet).

then when you call the method:

myDroid.hover(argument);

You need to provide an argument for the parameter. In this case, the arguments need to be a integer, so you can pass an integer directly:

myDroid.hover(6); 

or you can first declare a variable:

int hoverDistance = 6
myDroid.hover(hoverDistance); 

then Java will “assign” the argument(s) to the parameter(s)

furthermore, parameters do indeed have a local scope, so feet only exist within the hover method.

thanks for your reponse :slight_smile: