简体   繁体   中英

I can't figure out why a class i made isn't working

I just started with objects and classes and when i try to use a method in the class through the .method thing I get a syntax error. I used almost an identical format for a previous practice problem and it worked fine so I'm drawing a blank here.

this is the class for the object.

public class Automobile {
    public double mpg;
    public double gallons = 0;

    public Automobile(double mpg) {
        this.mpg = mpg;
    }

    public void fill(double f) {
        gallons += f;
    }

    public void takeTrip(double m) {
        gallons = gallons - (1 / (mpg / m));
    }

    public void reportFuel() {
        System.out.println(gallons);
    }
}

this is the tester class

public class Tester {
Automobile myBmw = new Automobile(24);
 myBmw.fill(20);
 myBmw.takeTrip(100);
 myBmw.reportFuel();
}

As per java syntax, you can't write executable statements as mentioned here:

 myBmw.fill(20);
 myBmw.takeTrip(100);
 myBmw.reportFuel();

out of methods/constructors/blocks. You need to move your code to an appropriate place as per your requirement.

You Tester class is trying to execute code outside of a valid execution context (ie a method or static block)...

public class Tester {
    Automobile myBmw = new Automobile(24);
    public Tester() {
        myBmw.fill(20);
        myBmw.takeTrip(100);
        myBmw.reportFuel();
    }
}

For example...

You cannot do this in the class like this. You have to call other class methods from a method/block.

public class Tester {
  Automobile myBmw = new Automobile(24);
  public Tester(){  // It may be any other function 

    myBmw.fill(20);
    myBmw.takeTrip(100);
    myBmw.reportFuel();
  }

  // OR if you miss the main()
  public static void main(String args[]){
    Automobile myBmw = new Automobile(24); 
    myBmw.fill(20);
    myBmw.takeTrip(100);
    myBmw.reportFuel();
  }
}

Change the block

public class Tester {
Automobile myBmw = new Automobile(24);
 myBmw.fill(20);
 myBmw.takeTrip(100);
 myBmw.reportFuel();
}

to

public class Tester {
Automobile myBmw = new Automobile(24);
public Tester(){
 myBmw.fill(20);
 myBmw.takeTrip(100);
 myBmw.reportFuel();
}
}

or place it inside another method in Tester class or a static { } block(you will need to declare Automobile instance as static too for this)

You should use main class , I have executed your code and its running fine when we use main class

public class Tester {
public static void main(String[] args) {
    Automobile myBmw = new Automobile(24);
     myBmw.fill(20);
     myBmw.takeTrip(100);
     myBmw.reportFuel();
}
}

And if you dont want to use main() , then you should use a method for it

public void testME() {
        Automobile myBmw = new Automobile(24);
        myBmw.fill(20.0);
        myBmw.takeTrip(100);
        myBmw.reportFuel();
    }

You did not provide main method in your tester class so that you got error.

public class Tester {
public static void main(String args[]){ // main method required.
Automobile myBmw = new Automobile(24);
 myBmw.fill(20);
 myBmw.takeTrip(100);
 myBmw.reportFuel();
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM