Droid problems with batteryLevel


#1

I need help because I can't see the correct battery level of the droid. Here is the code. Thanks.

public class Droid
{
  public int batteryLevel;
  public void 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);
  }
  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);
  }
  public int checkBatteryLevel()
  {
  	System.out.println("Battery level is: " + batteryLevel);
    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("Battery level is: " + batteryLevel);
    }
  }
  public static void main (String args[])
  {
  	 Droid droid = new Droid();
     droid.activate();
     droid.chargeBattery(5);
     droid.hover(1);
  }
}

This is what I get in the console:

Activated. How can I help you?
Battery level is: -5
Droid charging...
Battery level is: 0
Hovering...
Battery level is: -20


#2

Your class has no constructor. What do you have and how is that different from a constructor?

If you did have a constructor, it would be setting batteryLevel to 100. Since that's not happening, you don't.


#3

Thank you very much!


#4

because I have made the same mistake could you clarify how public void Droid () is not the constructor?


#5

It doesn't match the syntax of a constructor. Find a reference/example for a constructor and compare. (You're going to make mistakes like this, so what you really need is the ability to spot it)


#7