Why does my code not work


#1

I’ve been looking at this code for ages now and to me it looks exactly the same as the answer. Could someone please tell me what i’m doing wrong?

Code:

public class droid{
  int batteryLevel;
  Droid() {
    batteryLevel = 100;
  }
  public void activate() {
    System.out.println("Activated.");
    batteryLevel = batteryLevel - 5;
    System.out.println("Battery level is: " + batteryLevel + " percent."); 
  }
  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 + " percent.");
    }
    else {
      System.out.println(batteryLevel);
    }
  }
  public int checkBatteryLevel(){
    System.out.println(batteryLevel);
    return batteryLevel;
  }
  public void hover(int feet){
    if(feet > 2){
      System.out.println("I cant hover above 2 feet");
    }
    else{
      System.out.println("Hovering..");
      batteryLevel = batteryLevel - 20;
      System.out.println("Battery level is: "+batteryLevel + " percent");
    }
  }
  public static void main(String[] args) {
    Droid myDroid = new Droid();
    myDroid.activate();
    myDroid.chargeBattery(5);
    myDroid.hover(1); 
  }
}

Error message:

Droid.java:3: error: invalid method declaration; return type required
Droid() {
^
1 error

Solution video:
Solution youtube link

I hope someone can help,

Best regards Matt.


#2

following solution videos is a terrible way to learn programming. But okay, that is just my opinion

regarding your problem, this is your constructor:

Droid() {
    batteryLevel = 100;
  }

which should match your class name, which it currently doesn’t.


#3

Stetim94 is telling the truth about learning. Anyway, When you create a class, always have to be the first letter in Capital Letter so, as Steim94 said above.

Droid() {
batteryLevel = 100;
}

change to:

droid(){
batteryLevel = 100;
}

Take care with the capital Letter in java when you are developping.

Regards


#4

i think you swapped it, classes should have a uppercase first letter so the class name should be changed, not the constructor.


#5

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