Droid Challenge


#1

Hello again, I tried referring to the other Droid Challenge
link before starting this thread but didn't give me much help.
This is my first attempt at the Droid code & i'm getting multiple
errors. I'll start by posting the code.

Had trouble posting it in the preformatted text
so here is the gist https://gist.github.com/e6789aecc3b5e8cbfb82

If you need any more info, i'll be glad to post.


#2

Ok so, let me start out

First the BIG Problem is that you accidently closed your activate method way to quick as shown below:

public void activate() {
    System.out.println("Activated. How can I help you?");
  }
  
  batteryLevel = batteryLevel - 5;
  System.out.println("Battery level is " + batteryLevel + "Percent!");
}

Change that to this and your good:

public void activate() {
    System.out.println("Activated. How can I help you?");
  
  
  batteryLevel = batteryLevel - 5;
  System.out.println("Battery level is " + batteryLevel + "Percent!");
}

Secondly: Same thing but this time with return statement your code:

 public int checkBatteryLevel() {
     System.out.println("The Battery Level is " + batteryLevel + "Percent!");
   }
return batteryLevel;

Change it to:

   public int checkBatteryLevel() {
         System.out.println("The Battery Level is " + batteryLevel + "Percent!");
 return batteryLevel;       
}

Finally:

You called:

sample.activate;

Need to have parantheses around it!

sample.activate();

Hope that helped! If it didnt please reply saying so! And are you using Codecademy pro?


#3

Ok yeah that was it, actually was about 3
brackets misplaced in there. for some reason
I was thinking after every

System.out.println();

statement you closed it with a

}

Thanks again for answer.

Edit: Oh, & Yes @amanuel2 I am giving the Pro a go
as of now. I study on multiple devices so sometimes it's
easier to post to the forum than bring a chat window up in my
workspace due to screen size & buttons.

Oh, and while im at it just to make this code a bit more clear.

public void chargeBattery(int hours) {
if (batteryLevel > 100) {
  batteryLevel = 100;
  System.out.println(batteryLevel);
}  <------
else {

Does the bracket that the arrow is pointing to close
out the method or the if statement? Or would I need 2

public void chargeBattery(int hours) {
  if (batteryLevel > 100) {
  batteryLevel = 100;
  System.out.println(batteryLevel);
  }  
}     
 else {

#4

public void chargeBattery(int hours) { <-- that bracket opens the method definition
if (batteryLevel > 100) { <-- that bracket opens the conditional body

batteryLevel = 100;
System.out.println(batteryLevel);
}

^ that last bracket closes the conditional body
} <-- that bracket closes the method definition
else { <-- this is misplaced entirely due to the method definition being closed

A tip for brackets is that they close in reverse order that they are opened.

public void exampleMethod(int someArgument) {
  if (someArgument == 0) {
    if (someArgument * 1 == 0) {
      System.out.println("Yep, this computer is semi-intelligent.");
    }
  }
}

The first bracket to close is that third from the bottom. Then the second. Then finally the bottom-most.


To answer your main question, the arrow pointing at the bracket closes the conditional.


#5

Thanks for that again @jacobsandersen. I understand that now.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.