java: double 不能取消引用

java: double cannot be dereferenced

这是作业:

Implement a class Car. A Car object should have three (private) instance variables, one for fuel efficiency (representing miles per gallon), one for fuel level (representing gallons), and a variable that acts as an odometer (representing miles). The fuel efficiency of a car should be specified as a parameter in a Car constructor and the constructor should set the fuel level and the odometer to zero. There should also be a second Car constructor that has two parameters that initialize the fuel efficiency and the fuel level (and the constructor should set the odometer to zero). There should be getFuelEfficiency(), getOdometer(), and getFuelLevel() methods. There should be a method addFuel(double gallons) which adds a specified amount to the fuel level and then returns a reference to the current Car object. There should be a method drive(double miles) which simulates driving the car a specified distance. The drive() method should adjust the fuel level by the amount of fuel used, adjust the odometer by the amount of miles driven, and it should return the number of miles driven, which may be less than the number of miles specified if there is not enough fuel. Notice that there are no setFuelEfficiency(), setOdometer(), and setFuelLevel() methods. The fuel efficiency field is immutable; once it is set by the constructor, it cannot be changed. The odometer's value should only be changed by driving the car, as in a real car. The fuel level's value should only be changed by driving the car or by adding fuel. Here is a summary of the constructors and methods (the public interface) of the Car class.

这是我的代码:

public class Car {
    private double fuelEfficiency;
    private double fuelLevel;
    private double odometer;

    public Car(double fuelEfficiency) {
        this.fuelLevel = 0.0;
        this.odometer = 0.0;
    }

    public Car(double fuelEfficiency, double fuelLevel) {
        this.fuelLevel = fuelLevel;
        this.fuelEfficiency = fuelEfficiency;
        this.odometer = 0.0;
    }

    public double getFuelEfficiency() {
        return this.fuelEfficiency;
    }

    public double getFuelLevel() {
        return this.fuelLevel;
    }

    public double getOdometer() {
        return this.odometer;
    }

    public double addFuel(double gallons) {
        this.fuelLevel = this.fuelLevel + gallons;
        return this.fuelLevel;
    }

    public double drive(double miles) {

        double distance = (this.fuelLevel * this.fuelEfficiency);

        if (distance > miles) {
            distance = miles;
        }

        this.fuelLevel = (fuelLevel - (distance / fuelEfficiency));
        this.odometer = odometer + distance;
        return distance;
    }
    public String toString(){
        return "MPG: " + this.fuelEfficiency + "miles: " + this.odometer + "fuel: " + this.fuelLevel;
    }
}

这是我应该用来测试代码的 PreTestDrive,不应更改:

public class PreTestDrive
{
   public static void main( String[] args )
   {
      Car car1 = new Car(25.5);

      double fuelEfficiency = car1.getFuelEfficiency();
      double fuelLevel = car1.getFuelLevel();
      double odometer = car1.getOdometer();

      if (fuelLevel != 0.0 || fuelEfficiency != 25.5 || odometer != 0.0)
      {
         System.out.println("1. There is a problem with your constructors.");
         System.exit(0);
      }

      Car car2 = new Car(10.0, 5.0);

      fuelEfficiency = car2.getFuelEfficiency();
      fuelLevel = car2.getFuelLevel();
      odometer = car2.getOdometer();

      if (fuelLevel != 5.0 || fuelEfficiency != 10.0 || odometer != 0.0)
      {
         System.out.println("2. There is a problem with your constructors.");
         System.exit(0);
      }

      String s1 = car1.toString();

      if (! s1.equals("Car: mpg = 25.5, miles = 0.0, fuel = 0.0.") )
      {
         System.out.println("3. There is a problem with your toString() method.");
         System.exit(0);
      }

      String s2 = car2.toString();

      if (! s2.equals("Car: mpg = 10.0, miles = 0.0, fuel = 5.0.") )
      {
         System.out.println("4. There is a problem with your toString() method.");
         System.exit(0);
      }

      double newFuelLevel = car1.addFuel(12.5).getFuelLevel();

      fuelEfficiency = car1.getFuelEfficiency();
      odometer = car1.getOdometer();

      if(newFuelLevel != 12.5 || fuelEfficiency != 25.5 || odometer != 0.0)
      {
         System.out.println("5. There is a problem with your addFuel method.");
         System.exit(0);
      }

      double distance = car1.drive(76.5);

      if (distance != 76.5 || ! car1.toString().equals("Car: mpg = 25.5, miles = 76.5, fuel = 9.5."))
      {
         System.out.println("6. There is a problem with your drive method.");
         System.exit(0);
      }

      distance = car1.drive(1000);

      if (distance != 242.25 || ! car1.toString().equals("Car: mpg = 25.5, miles = 318.75, fuel = 0.0."))
      {
         System.out.println("7. There is a problem with your drive method.");
         System.exit(0);
      }

      distance = car1.drive(1);

      if (distance != 0.0 || ! car1.toString().equals("Car: mpg = 25.5, miles = 318.75, fuel = 0.0."))
      {
         System.out.println("8. There is a problem with your drive method.");
         System.exit(0);
      }

      System.out.println("Your Car class passed all of these tests.");
   }
}

当我 运行 PreTestDrive 时,我收到错误 java:double cannot be dereferenced。知道它可能是什么吗?

There should be a method addFuel(double gallons) which adds a specified amount to the fuel level and then returns a reference to the current Car object.

您的 addFuel 方法没有 return 对当前 Car 对象的引用。

看起来像这样:

public Car addFuel(double gallons) {
    this.fuelLevel += gallons;
    return this;
}

此外,您没有在 Car(double fuelEfficiency) 构造函数中设置 fuelEfficiency 字段。