I am suppose to write a ComparePlayers class that implements the Comparator<Player> interface. It should compare players by number of games played (and then by alphabetical order of surname if the number of games played is the same). This is what I wrote , it is the general idea i could grasp, and I was wondering how do I improvise this code so it would return the players name instead of 1 or -1. Am not sure how do i go about it.

import java.util.*;
public class ComparePlayers implements Comparator<Player>
{
   
    public int compare(Player ply1,Player ply2)
    {
       if(ply1.gPlayed()>ply2.gPlayed())
       {    
           return 1;
       }else if(ply1.gPlayed()==ply2.gPlayed())
       {
           return ply1.compareTo(ply2);
       }else
       {
           return -1;
       }
    }
}

and I am suppose to implement a new Constructor for the Club class that takes the Comparator<Player> parameter. What I wrote is as followed.
Is it correct? How do I change it?

public Club(Club c)
    {
        ComparePlayers cPlyr = new ComparePlayers();
        clubPlayers = c.clubPlayers;
        for (int num1=0; num1<clubPlayers.size()-1; num1++)
        {
            for (int num2=1; num2<clubPlayers.size(); num2++)
            {
                if (cPlyr.compare(clubPlayers.get(num1), clubPlayers.get(num2))==-1)
                {
                    Player temp = clubPlayers.get(num1);
                    clubPlayers.set(num1, clubPlayers.get(num2));
                    clubPlayers.set(num2, temp);
                }
            }
        }
    }

Cheers

1. The Comparator interface requires two methods (check out the Comparator API JavaDoc). Does your code compile?

a new Constructor for the Club class that takes the Comparator<Player> parameter.

Your constructor takes another Club as parameter, so obviously not right. This part of the exercise is actually a lot smaller and simpler than you think. It's just saying that when you instantiate A Club object you should supply an instance of your Comparator that the Club object can use later for sorting its players etc.

1. The Comparator interface requires two methods (check out the Comparator API JavaDoc). Does your code compile?

Your constructor takes another Club as parameter, so obviously not right. This part of the exercise is actually a lot smaller and simpler than you think. It's just saying that when you instantiate A Club object you should supply an instance of your Comparator that the Club object can use later for sorting its players etc.

okay... i think i understand what you are saying but how do i go about coding it? Am kinda lost... Cheers

Example of passing a class instance as a parameter to a constructor...

Person p = new Person("John Smith");
...
class Person {
  private String name;
  public Person(String name) {
    this.name = name;
  }
  ...
}

Example of passing a class instance as a parameter to a constructor...

Person p = new Person("John Smith");
...
class Person {
  private String name;
  public Person(String name) {
    this.name = name;
  }
  ...
}

I don't understand this..

You have been asked to pass an instance of your comparator to a constructor. Without actually doing your homework for you I showed you an example of passing an instance of String to a constructor. Understand how that works, and you will be able to write your own code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.