Your own droid challenge (need help)


#1

Hi all. I'm having a problem with the droid challenge. Please guide me on how to proceed. Thanks!

Below is the code I did:

class Droid {

  
  int batteryLevel= 100;

  
public Droid(batteryLevel){}
  
  public void activate(){
  System.out.println("Activated. How can I help you?");
  batteryLevel= batteryLevel-5;
  System.out.println("Battery level is:"+ batteryLevel +"percent");}
  
  public void chargeBattery(int hours){
  System.out.println("Droid is charging...");
  batteryLevel= batteryLevel+hours;
  if (batteryLevel>=100){
    batteryLevel=100;
  System.out.println("Battery level is full!");
  }
  else {System.out.println("Battery level will be: "+ batteryLevel+hours +" percent");
       }  
  }

  public int checkBatteryLevel(){
  System.out.println("Current 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...");
       batteryLevel= batteryLevel-20;
    System.out.println("Baterry level after hovering will be: "+ batteryLevel+ " percent");
   };
  }
  
 public static void main(String[] args)
 {
   Droid myDroid = new Droid();
   myDroid.activate();
   myDroid.chargeBattery(5);
   myDroid.hover(1.5);
 }
  

}

#2

What is the error you're getting?

edit:

public static void main(String[] args)
 {
   Droid myDroid = new Droid();
   myDroid.activate();
   myDroid.chargeBattery(5);
   myDroid.hover(1.5);
 }

1.5 is not an int. You need to use the type double or float for decimal precision.


#3

Thanks for the reply jacob. This is the error I got:

Droid.java:7: error: expected
public Droid(batteryLevel){}


#4

Ah @eldwen_lau, you closed you're constructor without
setting a variable to you're variable instance I believe.
Was just working on the same project earlier.

You're code is incorrect below.

    int batteryLevel = 100;

public Droid(batteryLevel){} <code>

It's asking you not to specify a value too the
instance variable batteryLevel, but instead
inside of the Droid constructer, set the batteryLevel
to = 100. Hope this helps.


#5

Thank you! That solved the error!


#7

Solved the problem. Thank you!