Here is the code ;-....
import java.util.*;
class Student implements Comparable {
String firstname, lastname;
int StudentId;
double GPA;
public Student() {
System.out.println("this is the unparamaterized constructor");
}
public Student(String firstname, String lastname, int StudentId, double GPA) {
if (firstname == null || lastname == null || StudentId == 0 || GPA == 0.0) {
throw new IllegalArgumentException();
}
this.firstname = firstname;
this.lastname = lastname;
this.StudentId = StudentId;
this.GPA = GPA;
}
public String firstname() {
return firstname;
}
public String lastname() {
return lastname;
}
public int StudentId() {
return StudentId;
}
public double GPA() {
return GPA;
}
public int compareTo(Object o) {
double f = GPA - ((Student) o).GPA;
if (f == 0.0) {
return 0;
} else if (f < 0.0)
return -1;
else
return 1;
}
}
public class ComparableTest extends Student {
public static void main(String a[]) {
TreeSet free = new TreeSet();
free.add(new Student("gaurav", "yadav", 26, 4.0));
free.add(new Student("paras", "yadav", 34, 3.0));
free.add(new Student("suman", "yadav", 65, 9.0));
free.add(new Student("ram", "saxena", 67, 7.8));
free.add(new Student("nitasha", "kuntal", 74, 7.9));
Object[] free2 = free.toArray();
Student s;
for (Object obj: free2) {
s = (Student) obj;
System.out.printf("name=%s %s id=%d GPA=%.1f\n", s.firstname(), s.lastname(), s.StudentId(), s.GPA());
}
}
}