Why am I going negative?


#1



https://www.codecademy.com/courses/learn-java/projects/droid


There are no specific error messages, I just return negatives that are different from the responses given in the outline
Such as:
" Activated. How may I help you?
Battery level is: -5 percent.
Droid charging...
0 percent
Hovering...
-20"


public class Droid{
  public int batteryLevel; 
    
   public void droid (){
      batteryLevel = 100;
    }
  public void activate (){
    //Activates droid and checks battery level
      System.out.println("Activated. How may 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;
    			System.out.println("Battery level is:" + batteryLevel + " percent");
        }
    		else {
          System.out.println(batteryLevel + " percent");
        }
  
  }
  public int checkBatteryLevel(){
    System.out.println(batteryLevel + "percent");
    return batteryLevel;
  }
  public void 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(batteryLevel);
}
  public static void main(String [ ] args){
    Droid charles = new Droid();
    charles.activate();
    charles.chargeBattery(5);
    charles.hover(2);
    
    
  }

}


#2

Fixed the error, as it turns out my original code snippet didn't make a correction for where

public void droid () {
batteryLevel = 100; 
}

should be

public Droid () {
batteryLevel = 100; 
}

#3

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