batteryLevel might not have been initialized


#1


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

There seems to be an issue with Battery Level but I can't figure out what or why. What do I need to do to make it "initialize"?

How does everything else look so far?

Droid.java:14: error: variable batteryLevel might not have been initialized
int batteryLevel = batteryLevel - 5;
^
Droid.java:23: error: variable batteryLevel might not have been initialized
int batteryLevel = batteryLevel + hours;
^
Droid.java:52: error: variable batteryLevel might not have been initialized
int batteryLevel = batteryLevel - 20;
^
3 errors


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("Robot has used 5 percent of battery.");
    System.out.println("Battery level is: " + batteryLevel + "percent.");
    
  }
  
  public void chargeBattery(int hours){
    
    System.out.println("Droid chargin...");
    int batteryLevel = batteryLevel + hours;
    
    if (batteryLevel <= 100){
      
      System.out.println("Battery level is: " + batteryLevel + "percent.");
      System.out.println("No charging required.");
      
    } else {
      
      System.out.println("Battery level is: " + batteryLevel + "percent.");
    }
  }
  
  public void checkBatteryLevel(){
    System.out.println("Battery level is: " + batteryLevel + "percent.");
    
  }
  
  public void hover(int feet){
    
    
    
    if (feet < 2){
      
      System.out.println("Error! I cant hover above 2 feet");
      
    } else {
      
      System.out.println("Hovering... " + feet + " feet.");
    	int batteryLevel = batteryLevel - 20;
    	System.out.println("Robot has used 20 percent of battery.");
    	System.out.println("Battery level is: " + batteryLevel + "percent.");
      
    }
    
  }
  
  
  public static void main(String[] args) {
    
    Droid bobby = new Droid();
    bobby.activate();
    bobby.chargeBattery(5);
    bobby.hover(1);
    
  }
  
  
}


#2

DO NOT define a variable repeatedly inside statements. You need to begin your class with a private variable, called batterLevel that you initialize inside your constructor. You should NEVER define a variable inside methods, if you expect to use it anywhere else in the class.

Here's your code, with the repeated definitions removed:

class Droid {
  
  private int batteryLevel;
  
  public Droid(){
    
    batteryLevel = 100;
    
  }
  
  public void activate(){
    
    System.out.println("Activated. How can I help you?");
    batteryLevel = batteryLevel - 5;
    System.out.println("Robot has used 5 percent of battery.");
    System.out.println("Battery level is: " + batteryLevel + "percent.");
    
  }
  
  public void chargeBattery(int hours){
    
    System.out.println("Droid chargin...");
    batteryLevel = batteryLevel + hours;
    
    if (batteryLevel <= 100){
      
      System.out.println("Battery level is: " + batteryLevel + "percent.");
      System.out.println("No charging required.");
      
    } else {
      
      System.out.println("Battery level is: " + batteryLevel + "percent.");
    }
  }
  
  public void checkBatteryLevel(){
    System.out.println("Battery level is: " + batteryLevel + "percent.");
    
  }
  
  public void hover(int feet){
    
    
    
    if (feet < 2){
      
      System.out.println("Error! I cant hover above 2 feet");
      
    } else {
      
      System.out.println("Hovering... " + feet + " feet.");
    	batteryLevel = batteryLevel - 20;
    	System.out.println("Robot has used 20 percent of battery.");
    	System.out.println("Battery level is: " + batteryLevel + "percent.");
      
    }
    
  }
  
  
  public static void main(String[] args) {
    
    Droid bobby = new Droid();
    bobby.activate();
    bobby.chargeBattery(5);
    bobby.hover(1);
    
  }
  
  
}

#3

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