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)