Stuck on Remote Control Competition

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

Finally solved this one! Thank anyway!

1 Like

@andrescq95 Sorry no-one got to this. Could you maybe share how you solved it for future readers please? :slight_smile: Thanks!

1 Like

Hi @iHiD! Absolutely. After reading and searching on how to implement Comparable , I found although I still need some practice to fully understand it.

Here’s the code I implemented:

TestTrack:

public static List<ProductionRemoteControlCar> getRankedCars( List<ProductionRemoteControlCar> unsortedList) {
        unsortedList.sort(Collections.reverseOrder(ProductionRemoteControlCar::compareTo));
        return unsortedList;
    }

ProductionRemoteControlCar:

@Override
    public int compareTo(ProductionRemoteControlCar car) {
        return Integer.compare(this.victories, car.victories);
    }
1 Like

Late to the party, somehow i missed this thread. Here’s a breakdown of the errors:

  1. compareTo must be defined with only one parameter. This is what the interface demands. It was implemented with 2 parameters and therefore the compiler complained that ProductionRemoteControlCar does not implement comparable. Since it did not implement comparable, the class should have been declared abstract.
  2. In TestTrack there’s an assignment list = Collections.sort(list) which is incorrect. The right hand side is an in-place sorting with type void. The left hand side has type List.
1 Like

Thanks for sharing!

Just coming back to this because I ran into the same compilation error:

The instructions and the dummy implementation instruct students to implement the following method:

List<ProductionRemoteControlCar> getRankedCars(ProductionRemoteControlCar car1, ProductionRemoteControlCar car2);

However, the test suite is actually expecting a different signature:

List<ProductionRemoteControlCar> getRankedCars(List<ProductionRemoteControlCar> cars);

The instructions also contain this snippet:

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

This doesn’t make a lot of sense, because the unsortedCars list is unused in this snippet. I think the author meant for the getRankedCars method to accept the unsorted list instead of two ProductionRemoteControlCar instances.

I can open a PR to address this, since at the very least the dummy implementation should be able to compile without requiring the student to fix the method signature.

See Java / Remote Control Competition - #7 by IsaacG

Oops, forgot to check whether there were already open PRs for this. Thanks for the heads-up!

This should be fixed now.

1 Like