Looking for assistance for following issue running into have following comparable method below that want to print largest value of goals scored from my Hockey Player ArrayList. I'm unable to store values from the for each loop successfully to pass it to the comparable method. Any suggestion what I'm doing wrong?
public static Comparable maximum(Comparable first, Comparable second, Comparable third) {
if (first.compareTo(second) > 0) {
if (first.compareTo(third) > 0) {
return first;
}
return third;
} else if (second.compareTo(third) > 0) {
return second;
}
return third;
}
for (HockeyPlayer hp : hockeyPlayerList){
int first = ((HockeyPlayer) hp).getGoalsScored(); {
first = player1;
first = player2;
first = player3;
first = player4;
}
System.out.println("The largest string is: " + Driver.maximum(player1,player2,player3));
}
I generally agree with "use java.util.Collections.sort(hockeyPlayerList);" comment, but just if you have to implement your own maximum...
Assuming your final goal is to be able to do this:
The function could looks like this:
And inside your
Player
class you will have to have atoString
function printing out a meaningful info about the player (System.out.println
will calltoString
function for an instance of thePlayer
class returned by amaximum
function). Something like (I am giving and example, use actual fields you have in Player function):Also the following fragment of the code makes no sense:
not sure what you were trying to accomplish there