I am working on the first part of my assignment in which the instructions are
Implement the Comparator<TeamStats> interface in a class named WinsComparator to compare teams according to the number of wins, most wins first. If the wins are equal then use the losses, least losses first. If both wins and losses are equal then compare the name in natural (alphabetic) order.
So far I am just figuring out comparing by wins but I get the error int cannot be dereferenced.
NOTES:
A- we HAVE to use comparator & Nothing we haven't been taught before..
B- it has been done in an example program and does work...
http://www.cis.fiu.edu/~kraynek/COP2210-examples/WordCount.java
Any suggestions down the line for this part of my prog would be helpful as well.
public class TeamStats implements Comparable<TeamStats> {
private String teamName;
private int wins;
private int losses;
private ArrayList<String> yearsPlayed;
public TeamStats( String teamName){
this.teamName= teamName;
yearsPlayed = new ArrayList<String>();
wins = 0;
losses = 0;
}//end constructor
public String getName(){
return teamName;
}//end getName
public int getWins(){
return wins;
}//end getWins
public int getLosses(){
return losses;
}//end get Losses
public ArrayList<String> getYearsPlayed(){
return yearsPlayed;
}//end getYears Played
public void incrementWins(){
wins++;
}//end increment wins
public void incrementLosses(){
losses ++;
}//end increment losses
public void addYear(String aYear){
if( !yearsPlayed.contains(aYear)){
yearsPlayed.add(aYear);
}//end if
}//end addYear
public boolean equals(Object rhs) {
if( rhs == null ) return false;
if( !getClass().equals(rhs.getClass()) ) return false;
TeamStats rhsStat = (TeamStats)rhs;
return getName().equals(rhsStat.getName());
} // end equals
public int hashCode() {
return getName().hashCode();
}
public String toString(){
return "Team: " + getName() + " Wins: " + getWins()
+ " Losses: " + getLosses() + " Years Played: " + getYearsPlayed() ;
}
public int compareTo(TeamStats otherStat) {
return getName().compareTo(otherStat.getName());
}// end compareTo
}//end class teamStats
class WinsComparator implements Comparator<TeamStats>{
public int compare(TeamStats lhs, TeamStats rhs){
int diff = rhs.getWins() - lhs.getWins();
if ( diff !=0 ) return diff;
return lhs.getWins().compareTo(rhs.getWins());
}
}
Edited by: tearfromthared on Dec 2, 2007 6:21 PM
Edited by: tearfromthared on Dec 2, 2007 6:22 PM