Build your own droid challenge - errors


#1

public class Droid {

int batteryLevel;

public Droid() {

int batteryLevel = 100;

}

public void activate() {

  System.out.println("Activated. How can I help you?");
  int batteryLevel = batteryLevel - 5;
  System.out.println("Battery level is: " + batteryLevel + "percent.");

}

public void chargeBattery(int hours) {

  System.out.println("Droid charging...");
  int batteryLevel = batteryLevel + hours;

  if (batteryLevel > 100) {
    batteryLevel = 100;
    System.out.println("Battery level is: " + batteryLevel + "percent.");

  } else {
    System.out.println("Battery level is: " + batteryLevel + "percent.");

  }

}

public int checkBatteryLevel() {

    System.out.println("Battery level is: " + 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...");
      int batteryLevel = batteryLevel - 20;
      System.out.println(batteryLevel);

    }

}

public static void main (String[] args) {

    Droid Droid = new Droid();

    Droid.activate();
    Droid.chargeBattery(5);
    Droid.hover(1);


    }

}

This is the code. I know the indentation is off, but I'm getting three errors:

Droid.java:13: error: variable batteryLevel might not have been initialized
int batteryLevel = batteryLevel - 5;
^
Droid.java:21: error: variable batteryLevel might not have been initialized
int batteryLevel = batteryLevel + hours;
^
Droid.java:48: error: variable batteryLevel might not have been initialized
int batteryLevel = batteryLevel - 20;
^
3 errors


#2

@defmork,
Please read the official documentation,
follow the links
Classes
Objects
in
https://docs.oracle.com/javase/tutorial/java/javaOO/


#3

Ok i just want to do a follow up to @leonhard.wettengmx.n The first problem:

you need to make battery level publicly accessible but you havent:

And thats the error for everything... So if you make the battery level publicly accessible then everything should go fine.


#4

@defmork,
And apart from what @amanuel2 stated
if you declared you fuelLevel

public int fuelLevel;

you do not use int fuelLevel within your Methods
you can just use fuelLevel