public class Student implements Comparable { String name; double gpa; public Student() {} /** * Initializes this Student object from a specified name and gpa. * * @param name - the specified name. * @param gpa - the specified gpa. * */ public Student (String name, double gpa) { this.name = name; this.gpa = gpa; } // constructor /** * Compares this Student object with a specified object. * The comparison is alphabetical; for two objects with the same name, * the comparison is by grade point averages. * * @param otherStudent - the specified Student object that this Student * object is being compared to. * * @return -1, if this Student object’s name is alphabetically less than * otherStudent’s name, or if the names are equal and this * Student object’s grade point average is greater than * otherStudent’s grade point average; * 0, if this Student object’s name and grade point average * are the same as otherStudent’s name and grade point * average; * 1, if this Student object’s name is alphabetically greater * than otherStudent’s name, or if the names are equal and * this Student object’s grade point average is less than * otherStudent’s grade point average. * */ public int compareTo (Student otherStudent) { if (name.compareTo (otherStudent.name) < 0) return -1; if (name.compareTo (otherStudent.name) > 0) return 1; if (gpa > otherStudent.gpa) return -1; if (gpa < otherStudent.gpa) return 1; return 0; } // method compareTo /** * Determines if this Student object’s name and grade point average are * the same as some specified object. * * @param obj - the specified object that this Student object is being * compared to. * * @return true - if obj is a Student object and this Student object has the * same name and grade point average as obj. * */ public boolean equals (Object obj) { return (obj instanceof Student) && name.equals (((Student)obj).name) && gpa == ((Student)obj).gpa; } // method equals /** * Returns a String representation of this Student object. * * @return a String representation of this Student object: name, blank, * grade point average. * */ public String toString() { return name + " " + gpa; } // method toString } // class Student