import javax.swing.*; import java.util.*; public class SetOps { public static boolean insertInSet(Set L, Student item) { //postcond: item is added if unique. Returns true if inserted, otherwise false return L.add(item); }//=== insertInSet public static boolean deleteFromSet(Set L, Student s) { //postcond: s is deleted, if found. Returns true if deleted, otherwise false return L.remove(s); }//=== deleteFromSet public static double averageGPA(final Set L) { //postcond: returns average of GPAs return -77.0; } //=== averageGPA public static void deleteBelowAvg(Set L) { //precond: method averageGPA (defined above) works // postcond: all elements with GPAs < mean are deleted } //=== deleteBelowAvg public static void minToMax(Set L) { // postcond: minimum GPA found and changed to 1.0 greater than max } //=== minToMax public static void destroy(Set L) { //postcondtion: every element of L is deleted L.clear(); } //=== destroy //=============================================================================================================== public static String TAB = "\t"; public static final boolean HASH_SET = false, TREE_SET = true; public static final int setSIZE = 8; public static void main(String[] args) { MyTerminal.cls(); System.out.println("\t\t\tSet Interface Driver Program\n"); System.out.println("To use this program succesfully:\n\t1.\tStretch BlueJ Terminal Window vertically"); System.out.println("\t2.\tMinimize all other windows."); MyTerminal.readString("\nPress ..."); boolean setType = HASH_SET; String title = ( setType ? "TreeSet OPERATIONS" : "HashSet OPERATIONS" ); Set A = makeSet(setSIZE, setType); for (;;) { MyTerminal.cls(); print(A); String strChoice; strChoice = JOptionPane.showInputDialog(null, new String[] {"1. Insert into set", "2. Delete from set", "3. Compute average GPA", "4. Delete Students with below average GPAs", "5. Find min GPA and make it 1.0 greater than max", "6. Destroy set", "7. New set", "0. Quit"} , title, JOptionPane.INFORMATION_MESSAGE); int choice; if ( strChoice.length() > 1 || strChoice.compareTo("0") < 0 || strChoice.compareTo("6") > 0 ) JOptionPane.showMessageDialog( null, "ERROR: enter number in [0, 7]", "INPUT ERROR", JOptionPane.ERROR_MESSAGE); else if ( strChoice.compareTo("0") > 0 && strChoice.compareTo("7") < 0 ) exec(A, Integer.parseInt(strChoice)); else if ( strChoice.equals("6") ) A = makeSet(setSIZE, setType); else break; }//=== for System.out.println("\nProgram over"); System.exit(0); } // main static void exec(Set A, int choice) { String op = ""; switch ( choice ) { case 1: insertDriver(A); op = "insertion"; break; case 2: deleteDriver(A); op = "deletion"; break; case 3: averageGPADriver(A); op = "average GPA"; break; case 4: deleteBelowAvgDriver(A); op = "delete below average"; break; case 5: minToMaxDriver(A); op = "min to max"; break; case 6: destroy(A); op = "destroy"; break; default: break; } System.out.println("Set after " + op + ":"); print(A); } //=== exec static void averageGPADriver(final Set A) { String title = "FINDING AVERAGE GPA FOR SET"; double GPA = averageGPA(A); String msg = "Average GPA = " + GPA; JOptionPane.showMessageDialog(null, msg, title, JOptionPane.INFORMATION_MESSAGE); } //=== searchDriver static void minToMaxDriver(Set A) { minToMax(A); print(A); JOptionPane.showMessageDialog(null, "", "minToMax RESULTS", JOptionPane.INFORMATION_MESSAGE); }//=== minToMaxDriver static void deleteBelowAvgDriver(final Set A) { String title = "Deleting students: GPAs below mean"; String msg = "mean GPA before deletion: " + averageGPA(A); deleteBelowAvg(A); print(A); JOptionPane.showMessageDialog(null, msg, title, JOptionPane.INFORMATION_MESSAGE); } //===deleteBelowAvgDriver static void insertDriver(Set A) { String title = "INSERTING STUDENT IN SET"; String name = JOptionPane.showInputDialog(null, "Enter name", title, JOptionPane.INFORMATION_MESSAGE); String strGPA; String msg = ""; for (;;) { strGPA = JOptionPane.showInputDialog(null, msg + "Enter GPA", title, JOptionPane.INFORMATION_MESSAGE); if ( strGPA.charAt(0) >= '0' && strGPA.charAt(0)<= '9' || strGPA.charAt(0) == '-' ) break; else msg = "INVALID first character: " + strGPA.charAt(0) + " -- "; } double GPA = Double.parseDouble(strGPA); String strYOB; msg = ""; Student std = new Student(name, GPA); if ( insertInSet(A, std) ) msg = "Insertion was successful."; else msg = "Insertion failed."; JOptionPane.showMessageDialog(null, msg, "Insertion results", JOptionPane.INFORMATION_MESSAGE); } //=== insertDriver static void deleteDriver(Set A) { String title = "DELETING FROM SET OF " + A.size() + " ITEMS"; String name = JOptionPane.showInputDialog(null, "Enter name", title, JOptionPane.INFORMATION_MESSAGE); String strGPA; String msg = ""; for (;;) { strGPA = JOptionPane.showInputDialog(null, msg + "Enter GPA", title, JOptionPane.INFORMATION_MESSAGE); if ( strGPA.charAt(0) >= '0' && strGPA.charAt(0)<= '9' || strGPA.charAt(0) == '-' ) break; else msg = "INVALID first character: " + strGPA.charAt(0) + " -- "; } double GPA = Double.parseDouble(strGPA); Student std = new Student(name, GPA); msg = "" + std.hashCode(); System.out.println("s.contains(" + std + ") = " + A.contains(std)); if ( deleteFromSet(A, std) ) msg = "Deletion successful: " + msg; else msg = "Deletion failed: " + msg; print(A); JOptionPane.showMessageDialog(null, msg, "Deletion results", JOptionPane.INFORMATION_MESSAGE); } //=== deleteDriver static Set makeSet(int n, boolean listType) { Set A; if ( listType == HASH_SET ) A = new HashSet(n); else A = new TreeSet(); Student.setListVars(); for (int k = 0; k < n; k++) A.add( new Student() ); return A; } static void print(final Set A) { System.out.println("\nindex" + TAB + "(name, GPA, hashCode)"); int pos = 0; for (Iterator iter = A.iterator(); iter.hasNext(); pos++) { System.out.println(pos + TAB + iter.next() ); } }//=== print } //=== CollectionOps class Student implements Comparable { private final String name; private double GPA; private static Random rGen = new Random(17); private static final String[] nameList = {"mm", "jj", "bb", "pp"}; private static int listTraversals = 0; private static int listIndex = 0; public static void setListVars() { listTraversals = listIndex = 0; rGen.setSeed(17); } public Student() { this(nameList[listIndex] + (listTraversals + 1), 1 + rGen.nextInt(4) + 0.2* rGen.nextInt(5)); listIndex = (listIndex + 1) % nameList.length; if ( listIndex == 0 ) listTraversals += 1; } public Student(String name, double GPA) { this.name = name; this.GPA = GPA; } public String getName() { return name; } public double getGPA() { return GPA; } public String toString() { return "(" + name + ", " + GPA + ", " + hashCode() + ")"; } public boolean equals(Object obj) { if ( obj == null ) return false; else { Student std = (Student) obj; return name.equals(std.name) && (GPA == std.GPA); } } public int hashCode() { int gpa = (int)(100.0*GPA); return 1000*name.hashCode() + gpa; } public int compareTo(Object obj) { if ( obj == null ) return -77; Student other = (Student) obj; int gpaDiff = (int)(10.0*GPA - 10.0*other.GPA); return 100*name.compareTo(other.name) + gpaDiff; } } // === end of Student class