Stuck with the return statement


#1




Droid.java:24: error: missing return statement
}
^
Droid.java:39: error: missing return statement
}
^
2 errors


public class Droid {
 
  int batteryLevel;
  
  public Droid(){
    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 int chargeBattery(int hours){
    System.out.println("Droid charging...");
    batteryLevel = batteryLevel+hours;
    if (batteryLevel > 100){
      batteryLevel=100;
      System.out.println("The battery is full.");
    }
    else 
      System.out.println("The battery level is: " + batteryLevel + " percent");
  }
  
  public int checkBatteryLevel(){
    System.out.println("The battery level is: " + batteryLevel + " percent");
    return batteryLevel;
  }
  
  public int hover(int feet){
    if (feet>2){
      System.out.println("Error! I cannot hover above 2 feet.");
    }
    else 
      System.out.println("Hovering...");
      batteryLevel=batteryLevel-20;
      System.out.println("The battery level is: " + batteryLevel + " percent");
  }
  
  public static void main(String[] args){
    Droid droid = new Droid();
    droid.activate();
    droid.chargeBattery(5);
    droid.hover(1);
  }

}


#2

What's stopping you from fixing the problem? What information are you missing? How far are you getting in attempting to address the error message?


#3

i dont understand where i have to insert the 'return' statement and why


#4

This method:

static int add(int a, int b) {
}

Has no return statement, yet it says that it returns an int.
It would either need to either fulfill its promise of returning an int, or change its return type to void to say that it has no return value.

In order to use that method like so:

System.out.println(add(5, 6));

It would have to return a result


#5

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