Your Own Droid


#1

Your Own Droid.

Hi I have problem with my code. I have a lot of errors and I dont know why:confused:

Here is My code:


public class Droid{
  int batteryLevel = 100;
}
public Droid(){}

public void activate(){
  System.out.println("Activated. How can I help you?");
  batteryLevel -= 5; //batteryLevel = batteryLevel - 5;
  System.out.println("Battery level is: " + batteryLevel + " percent.");
  
}
public void chargeBattery (int hours){
  
  System.out.println("Droid charging...");
  
  batteryLevel += hours; //batteryLevel = batteryLevel + hours;
  
  if(batteryLevel >= 100){
   System.out.println("Battery is fully charged");
  } else{
    Syste.out.println("BatteryLevel is " + batteryLevel);
  }
}
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...");
      batteryLevel = batteryLevel - 20;
  System.out.println("Battery level is: " + batteryLevel + " percent.");   
}
      }
public static void main(String[] args){
  Droid R2 = new Droid();
  R2.activate();
  R2.chargeBattery(5);
  R2.hover(0);
}
                      

And here are my errors:

Droid.java:4: error: class, interface, or enum expected
public Droid(){}
^
Droid.java:6: error: class, interface, or enum expected
public void activate(){
^
Droid.java:8: error: class, interface, or enum expected
batteryLevel -= 5; //batteryLevel = batteryLevel - 5;
^
Droid.java:9: error: class, interface, or enum expected
System.out.println(“Battery level is: " + batteryLevel + " percent.”);
^
Droid.java:11: error: class, interface, or enum expected
}
^
Droid.java:12: error: class, interface, or enum expected
public void chargeBattery (int hours){
^
Droid.java:16: error: class, interface, or enum expected
batteryLevel += hours; //batteryLevel = batteryLevel + hours;
^
Droid.java:18: error: class, interface, or enum expected
if(batteryLevel >= 100){
^
Droid.java:20: error: class, interface, or enum expected
} else{
^
Droid.java:22: error: class, interface, or enum expected
}
^
Droid.java:24: error: class, interface, or enum expected
public int checkBatteryLevel(){
^
Droid.java:26: error: class, interface, or enum expected
return batteryLevel;
^
Droid.java:27: error: class, interface, or enum expected
}
^
Droid.java:28: error: class, interface, or enum expected
public void hover (int feet){
^
Droid.java:30: error: unclosed string literal
System.out.println("Error! I cannot hover above 2 feet.);
^
Droid.java:33: error: class, interface, or enum expected
batteryLevel = batteryLevel - 20;
^
Droid.java:34: error: class, interface, or enum expected
System.out.println(“Battery level is: " + batteryLevel + " percent.”);
^
Droid.java:35: error: class, interface, or enum expected
}
^
Droid.java:37: error: class, interface, or enum expected
public static void main(String[] args){
^
Droid.java:39: error: class, interface, or enum expected
R2.activate();
^
Droid.java:40: error: class, interface, or enum expected
R2.chargeBattery(5);
^
Droid.java:41: error: class, interface, or enum expected
R2.hover(0);
^
Droid.java:42: error: class, interface, or enum expected
}
^
23 errors

Thank You for your responses :slight_smile:


#2

here:

System.out.println("Error! I cannot hover above 2 feet.);

you forgot to end your string (missing quotation marks)

on the forum, you can clearly see this by the color change around that line (look back at your post)


#3

Thank you. But after correcting this mistakei still have 23 errors :confused:

“Droid.java:4: error: class, interface, or enum expected
public Droid(){}
^
Droid.java:6: error: class, interface, or enum expected
public void activate(){
^
Droid.java:8: error: class, interface, or enum expected
batteryLevel -= 5; //batteryLevel = batteryLevel - 5;
^
Droid.java:9: error: class, interface, or enum expected
System.out.println(“Battery level is: " + batteryLevel + " percent.”);
^
Droid.java:11: error: class, interface, or enum expected
}
^
Droid.java:12: error: class, interface, or enum expected
public void chargeBattery (int hours){
^
Droid.java:16: error: class, interface, or enum expected
batteryLevel += hours; //batteryLevel = batteryLevel + hours;
^
Droid.java:18: error: class, interface, or enum expected
if(batteryLevel >= 100){
^
Droid.java:20: error: class, interface, or enum expected
} else{
^
Droid.java:22: error: class, interface, or enum expected
}
^
Droid.java:24: error: class, interface, or enum expected
public int checkBatteryLevel(){
^
Droid.java:26: error: class, interface, or enum expected
return batteryLevel;
^
Droid.java:27: error: class, interface, or enum expected
}
^
Droid.java:28: error: class, interface, or enum expected
public void hover (int feet){
^
Droid.java:31: error: class, interface, or enum expected
} else{
^
Droid.java:33: error: class, interface, or enum expected
batteryLevel = batteryLevel - 20;
^
Droid.java:34: error: class, interface, or enum expected
System.out.println(“Battery level is: " + batteryLevel + " percent.”);
^
Droid.java:35: error: class, interface, or enum expected
}
^
Droid.java:37: error: class, interface, or enum expected
public static void main(String[] args){
^
Droid.java:39: error: class, interface, or enum expected
R2.activate();
^
Droid.java:40: error: class, interface, or enum expected
R2.chargeBattery(5);
^
Droid.java:41: error: class, interface, or enum expected
R2.hover(0);
^
Droid.java:42: error: class, interface, or enum expected
}
^
23 errors”


#4

this is currently your class:

public class Droid{
  int batteryLevel = 100;
}

the constructor (public Droid(){}) and all the methods are not part of your class, why?

that should leave you with 1 error


#5

Ok all is fine now. Thank you for your time and help. Have a nice day :smile:


#6