public class ComparableDemo1 { public static void main(String[] args) { HighSchoolStudent student1 = new HighSchoolStudent(3.0, "Angie", 56); HighSchoolStudent student2 = new HighSchoolStudent(2.9, "Bill", 72); if (student1.compareTo(student2) > 0) { System.out.println(student1 + " IS GREATER THAN " + student2); } else if (student1.compareTo(student2) < 0) { System.out.println(student1 + " IS LESS THAN " + student2); } else { System.out.println(student1 + " IS EQUAL TO " + student2); } ElementarySchoolStudent student3 = new ElementarySchoolStudent(100, "Colleen"); ElementarySchoolStudent student4 = new ElementarySchoolStudent(12, "David"); if (student3.compareTo(student4) > 0) { System.out.println(student3 + " IS GREATER THAN " + student4); } else if (student3.compareTo(student4) < 0) { System.out.println(student3 + " IS LESS THAN " + student4); } else { System.out.println(student3 + " IS EQUAL TO " + student4); } }// end of main method }// end of ComparableDemo1 class //////////////////////////////////////////////////////// class HighSchoolStudent implements Comparable { public HighSchoolStudent(double gpa, String name, int height) { myGPA = gpa; myName = name; myHeight = height; } public int compareTo(Object other) { if (this.myGPA > ((HighSchoolStudent) other).myGPA) { return 1; } else if (this.myGPA < ((HighSchoolStudent) other).myGPA) { return -1; } return 0; // return this.myHeight - ((HighSchoolStudent) other).myHeight; // Why won't this single line return statement work for myGPA? // return this.myName.compareTo(((HighSchoolStudent) other).myName); // Is compareTo a recursive method? } public String toString() { return "name = " + myName + " GPA = " + myGPA + " height = " + myHeight; } private double myGPA; private String myName; private int myHeight; // in inches } //////////////////////////////////////////////////////// class ElementarySchoolStudent implements Comparable { public ElementarySchoolStudent(int workEthic, String name) { myWorkEthic = workEthic; myName = name; } public int compareTo(Object other) { return this.myWorkEthic - ((ElementarySchoolStudent) other).myWorkEthic; } public String toString() { return "name = " + myName + " work ethic = " + myWorkEthic; } private int myWorkEthic; private String myName; }