Problem with Need For Speed exercise

I have the following code, but the tests are not running correctly. Every time I try to run the tests, it returns a timeout error as if there were an infinite loop in my code.

class NeedForSpeed {
    private int speed;
    private int batteryDrain;
    private int distanceDriven = 0;
    private int battery = 100;
    
    public NeedForSpeed(int speed, int batteryDrain) {
        this.speed = speed;
        this.batteryDrain = batteryDrain;
    }

    public boolean batteryDrained() {
        return this.batteryDrain > this.battery;
    }

    public int distanceDriven() {
        return this.distanceDriven;
    }

    public void drive() {
        if(this.batteryDrain < this.battery) {
            this.distanceDriven += this.speed;
            this.battery -= this.batteryDrain;
        }
    }

    public static NeedForSpeed nitro() {
        return new NeedForSpeed(50, 4);
    }
}

class RaceTrack {
    private int distance;
    
    public RaceTrack(int distance) {
        this.distance = distance;
    }

    public boolean tryFinishTrack(NeedForSpeed car) {
        while (car.distanceDriven() < this.distance && !car.batteryDrained()) {
            car.drive();
        }
        return car.distanceDriven() >= this.distance;
    }
}

Does anyone can help me?

What happens if you create a car with speed 1 and drain 51 then tell it to drive a track with distance 2?

I am having this problem on the JavaScript simple-cipher exercise, which passes all tests on my home system – could there be a problem with the system here?

Please create a new thread for a new request. This thread is about Java and Need for Speed. You’re asking about a different exercise on a different language. Please use a new thread. Please include your code and the test failures. Please use a codeblock and not a screenshot.

I was not requesting help: mine is already out to mentoring.
Sorry – I misinterpreted this as another exercise on the JavaScript Track, and thought that two such similar results might indicate something happening trackwide.

…and this evening the code passes without my having touched it in the interim

if
  batteryDrain = 10
  battery = 10
  
batery is not drained:
  batteryDrained() {
    return batteryDrain > battery;
    // 10 > 10  false
    
loop continues ad infinitum, 
because drive() doesn't update
  drive() {
    if (batteryDrain < battery) {
    // 10 < 10 false
    // do nothing
1 Like

Nice catch.