2

I'm working on a Java project that asks me to implement the function obtainRanking() : void with the following description:

Sort the players list by using the class method sort (List) from the Collections class, a method to sort the objects of a collection. For that, the object's class (Player in our case), should implement the interface "Comparable" and its method compareTo.

So far, this is how I implemented the interface Comparable:

package modeloqytetet;

public interface Comparable {
    public int compareTo(Object otroJugador);
}

Inside class Player this is how I implemented the said method:

@Override
    public int compareTo(Object otherJugador) {
        int otherCapital = ((Player) otherJugador).getCapital();

        return otherCapital-getCapital();
    }

Now, the method obtainRanking() : void should be implemented in other class and I don't know how to do it. I've been trying to figure out by looking some examples around the internet but nothing seems to work.

Any help would be appreciated.

d3vcho
  • 83
  • 1
  • 10
  • Any method called `obtainRanking` should not return void. Nevertheless, You can pass the the Comparator to `sort` method that you use in the method in other class. – Sid Oct 21 '18 at 11:34
  • 6
    Maybe unrelated but I think they mean that they want you to implement [Comparable](https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html) and not your own `Comparable` interface – Mark Oct 21 '18 at 11:34
  • @Sid I think that the purpose of the method is to sort an existing ArrayList of Player class so the return should be void. Anyway the naming is not right, but I didn't choose it :P – d3vcho Oct 21 '18 at 11:35
  • So you can directly sort in `obtainRanking` method then, can't you? – Sid Oct 21 '18 at 11:36
  • 1
    Hi d3vcho! You'll be needing this method: https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#sort-java.util.List-java.util.Comparator- - put your player instances into a collection, use that to sort it, then the ranking you want is their position in the sorted list. Hope that helps! – hugh Oct 21 '18 at 11:37
  • @Mark I actually didn't think about that. They didn't explain us what an interface is so, it might be my bad. Thanks for the suggestion. – d3vcho Oct 21 '18 at 11:40
  • @Sid That's right, that's what I'm asking for. I don't know if I made myself clear. – d3vcho Oct 21 '18 at 11:41
  • @hugh Thanks for the suggestion, will test it up in a little bit. – d3vcho Oct 21 '18 at 11:42
  • You probably should read this part of official tutorial https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html – Pshemo Oct 21 '18 at 11:43

2 Answers2

3

The instructions is telling you to implement java.lang.Comparable<T>, not your own Comparable interface.

You should do this:

class Player implements Comparable<Player> {
    @Override
    public int compareTo(Player other) {
        return Integer.compare(this.getCapital(), other.getCapital());
    }

    ...
}

For why you should not simply subtract one integer from another to compare them, see here.

Then, you can implement obtainRankings like this:

// this name is quite bad. I would call it sortPlayersCapitalInPlace or something like that
public void obtainRankings(List<Player> players) {
    Collections.sort(players);
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • That seems good! One more thing, shouldn't I @Override the method compareTo? – d3vcho Oct 21 '18 at 11:47
  • @d3vcho The `@Override` annotation is optional. I didn't write it there because I was lazy... :). You should write it. – Sweeper Oct 21 '18 at 11:50
  • @d3vcho it's good practice, yes - but it's not actually required. In fact it was only in Java 7 that it was legal to annotate implemented `interface` methods with `@Override`. – Boris the Spider Oct 21 '18 at 11:50
  • 1
    @d3vcho You are overriding it, but if you are asking if you should add `@Override` annotation than yes, it should be used whenever your intention is to override some method to make compiler test that fact. More at [When do you use Java's @Override annotation and why?](https://stackoverflow.com/q/94361) – Pshemo Oct 21 '18 at 11:52
0

You can use the following code snippets in your obtainRankings() method for customized sorting.

For Ascending order of players,

public static void obtainRankings(List<Player> list){
        Collections.sort(list, (pl1, pl2) -> {
            return ((pl1.getCapital() > pl2.getCapital()) ? 1 : -1);
        });
    }

For Descending order of players,

public static void obtainRankings(List<Player> list){
        Collections.sort(list, (pl1, pl2) -> {
            return ((pl1.getCapital() > pl2.getCapital()) ? -1 : 1);
        });
    }
Manjunath H M
  • 818
  • 1
  • 10
  • 25