Remote Control Competition

Hi. I solved all tasks except task 3(Race) . this is my TestTrack class. can anybody help me please.

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class TestTrack implements Comparator<ProductionRemoteControlCar> {

    **public static void race(RemoteControlCar car) {**
**        car.drive();**
**    }**
public static List<ProductionRemoteControlCar>getRankedCars(List<ProductionRemoteControlCar> cars) {
    Collections.sort(cars, new TestTrack());
    return cars;
}

@Override
public int compare(ProductionRemoteControlCar t1, ProductionRemoteControlCar t2) {
    return Integer.compare(t2.getNumberOfVictories(), t1.getNumberOfVictories());
}

}

Hi @IamKia,

Please share your code in “codeblocks” with three backticks (```), like this:

```java
your code here
```

Also, please describe the problem.
Do you understand the task?
Do you think you know how to solve it?
Do you get an error message? (Please share it with us.)
Do you understand what the issue is?

Thank you. sure
I think maybe I have misunderstanding with the task.

public class ExperimentalRemoteControlCar implements RemoteControlCar {
    private int distance;

    public void drive() {
        distance+=20;
    }
    public int getDistanceTravelled() {
        return distance;
    }

    public int getDistance() {
        return distance;
    }

    public void setDistance(int distance) {
        this.distance = distance;
    }
}


import java.util.Comparator;

class ProductionRemoteControlCar implements RemoteControlCar, Comparable<ProductionRemoteControlCar> {

    @Override
    public int compareTo(ProductionRemoteControlCar otherCar) {
        return Integer.compare(this.numberOfVictories,otherCar.numberOfVictories);
    }


    int numberOfVictories;

    public int getNumberOfVictories() {
        return numberOfVictories;
    }

    public void setNumberOfVictories(int numberOfVictories) {
        this.numberOfVictories=numberOfVictories;
    }

    private static int distance=0;

    public void drive() {
        distance+=10;
    }

    public int getDistanceTravelled() {
        return distance;
    }

    public static int getDistance() {
        return distance;
    }

    public static void setDistance(int distance) {
        ProductionRemoteControlCar.distance = distance;
    }
}

public interface RemoteControlCar {
    void drive();
    int getDistanceTravelled();
}



import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class TestTrack implements Comparator<ProductionRemoteControlCar> {

    public static void race(RemoteControlCar car) {
        car.drive();
    }

    public static List<ProductionRemoteControlCar>getRankedCars(List<ProductionRemoteControlCar> cars) {
        Collections.sort(cars, new TestTrack());
        return cars;
    }

    @Override
    public int compare(ProductionRemoteControlCar t1, ProductionRemoteControlCar t2) {
        return Integer.compare(t2.getNumberOfVictories(), t1.getNumberOfVictories());
    }
}

The task is:
Implement the TestTrack.race(RemoteControlCar car) method in which the car s get to drive() .

and here the result:

CODE RUN

@Test
@Tag("task:3")
@DisplayName("The TestTrack.race method uses the drive method on the remote control car")
public void race() {
    ProductionRemoteControlCar productionCar = new ProductionRemoteControlCar();
    ExperimentalRemoteControlCar experimentalCar = new ExperimentalRemoteControlCar();
    TestTrack.race((RemoteControlCar) productionCar);
    TestTrack.race((RemoteControlCar) productionCar);
    TestTrack.race((RemoteControlCar) experimentalCar);
    TestTrack.race((RemoteControlCar) experimentalCar);
    assertThat(experimentalCar.getDistanceTravelled() - productionCar.getDistanceTravelled()).isEqualTo(20);
}

TEST FAILURE

Message: expected: 20 but was: 10 Exception: org.opentest4j.AssertionFailedError: expected: 20 but was: 10 at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502) at RemoteControlCarTest.race(RemoteControlCarTest.java:57) 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)

Please use backticks (```) not forward-ticks (´´´) for the code blocks. Otherwise the Markdown parser here on the forum will modify/swallow parts of your code, e.g. everything between angle brackets (Comparable<ProductionRemoteControlCar>).


I wouldn’t call myself a Java programmer but I’ll try to help until the real experts show up.

Compare the definitions of the two member variables named distance in ExperimentalRemoteControlCar and ProductionRemoteControlCar.
Can you spot a difference?

Thanks for your help. I edit it. I solve the compare task and I think maybe I don’t understand the drive task correctly.

I found my mistake. it was the static keyword in the definition of distance in the ProductionRemoteControlCar class.