开发者

override abstract method compare()

Hi I'm reasonably new to programming and I am having difficulty with my compare method, I have several classes, my initial issue is with my parent class.

I get this error:

Person is not abstract and does not override method compare(java.lang.Object, java.lang.Object) in Comparator

    public class Person implements Comparator
    {
     //some methods

    public int compare(Person p1, Person p2)
    {
       // if last names are the same compare first names
       if(p1.getLastName().equals(p2.getLastName()))
       {
           return p1.getFirstName().compareTo(p2.getFirstName());
       }
       return p1.getLastName().compareTo(p2.getLastName());

    }

My child class looks some thing like this:

    public class Player extends Person implements Comparator
    {
      //some methods

    public int compare(Player p1, Player p2)
    {
       if(p1.getGamesPlayed()<p2.getGamesPlayed())

       {
          return -1;
       }else if (p1.getGamesPlayed()==p2.getGamesPlayed())
       {
          return 0;
       }else
       {
          return 1;
       }
     }

I also have a club class that stores all info in an ArrayList<Player>team.

my interface :

    public interface Comparator<T>
    {
        int compare(T o1, T o2);
    }

and I also have this class

   public class ComparePlayers implements Comparator<Player>
   {

      public int compare(Player p1, Player p2)
      {
         if(p1.getGamesPlayed()< p2.getGamesPlayed())
         {
            return -1;
         }else if(p1.getGamesPlayed()== p2.getGamesPlayed())
         {
            return p1.getLastName().compareTo(p2.getLastName());
         }else
         {
             return 1;
         }
       }

the spec for this is:

When a new player is signed, she/he should be inserted into the Club class in alphabetical order of last name (and first name if last names are the same). To do this make your Person and Player class implement the appropriate Comparable interface.

Write a class ComparePlayers 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开发者_如何学编程). Implement a new Constructor for the Club class that takes a Comparator<Player> parameter. Hence write a main program that will print information about each player in the club where players are listed by decreasing order of games played. This should allow ordering to be dictated by the main program without modifying the code in any of your other classes.

I'm sorry if this is long winded but I am struggling to see what is wrong, I have tried several variation and it just wont work. this is due in on Friday, just a push in the right direction would be hugely appreciated.


Change your compare implementation to:

public int compare(Object o1, Object 02)
{
   Person p1 = (Person)o1;
   Person p2 = (Person)o2;
   // if last names are the same compare first names
   if(p1.getLastName().equals(p2.getLastName()))
   {
       return p1.getFirstName().compareTo(p2.getFirstName());
   }
   return p1.getLastName().compareTo(p2.getLastName());

}


Person and Player shouldn't implement Comparator. If they need to be comparable by some natural order, you should implement Comparable.

Your ComparePlayers class looks fine, what's the issue with it? (I assume youre using java.util.Comparator here, don't you?)

In order to sort the list you might use Collections.sort(players, new ComparePlayers());, if players is of type List<Player> (resp. an implementation of that interface).

Note that if Person and Player should Comparable, then you'd need to implement compareTo(Person p2) and compareTo(Player p2) where the implementation for Player should call super.compareTo(p2); for the equal games played case.

Example:

class Person<T extends Person> implements Comparable<T> {
  public int compareTo(Tp2) { ... }
}

class Player extends Person<Player> {
  public int compareTo(Player p2) {
    ...
    if(gamesPlayed == p2.getGamesPlayed() ) {
      return super.compareTo(p2);
    }
    ...
  }
}


The compare method of Comparator takes two Object parameters while your method takes two Person parameters, and so the compiler can't find where you're overriding this method. The solution is to either change the parameters of the method to be Objects or else (and preferably) use a generic Comparator since this will allow your compare method to have Person parameters and also adds type safety checking at compile time.

edit 1: shoot, you already have an example of a generic Comparable class.

edit 2: I didn't see that you were creating your own interface til I read the comment about this. I agree with the commenter -- use the class already in java.util.


Like the exception suggests this declaration

  • public int compare(Person p1, Person p2)

has to be changed to

  • public int compare(Object p1, Object p2)

Then you'll also have to adjust the method code as

public int compare(Object p1, Object p2)
{
   // if last names are the same compare first names
   if(((Person)p1).getLastName().equals(((Person)p2).getLastName()))
   {
       return ((Person)p1).getFirstName().compareTo(((Person)p2).getFirstName());
   }
   return ((Person)p1).getLastName().compareTo(((Person)p2).getLastName());

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜