Java / Elon's Toy Car / Task 3 & 5

Hello,

I’m stuck with passing the tasks 3 and 5 from the ‘Elon’s Toy Car’ exercise.
Test 4 from Task 3 - failed:
Code Run:

@Test
@Tag(“task:3”)
@DisplayName(“The batteryDisplay method shows full battery message on a new car”)
public void new_car_battery_display() {
ElonsToyCar car = new ElonsToyCar();
assertThat(car.batteryDisplay()).isEqualTo(“Battery at 100%”);
}
Test Failure:
Message:
expected: “Battery at 100%”
but was: “Battery at 83%”
Exception: org.opentest4j.AssertionFailedError:
expected: “Battery at 100%”
but was: “Battery at 83%”
at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
at ElonsToyCarTest.new_car_battery_display(ElonsToyCarTest.java:38)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

I don’t understand why Elon's Toy Car in Java on Exercism returns “Battery at 83%” instead of “Battery at 100%” whereas my IntelliJ IDEA Community Edition displays “Battery at 100%”, what is expected.

@mdel2021 To answer, why your code does what you have shown, we need to see your code, too. Please post your code here as well, all of the file with the solution. Please use a code block (Preformatted text, Ctrl+e), not a quote as you did for the failure message.

Hello,
please find the code:

public class ElonsToyCar {
    private static int distance;
    private static int battery;

    public ElonsToyCar() {
    }

    public static ElonsToyCar buy() {
        ElonsToyCar.distance = 0;
        ElonsToyCar.battery = 100;
        System.out.println(batteryDisplay());
        System.out.println(distanceDisplay());
        return new ElonsToyCar();
    }

    public static String distanceDisplay() {
        return "Driven " + distance + " meters";
    }

    public static String batteryDisplay() {
        return "Battery at " + battery + "%";
    }

    public void drive() {
        if (battery >= 1) {
            distance += 20;
            battery--;
            System.out.println(batteryDisplay());
            System.out.println(distanceDisplay());
        } else {
            System.out.println("Battery empty");
        }
    }
}

As I written previously, tasks 1,2,4,6 are passed.
The following tests to tasks 3 and 5 are failing:

Task 3 - test 4:

@Test
@Tag("**task:3**")
@DisplayName("**The batteryDisplay method shows full battery message on a new car**")
public void new_car_battery_display() {
    ElonsToyCar car = new ElonsToyCar();
    assertThat(car.batteryDisplay()).isEqualTo("Battery at 100%");
}
Message: 
expected: "Battery at 100%"
 but was: "Battery at 83%"
Exception: org.opentest4j.AssertionFailedError: 
expected: "Battery at 100%"
 but was: "Battery at 83%"
	at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
	at ElonsToyCarTest.new_car_battery_display(ElonsToyCarTest.java:38)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

Task 5 - test 7:

@Test
@Tag("**task:5**")
@DisplayName("**The batteryDisplay method shows the correct message after driving once**")
public void battery_display_after_driving_once() {
    ElonsToyCar car = new ElonsToyCar();
    car.drive();
    assertThat(car.batteryDisplay()).isEqualTo("Battery at 99%");
}
Message: 
expected: "Battery at 99%"
 but was: "Battery at 98%"
Exception: org.opentest4j.AssertionFailedError: 
expected: "Battery at 99%"
 but was: "Battery at 98%"
	at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
	at ElonsToyCarTest.battery_display_after_driving_once(ElonsToyCarTest.java:70)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

Task 5 - test 8:

@Test
@Tag("**task:5**")
@DisplayName("**The batteryDisplay method shows the correct battery percentage after driving multiple times**")
public void battery_display_after_driving_multiple_times() {
    ElonsToyCar car = new ElonsToyCar();
    for (int i = 0; i < 23; i++) {
        car.drive();
    }
    assertThat(car.batteryDisplay()).isEqualTo("Battery at 77%");
}
Message: 
expected: "Battery at 77%"
 but was: "Battery at 0%"
Exception: org.opentest4j.AssertionFailedError: 
expected: "Battery at 77%"
 but was: "Battery at 0%"
	at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
	at ElonsToyCarTest.battery_display_after_driving_multiple_times(ElonsToyCarTest.java:83)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

Task 5 - test 9:

@Test
@Tag("**task:5**")
@DisplayName("**The batteryDisplay method shows battery empty after draining all battery**")
public void battery_display_when_battery_empty() {
    ElonsToyCar car = new ElonsToyCar();
    // Drain the battery
    for (int i = 0; i < 100; i++) {
        car.drive();
    }
    // Attempt to drive one more time (should not work)
    car.drive();
    assertThat(car.batteryDisplay()).isEqualTo("Battery empty");
}
Message: 
expected: "Battery empty"
 but was: "Battery at 0%"
Exception: org.opentest4j.AssertionFailedError: 
expected: "Battery empty"
 but was: "Battery at 0%"
	at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
	at ElonsToyCarTest.battery_display_when_battery_empty(ElonsToyCarTest.java:100)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

This test is the one that shows, what is wrong. The test says: when creating a new instance, the battery needs to be full. As far as I can tell this is not done in your code. You declare instance variables and use them, but you do not initialise them.

Is that clear enough to help?