Create your own Droid


#1

Hello all,

I'm currently having calculation errors in my coding. In the final step of the project it presents the output of what should be shown however I have something else.

public class Droid {
int batterylevel;

public Droid(){
int batterylevel = 100;
}

public void activate (){
System.out.println("Activated. How can i help you?");
batterylevel = batterylevel - 5;
System.out.println("Battery level is: " + batterylevel + " percent.");
}

public void chargeBattery(int hours){
System.out.println("Droid charging...");
batterylevel = batterylevel + hours;
if (batterylevel > 100)
{ batterylevel = 100;}
else
{System.out.println("batterylevel is " + batterylevel + " %");
}
}
public int checkbatterylevel() {
System.out.println("batterylevel is" + batterylevel + "%");
return batterylevel;
}
public void hover (int feet){
if ( feet > 2)
{System.out.println("Error! I cant hover above 2 feet");}
else {
System.out.println("hovering...");
batterylevel = batterylevel - 20;
System.out.println( "batterylevel is " + batterylevel + " %");}
}
public static void main(String[] args){
Droid ray = new Droid();
ray.activate();
ray.chargeBattery(5);
ray.hover(2);
}
}

my outcomes are
Activated. How can i help you?
Battery level is: -5 percent.
Droid charging...
batterylevel is 0 %
hovering...
batterylevel is -20 %

when the actually outcome should be
Activated. How can i help you?
Battery level is: 95 percent.
Droid charging...
batterylevel is 100 %
hovering...
batterylevel is 80 %

I know alternative ways to figure how to correct the problem but I would like to do it the Codeacademy way. Sorry for long post and thank you for your feedback.


#3

Ok this was funny.. The reason you didnt get your anwser was since batteryLevel was defined in your constructor and was not avaibale in other methods. So if you change the batterylevel where you actually initalize the variable to 100 it will work. Like this:

Change this:

public class Droid {
int batterylevel;

public Droid(){
int batterylevel = 100;
}

To this:

public class Droid {
int batterylevel = 100;

public Droid(){
}

#4

Sorry and thank you, amanuel2. However, in the project steps it asked to not set the instance variable to anything and to declare battery level = 100; in the constructor.

"Create a Droid constructor. Inside of the Droid constructor, set batteryLevel equal to 100. Now every time a Droid is created, its battery level will be at 100 percent."


#6

Haha! Found another solution. You could just make it global by making it public.So change this:

public Droid(){
int batterylevel = 100;
}

to this:

public Droid(){
public int batterylevel = 100;
}

#7

you're coding using Eclipse correct? if you are i tried that way and it's not debugging. But still thank you for all your help amanuel2.


#8

Huh? Show some type of screenshot so i can understand?(You can take a screen shot by ALT+PRINT-SCREEN, or in Mac COMMAND+PRINT-SCREEN).


#9

Declaring batterylevel = 100; as public didn't work. It's an illegal modifier for a parameter for batterylevel; only final is permitted. i'm confused as to how codeacadmey is asking battery level to be declared in constructor when it obviously doesn't work.


#10

I must go but really thank you amanuel2. maybe ill catch you on forums another time :stuck_out_tongue:


#11

are you sure it didnt work, it worked for me in codecademy....


#12

Hey amanuel2, I finally fixed my problem the way how codeademy wanted it done. Instead declaring batterylevel as a integer, i just needed to declare it as just ~batterylevel=100; ~ and the output will be corrected.


#13

Wow how come i never thought of that :disappointed:


#14

thing about coding is that there are so many ways in figuring out how to get an answer >.>