Main method

class Droid{
int batteryLevel;
public Droid(){
batteryLevel=100 ;
}
public void activate(){
System.out.println(“Activated. How can I help you?”);
batteryLevel=batteryLevel-5;
System.out.println("Battery level is: "+batteryLevel);

}
int hours;
public void chargeBattery(){
System.out.println(“Droid charging…”);
batteryLevel=batteryLevel+hours;
if (batteryLevel>100){
batteryLeverl=100;
System.out.println("Battery level is: "+batteryLevel);
}else {
System.out.println("Battery level is: "+batteryLevel);
}
}
public int checkBatteryLever(){
System.out.println("Battery level is: "+batteryLevel);
return batteryLevel;
}
public int hover(int feet){
if(feet>2){
System.out.println(“Error! I canno hover above 2 feet.”);
}else{
System.out.println(“Hoveing…”);
batteryLevel=batteryLevel-20;
System.out.println("Battery level is: "+batteryLevel);
}
}

}

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

I can’t find my mistakes. This code just doesn’t work. Can anyone help?

Shouldn’t your hover function take in an integer?

You should make it a void function so,

    public void hover(int feet){    
        if(feet >= 2) {      
            System.out.println("Error! I cannot hover above 2 feet.");    
    }   else{      
            System.out.println("Hoveing...");        
        batteryLevel=batteryLevel-20;      
            System.out.println("Battery level is: "+batteryLevel);    
    }  
    }

Oh, thank you. I have changed that. But it seems that the problem is about the main method…here is the console:

The error is being given there because your functions are not defined correctly. For example, Your chargeBattery has no parameters and yet you give it an integer 5.

And your use of closing tags does not suffice thus resulting in the 5th error.

Thanks, I finally got it run.

1 Like

Can I see your working code? :grinning:

What the error message is saying is that only classes, enums and interfaces may be at the top level of a file. Everything else has to be in one of those.

of course~

class Droid{
int batteryLevel;
public Droid(){
batteryLevel=100 ;
}
public void activate(){
System.out.println(“Activated. How can I help you?”);
batteryLevel=batteryLevel-5;
System.out.println("Battery level is: "+batteryLevel);

}

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);
}else {
System.out.println("Battery level is: "+batteryLevel);
}
}
public int checkBatteryLevel(){
System.out.println("Battery level is: "+batteryLevel);
return batteryLevel;
}
public void hover(int feet){
if(feet>2){
System.out.println(“Error! I canno hover above 2 feet.”);
}else{
System.out.println(“Hoveing…”);
batteryLevel=batteryLevel-20;
System.out.println("Battery level is: "+batteryLevel);
}
}
public static void main(String args){
Droid myDroid=new Droid();
myDroid.activate();
myDroid.chargeBattery(5);
myDroid.hover(1);
}
}

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