Hello! I got stuck on the Remote Control Competition exercise. I’m adding the code here.
This is my first time using interfaces and I don’t understand how to implement the Comparable in order to complete this exercise.
Your help is appreciated!
Remote Control Car
public interface RemoteControlCar {
void drive();
int getDistanceTravelled();
}
Experimental Remote Control Car
public class ExperimentalRemoteControlCar implements RemoteControlCar {
public int distance = 0;
public void drive() {
distance = (getDistanceTravelled() + 20);
}
public int getDistanceTravelled() {
return distance;
}
}
Production Remote Control Car
public class ProductionRemoteControlCar implements RemoteControlCar, Comparable <ProductionRemoteControlCar>{
public int distance = 0;
public int victories = 0;
public void drive() {
distance = (getDistanceTravelled() + 10);
}
public int getDistanceTravelled() {
return distance;
}
public int getNumberOfVictories() {
return victories;
}
public void setNumberOfVictories(int numberOfVictories) {
victories = numberOfVictories;
}
public int compareTo(ProductionRemoteControlCar car1,ProductionRemoteControlCar car2 ) {
if (car1 instanceof ProductionRemoteControlCar && car2 instanceof ProductionRemoteControlCar) {
Object obj1 = (Object) car1;
Object obj2 = (Object) car2;
if ( car1.getNumberOfVictories() == car2.getNumberOfVictories() ){
return 0;
}
else if (car1.getNumberOfVictories() > car2.getNumberOfVictories()) {
return 1;
}
else {
return -1;
}
}
}
}
Test Track
import java.util.*;
public class TestTrack{
public static void race(RemoteControlCar car) {
car.drive();
}
public static List<ProductionRemoteControlCar> getRankedCars(ProductionRemoteControlCar prc1, ProductionRemoteControlCar prc2) {
List<ProductionRemoteControlCar> list = new ArrayList();
list.add(prc1);
list.add(prc2);
list = Collections.sort(list);
return list;
}
}
Error log:
We received the following error when we ran your code:
[ERROR] COMPILATION ERROR :
[ERROR] /tmp/solution/src/main/java/ProductionRemoteControlCar.java:[1,8] ProductionRemoteControlCar is not abstract and does not override abstract method compareTo(ProductionRemoteControlCar) in java.lang.Comparable
[ERROR] /tmp/solution/src/main/java/TestTrack.java:[13,32] incompatible types: void cannot be converted to java.util.List<ProductionRemoteControlCar>
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project exercise: Compilation failure: Compilation failure:
[ERROR] /tmp/solution/src/main/java/ProductionRemoteControlCar.java:[1,8] ProductionRemoteControlCar is not abstract and does not override abstract method compareTo(ProductionRemoteControlCar) in java.lang.Comparable
[ERROR] /tmp/solution/src/main/java/TestTrack.java:[13,32] incompatible types: void cannot be converted to java.util.List<ProductionRemoteControlCar>
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
Exercism instructions:
Please implement the Comparable<T> interface in the ProductionRemoteControlCar class. The default sort order for cars should be descending order of victories.
Implement the static TestTrack.getRankedCars() to return the cars passed in, sorted in descending order of number of victories.
ProductionRemoteControlCar prc1 = new ProductionRemoteControlCar();
ProductionRemoteControlCar prc2 = new ProductionRemoteControlCar();
prc1.setNumberOfVictories(3);
prc2.setNumberOfVictories(2);
List<ProductionRemoteControlCar> unsortedCars = new ArrayList<>();
unsortedCars.add(prc1);
unsortedCars.add(prc2);
List<ProductionRemoteControlCar> rankings = TestTrack.getRankedCars(prc1, prc2);
// => rankings.get(0) == prc2
// => rankings.get(1) == prc1