package CollectionEx; // representing a class at HCC // Comparable is a generic interface, you have to say what you can be compared to public class HCCClass implements Comparable { private String program; // which program e.g. CIS, ACCT, ENG private int coursenum; // the course number e.g. 102, 214 private String title; // the title e.g. Intro to Information Systems, Programming II: Java private String description; // the obligatory course description private int credits; // how many credits it is worth // constructors public HCCClass() { program = "XXX"; coursenum = 0; title = "Course Name"; description = "Course Description"; credits = 0; } public HCCClass(String program, int coursenum, String title, int credits) { this(); setProgram(program); setCoursenum(coursenum); setTitle(title); setCredits(credits); } // accessors and mutators public String getProgram() { return program; } public void setProgram(String program) { this.program = program; } public int getCoursenum() { return coursenum; } public void setCoursenum(int coursenum) { this.coursenum = coursenum; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public int getCredits() { return credits; } public void setCredits(int creditsval) { if (creditsval > 0) { this.credits = creditsval; } } // utilities public String toString() { // use format from String to add leading zeroes for 3-digit class number // just to be fiddly String num = String.format("%03d", getCoursenum()); return getProgram() + " " + num + ": " + getTitle() + " (" + getCredits() + ")"; } // for Comparable // return positive number if this is bigger // return 0 if they are equal // return negative number if other is bigger public int compareTo(HCCClass other) { // bigger than any null class if (other == null) { return 1; } // compare program names based on String alphabetical using compareTo from String int programDiff = getProgram().compareTo(other.getProgram()); // if different programs, use alphabetical comparison as result if (programDiff != 0) { return programDiff; } //if programs same compare by course number -- based on size of int return getCoursenum() - other.getCoursenum(); } }