Java Reference
In-Depth Information
@Override
public int compare(Player o1, Player o2) {
float o1A, o2A;
int retVal;
if (o1.getId() == o2.getId())
return 0;
if (o1.hasRole(Player.Roles.Batter)){
try {
o1A = o1.asBatter().getAverage();
} catch (NotEnoughAtBatsException e){
o1A = (float) 0.0;
}
} else
o1A = 0.0f;
if (o2.hasRole(Player.Roles.Batter)){
try {
o2A = o2.asBatter().getAverage();
} catch (NotEnoughAtBatsException e){
o2A = (float) 0.0;
}
} else
o2A = 0.0f;
if (o1A < o2A)
retVal = -1;
else if (o2A < o1A)
retVal = 1;
else if (o1.getId() < o2.getId())
retVal = -1;
else retVal = 1;
return retVal;
}
public boolean equals(Player o1, Player o2){
return (o1.getId()== o2.getId());
}
}
Now that we have a comparator that will rank Players by their batting average, we can create
a method that will print out the players on a Team by their batting averages. We will create a
Search WWH ::




Custom Search