Your Own Droid excercise


#1

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

Why in this excercise you need to create a constructor, if you cood just set the batterylevel = 100 in class ?
class Droid {
int batteryLevel = 100;



class Droid {
  																					
  int batteryLevel;

  public Droid(){
    
    batteryLevel = 100;
  }
  
  				public void activate() {
    
    System.out.println("Activating........DESTROY ALL HUMANS!!!");
    batteryLevel = batteryLevel - 5;
    System.out.println("Powerlevel is " + batteryLevel + " %.");
  }
  
  				public void chargeBattery(int hours){
    
    System.out.println("Exterminator is powering up.......");
    
    batteryLevel = batteryLevel + hours;
    
    if (batteryLevel >= 100) {
      
      batteryLevel = 100;
      System.out.println("Powerlevel is " + batteryLevel + " %!!!");
    }
    
    else {
      System.out.println("Powerlevel is " + batteryLevel + "%. NEED MORE POWER");
    }
  }
  
  				public int checkBatteryLevel(){
    
    System.out.println("Powerlevel is " + batteryLevel + " %");
    return batteryLevel;
  }
  
 					public void hover(int feet){
   
    if (feet > 2) {
      System.out.println("ERROR!!! cannot exterminate above 2 feet(who uses feet anyway, if meters is better?)");  
    }
    else {
      
      System.out.println("Hovering ... ready to EXTERMINATE...");
      batteryLevel = batteryLevel - 20;
      System.out.println("Powerlevel is " + batteryLevel + " %. More power would be NICE!!!" );
    }
    
  }
  
public static void main(String[] args) {
  
  Droid exterminator69 = new Droid();
  
  exterminator69.activate();
  exterminator69.chargeBattery(5);
  exterminator69.hover(2);
}
} 



#2

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