class student implements Comparable // must implement Comparable to work
{private String first;
private String last;
private double avg;
public student(String f, String l, double a)
{first= f;
last = l;
avg = a;
}
public String toString()
{return first + " " + last;
}
// had to use Object in compareTo to implement Comparable
public int compareTo(Object other)
{if (avg < ((student)other).avg) // this will prioritize from
return 1; // highest avg to lowest
else // in a PriorityQueue
return 0; // switch sign to < and it goes other way
// I think because its set to remove Min
}
}
______________________________________________________________________________
import java.io.*;
import java.util.*;
public class test2
{public static input in = new input();
public static void main(String[] args) throws IOException
{
PriorityQueue q = new PriorityQueue();
q.add(new student("Joe","Smith",78));
q.add(new student("Tom","Jones",90));
q.add(new student("Helen","Hayes",54));
q.add(new student("Vera","Miles",88));
q.add(new student("Kelly","Rich",66));
for (student s : q)
{System.out.println(s); //comes out in what appears to be a random order
}
System.out.println();
System.out.println();
System.out.println();
while (q.size() != 0)
{student s = q.remove(); // removes according to priority established by
System.out.println(s); // compareTo function
}
}
}