Question

As a project for university, I am writing a Java program that takes classes derived from a Player class and stores them in a Club class. The Club is a cricket club (hence the variable/class names). The class below is still only partially built but it compiles and is complete enough in regards to the issue I need resolving. I am receiving two 'unchecked' warnings when compiling the class below:

import java.util.*;

public class Club{
    private String name;
    private List<Player> players;
    private Set<Player> playersAverage;
    private int regID;

    @SuppressWarnings(value = {"unchecked"})
    public Club(){
        this.name = "";
        this.players = new ArrayList<Player>();
        this.playersAverage = new TreeSet<Player>(new BattingAverageComparator());
        this.regID = 1;
    }

    @SuppressWarnings(value = {"unchecked"})
    public Club(String name){
        this.name = name;
        this.players = new ArrayList<Player>();
        this.playersAverage = new TreeSet<Player>(new BattingAverageComparator());
        this.regID = 1;
    }

    public void setName(String name){
        this.name = name;
    }

    public String getName(){
        return this.name;
    }

    public boolean registerPlayer(Player player) throws UninitialisedObjectException, NullPointerException{
        if(!(validPlayer(player))){
            throw new UninitialisedObjectException("attempted to add an uninitialised player object to Club.players");
        }
        if(!(this.players.contains(player))){
            player.setRegID(this.regID);
            this.regID++;
            for(int i = 0; i < this.players.size(); i++){
                if(player.compareTo(this.players.get(i)) > 0){
                    this.players.add(i,player);
                    return true;
                }
            }
        }
        return false;
    }

    public boolean removePlayer(Player player) throws NullPointerException{
        return this.players.remove(player);
    }

    public String getPlayerDetails(int regID) throws InvalidRegistrationIDException{
        String s = "";
        for (int i=0; i < this.players.size(); i++){
            if (this.players.get(i).getRegID() == regID){
                s = this.players.get(i).toString();
                break;
            }
        }
        if(s == ""){
            throw new InvalidRegistrationIDException("getPlayerDetails() attempted on invalid regID");
        }
        return s;
    }

    private boolean validPlayer(Player player){
        return player.getFirstName()!="" || player.getLastName()!="" || player.getAge()>0 || player.getHeight()>0 || player.getWeight()>0;
    }

    public void averages(BattingAverageComparator compareAveragesOf){
    }
}

Using the following Comparator:

import java.util.*;

public class BattingAverageComparator implements Comparator{
    public int compare(Object obj1,Object obj2) throws IllegalArgumentException{
        if(!(obj1 instanceof Player) || !(obj2 instanceof Player)){
            throw new IllegalArgumentException("BattingAverageComparator cannot compare objects that are not of, or do not extend, the Player class.");
        }
        Player thisPlayer = (Player) obj1;
        Player thatPlayer = (Player) obj2;
        if(thisPlayer.getDismissals() == 0 && thatPlayer.getDismissals() == 0){
            if(thisPlayer.getRuns() > thatPlayer.getRuns()){
                return 1;
            }
            else if (thisPlayer.getRuns() < thatPlayer.getRuns()){
                return -1;
            }
            else{
                return thisPlayer.compareTo(thatPlayer);
            }
        }
        else if(thisPlayer.getDismissals() == 0 && thatPlayer.getDismissals() > 0){
            return -1;
        }
        else if(thisPlayer.getDismissals() > 0 && thatPlayer.getDismissals() == 0){
            return 1;
        }
        else{
            double thisAverage = thisPlayer.getRuns()/thisPlayer.getDismissals();
            double thatAverage = thatPlayer.getRuns()/thatPlayer.getDismissals();
            if(thisAverage > thatAverage){
                return 1;
            }
            else if(thisAverage == thatAverage){//need to make a double threshold
                return 0;
            }
            else{
                return -1;
            }
        }
    }

    public boolean equals(Object obj){
        return obj instanceof BattingAverageComparator;
    }
}

The following warning appears for both constructors:

Club.java:13: warning: [unchecked] unchecked conversion found : BattingAverageComparator
required: java.util.Comparator<? super Player>
this.playersAverage = new TreeSet<Player>(new BattingAverageComparator());

Is there anyway to fix this other than suppressing the warning?

If you need any more information I will post it. There are quite a few classes in the program and I see no need to post them all at present.

Was it helpful?

Solution

The problem is here:

public class BattingAverageComparator implements Comparator{

You declare this as a raw comparator, but you are feeding in a generic type of <Player>

So change it to

public class BattingAverageComparator implements Comparator<Player>{

OTHER TIPS

Yep, use generic type:

public class BattingAverageComparator implements Comparator<Player>{
  public int compare(Player obj1,Player obj2){
    //etc.
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top