public class ComparableDemo2 { public static void main(String[] args) { Comparable student1 = new HighSchoolStudent(3.0, "Angie", 56); Comparable student2 = new HighSchoolStudent(2.9, "Bill", 72); Comparable student3 = new ElementarySchoolStudent(100, "Colleen"); Comparable student4 = new ElementarySchoolStudent(12, "David"); // System.out.println(student1.getName()); // compile error since student1 is "cloaked" in this program as a Comparable reference and getName isn't a method listed in the Comparable interface System.out.println(((HighSchoolStudent) student1).getName()); // valid since student1 is temporarily casted to the HighSchoolStudent that it really is "deep down" if (student1.compareTo(student2) > 0) { System.out.println(student1.toString() + " IS GREATER THAN " + student2.toString()); // toString is optional } else if (student1.compareTo(student2) < 0) { System.out.println(student1 + " IS LESS THAN " + student2); } else { System.out.println(student1 + " IS EQUAL TO " + student2); } System.out.println("BEST HIGH SCHOOL STUDENT IS: " + compareStudents(student1, student2)); // use of toString is allowed since it is inherited from Object class System.out.println("BEST ELEMENTARY STUDENT IS: " + compareStudents(student3, student4)); // What if student4 were replaced by student2? }// end of main method public static Comparable compareStudents(Comparable studentA, Comparable studentB) // this method is flexible, it can be passed HS or ElementarySchoolStudent objects! { // in fact, it could be passed objects from any of the hundreds of classes that realize the Comparable interface like String, Integer, etc. if (studentA.compareTo(studentB) > 0) { return studentA; } else if (studentA.compareTo(studentB) < 0) { return studentB; } return studentA; // a reference to a whole HS or ElementarySchoolStudent object is being returned (valid since those classes implement Comparable) } }// end of ComparableDemo2 class //////////////////////////////////////////////////////// class HighSchoolStudent implements Comparable { public HighSchoolStudent(double gpa, String name, int height) { myGPA = gpa; myName = name; myHeight = height; } public String getName() { return myName; } public int compareTo(Object other) { if (this.myGPA > ((HighSchoolStudent) other).myGPA) // How would you modify this method to use height as a tiebreaker if GPA's are the same { return 1; } else if (this.myGPA < ((HighSchoolStudent) other).myGPA) // this is optional, could be: else if (myGPA < ((HighSchoolStudent)... { return -1; } return 0; // could place this in an else clause // return this.myHeight - ((HighSchoolStudent) other).myHeight; // This single line return statement wouldn't work for myGPA since myGPA is a double and returning the double 1.0 isn't the same as returning an int 1 for example // return this.myName.compareTo(((HighSchoolStudent) other).myName); // making use of the String compareTo method within the HSStudent compareTo 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 String getName() { return myName; } 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; }