In: Computer Science
Please finish this code (Java)
1. Fill in the compare methods for the CompareByPlayoffsAndSalary and CompareByWinsLossesChamps classes
2. In the CompareByPlayoffsAndSalary the compare method should list:
(a) teams that made the playoffs last year before teams that did not.
(b) If this is the same then it should list teams with lower salary first.
3. In the CompareByWinsLossesChamps list:
(a) teams with more wins first.
(b) If the same then list by teams with fewer losses.
(c) If this is the same, teams with the most championships should be listed first.
BasketBallTeam Class public class BasketBallTeam { String name; double salaryInMillions; int numberOfWins; int numberOfLosses; int numberOfChampionships; boolean playoffTeam; public BasketBallTeam(String name, double salaryInMillions, int numberOfWins, int numberOfLosses, int numberOfChampionships, boolean playoffTeam) { this.name = name; this.salaryInMillions = salaryInMillions; this.numberOfWins = numberOfWins; this.numberOfLosses = numberOfLosses; this.numberOfChampionships = numberOfChampionships; this.playoffTeam = playoffTeam; } public double getSalaryInMillions() { return salaryInMillions; } public void setSalaryInMillions(double salaryInMillions) { this.salaryInMillions = salaryInMillions; } public int getNumberOfWins() { return numberOfWins; } public void setNumberOfWins(int numberOfWins) { this.numberOfWins = numberOfWins; } public int getNumberOfLosses() { return numberOfLosses; } public void setNumberOfLosses(int numberOfLosses) { this.numberOfLosses = numberOfLosses; } public int getNumberOfChampionships() { return numberOfChampionships; } public void setNumberOfChampionships(int numberOfChampionships) { this.numberOfChampionships = numberOfChampionships; } public boolean isPlayoffTeam() { return playoffTeam; } public void setPlayoffTeam(boolean playoffTeam) { this.playoffTeam = playoffTeam; } public String toString(){ return name; } }
CompareByWInsLossesChamps Class
import java.util.Comparator; public class CompareByWinsLossesChamps implements Comparator<BasketBallTeam> { public int compare(BasketBallTeam a, BasketBallTeam b){ } } }
CompareByPlayoffsAndSalary Class
import java.util.Comparator; public class CompareByPlayoffsAndSalary implements Comparator<BasketBallTeam> { public int compare(BasketBallTeam a, BasketBallTeam b){ } } }
Note: here comapre function is done as per the requirement in the question along with also write explanantion in comment in the code for better understanding, but still if you have any doubt or issue or want to change in the code, you post the issue here.
CompareByPlayoffsAndSalary.java
import java.util.Comparator;
public class CompareByPlayoffsAndSalary implements Comparator<BasketBallTeam> {
int teamA = 1, teamB = -1;
/*
* if compare return positive value (1) value means first argument true if
* compare return negative value (-1) means second argument is true if compare
* return 0 means both argument is equal
*/
/*
* if condition is true than it return 1 (Team a wins) first argument true ,
* else condition is true means compare return -1 which is (team b wins) or
* second argument true, lastly return 0 , as all condition is equal no one wins
*/
public int compare(BasketBallTeam a, BasketBallTeam b) {
if (a.isPlayoffTeam() != b.isPlayoffTeam()) {
if (a.isPlayoffTeam())
return teamA;
else
return teamB;
} else if (a.getSalaryInMillions() != b.getSalaryInMillions()) {
if (a.getSalaryInMillions() < b.getSalaryInMillions())
return teamA;
else
return teamB;
}
return 0;
}
}
CompareByWinsLossesChamps.java
import java.util.Comparator;
public class CompareByWinsLossesChamps implements Comparator<BasketBallTeam> {
int teamA = 1, teamB = -1;
/*
* if compare return positive value (1) value means first argument true if
* compare return negative value (-1) means second argument is true if compare
* return 0 means both argument is equal
*/
/*
* if condition is true than it return 1 (Team a wins) first argument true ,
* else condition is true means compare return -1 which is (team b wins) or
* second argument true, lastly return 0 , as all condition is equal no one wins
*/
public int compare(BasketBallTeam a, BasketBallTeam b) {
if (a.getNumberOfWins() != b.getNumberOfWins()) {
if (a.getNumberOfWins() > b.getNumberOfWins())
return teamA;
else
return teamB;
} else if (a.getNumberOfLosses() != b.getNumberOfLosses()) {
if (a.getNumberOfLosses() < b.getNumberOfLosses())
return teamA;
else
return teamB;
} else if (a.getNumberOfChampionships() != b.getNumberOfChampionships()) {
if (a.getNumberOfChampionships() > b.getNumberOfChampionships())
return teamA;
else
return teamB;
}
return 0;
}
}
BasketBallTeam.java
public class BasketBallTeam {
String name;
double salaryInMillions;
int numberOfWins;
int numberOfLosses;
int numberOfChampionships;
boolean playoffTeam;
public BasketBallTeam(String name, double salaryInMillions, int numberOfWins, int numberOfLosses,
int numberOfChampionships, boolean playoffTeam) {
this.name = name;
this.salaryInMillions = salaryInMillions;
this.numberOfWins = numberOfWins;
this.numberOfLosses = numberOfLosses;
this.numberOfChampionships = numberOfChampionships;
this.playoffTeam = playoffTeam;
}
public double getSalaryInMillions() {
return salaryInMillions;
}
public void setSalaryInMillions(double salaryInMillions) {
this.salaryInMillions = salaryInMillions;
}
public int getNumberOfWins() {
return numberOfWins;
}
public void setNumberOfWins(int numberOfWins) {
this.numberOfWins = numberOfWins;
}
public int getNumberOfLosses() {
return numberOfLosses;
}
public void setNumberOfLosses(int numberOfLosses) {
this.numberOfLosses = numberOfLosses;
}
public int getNumberOfChampionships() {
return numberOfChampionships;
}
public void setNumberOfChampionships(int numberOfChampionships) {
this.numberOfChampionships = numberOfChampionships;
}
public boolean isPlayoffTeam() {
return playoffTeam;
}
public void setPlayoffTeam(boolean playoffTeam) {
this.playoffTeam = playoffTeam;
}
public String toString() {
return name;
}
}