I've been working on a linked list, but have hit a bit of a snag. The list is supposed to be a representation of athlete objects containing their names and scores. The athlete class was supplied to me, and I am therefore not supposed to change it. I've completed most of the competition class, but according to the rubric am now supposed to print the top 3 scores from the list. I've tried a couple different approaches, but have had no luck. Does anyone have any idea as to how I could accomplish this? Below is the source code, the Competition and CompetitionDriver classes are ones of which I have developed. By the way, this is for a university course. Thanks to anyone with suggestions!
Athlete.java:
public class Athlete implements Comparable {
private String name;
private int score;
public Athlete (String n, int s) {
name = n;
score = s;
}
public int getScore() {
return score;
}
public String toString() {
return name;
}
public int compareTo(Object other) {
return (score - ((Athlete)other).getScore());
}
}
Competition.java:
public class Competition {
private Athlete current;
private Competition next;
public Competition() {
current = null;
next = null;
}
public Competition(Athlete currentIn, Competition nextIn) {
current = currentIn;
next = nextIn;
}
public void addToEnd(Athlete input) {
if (current != null) {
Competition temp = this;
while (temp.next != null) {
temp = temp.next;
}
temp.next = new Competition(input, null);
}
else {
current = input;
}
}
public void print() {
Competition temp = this;
while(temp != null) {
System.out.println("\nAthlete name: "
+ temp.current.toString()
+ "\nAthlete score: "
+ temp.current.getScore());
temp = temp.next;
}
}
}
CompetitionDriver.java:
public class CompetitionDriver {
public static void main(String[] args) {
Competition competition = new Competition();
competition.addToEnd(new Athlete("Jane", 7));
competition.addToEnd(new Athlete("Mark", 9));
competition.addToEnd(new Athlete("Mary", 6));
competition.addToEnd(new Athlete("Eve", 2));
competition.addToEnd(new Athlete("Dan", 15));
competition.addToEnd(new Athlete("Adam", 4));
competition.addToEnd(new Athlete("Bob", 3));
competition.addToEnd(new Athlete("Kathy", 8));
competition.addToEnd(new Athlete("John", 5));
competition.addToEnd(new Athlete("Rob", 1));
competition.print();
competition.printTop();
}
}