大学のプロジェクトとして、Playerクラスから派生したクラスをClubクラスに格納するJavaプログラムを作成しています。クラブはクリケットクラブです(したがって、変数/クラス名)。以下のクラスはまだ部分的にしか構築されていませんが、コンパイルして、解決する必要のある問題に関して十分に完了しています。以下のクラスをコンパイルすると、2つの「チェックされていない」警告が表示されます。
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){
}
}
次のコンパレータを使用します。
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;
}
}
次の警告が両方のコンストラクターに表示されます。
Club.java:13: warning: [unchecked] unchecked conversion found : BattingAverageComparator
required: java.util.Comparator<? super Player>
this.playersAverage = new TreeSet<Player>(new BattingAverageComparator());
警告を抑制する以外にこれを修正する方法はありますか?
さらに情報が必要な場合は投稿します。プログラムにはかなりの数のクラスがあり、現時点ではそれらすべてを投稿する必要はないと思います。