import javax.swing.*; import java.util.*; public class MapOps { public static Object insertInMap(Map L, Student item) { // precond: key is name field of Student object //postcond: item is added if unique. Returns object formerly associated with key // or null if key not in map return null; }//=== insertInMap public static boolean deleteFromMap(Map L, String name) { // precond: key is name field of Student object //postcond: s is deleted, if found. Returns true if deleted, otherwise false return false; }//=== deleteFromMap public static double averageGPA(final Map L) { // precond: key is name field of Student object //postcond: returns average of GPAs or -77 if no objects return -77; } //=== averageGPA public static void deleteBelowAvg(Map L) { //precond: method averageGPA (defined above) works // key is name field of Student object // postcond: all elements with GPAs < mean are deleted } //=== deleteBelowAvg public static void minToMax(Map L) { // postcond: minimum GPA found and changed to 1.0 greater than max } //=== minToMax public static void destroy(Map L) { //postcondtion: every element of L is deleted L.clear(); } //=== destroy //=============================================================================================================== public static String TAB = "\t"; public static final boolean HASH_Map = false, TREE_Map = true; public static final int MapSIZE = 8; public static void main(String[] args) { MyTerminal.cls(); System.out.println("\t\t\tMap 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 MapType = HASH_Map; String title = ( MapType ? "TreeMap OPERATIONS" : "HashMap OPERATIONS" ); Map A = makeMap(MapSIZE, MapType); for (;;) { MyTerminal.cls(); print(A); String strChoice; strChoice = JOptionPane.showInputDialog(null, new String[] {"1. Insert into Map", "2. Delete from Map", "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 Map", "7. New Map", "0. Quit"} , title, JOptionPane.INFORMATION_MESSAGE); int choice; if ( strChoice.length() > 1 || strChoice.compareTo("0") < 0 || strChoice.compareTo("7") > 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("7") ) A = makeMap(MapSIZE, MapType); else break; }//=== for System.out.println("\nProgram over"); System.exit(0); } // main static void exec(Map 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("Map after " + op + ":"); print(A); } //=== exec static void averageGPADriver(final Map A) { String title = "FINDING AVERAGE GPA FOR Map"; double GPA = averageGPA(A); String msg = "Average GPA = " + GPA; JOptionPane.showMessageDialog(null, msg, title, JOptionPane.INFORMATION_MESSAGE); } //=== searchDriver static void minToMaxDriver(Map A) { minToMax(A); print(A); JOptionPane.showMessageDialog(null, "", "minToMax RESULTS", JOptionPane.INFORMATION_MESSAGE); }//=== minToMaxDriver static void deleteBelowAvgDriver(final Map 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(Map A) { String title = "INSERTING STUDENT IN Map"; 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); int size = A.size(); boolean keyThere = A.containsKey(name); Object result = insertInMap(A, std); if ( A.size() > size || keyThere ) msg = "Insertion was successful."; else msg = "Insertion failed."; JOptionPane.showMessageDialog(null, msg, "Insertion results", JOptionPane.INFORMATION_MESSAGE); } //=== insertDriver static void deleteDriver(Map A) { String title = "DELETING FROM Map OF " + A.size() + " ITEMS"; String name = JOptionPane.showInputDialog(null, "Enter name", title, JOptionPane.INFORMATION_MESSAGE); String strGPA; String msg = ""; System.out.println("m.containsKey(" + name + ") = " + A.containsKey(name)); if ( deleteFromMap(A, name) ) msg = "Deletion successful: " + msg; else msg = "Deletion failed: " + msg; print(A); JOptionPane.showMessageDialog(null, msg, "Deletion results", JOptionPane.INFORMATION_MESSAGE); } //=== deleteDriver static Map makeMap(int n, boolean listType) { Map A; if ( listType == HASH_Map ) A = new HashMap(n); else A = new TreeMap(); Student.setListVars(); for (int k = 0; k < n; k++) { Student std = new Student(); A.put( std.getName(), std ); } return A; } static void print(final Map A) { System.out.println("\ncount" + TAB + "nameKey-->" + TAB + "(name, GPA, YOB)"); int count = 1; Set mSet = A.keySet(); for (Iterator iter = mSet.iterator(); iter.hasNext(); count++) { String nameKey = (String) iter.next(); System.out.println(count + TAB + nameKey + TAB + TAB + A.get(nameKey) ); } }//=== print } //=== CollectionOps class Student implements Comparable { private final String name; private double GPA; private int YOB; 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), 1987 + rGen.nextInt(4)); listIndex = (listIndex + 1) % nameList.length; if ( listIndex == 0 ) listTraversals += 1; } public Student(String name, double GPA, int YOB) { this.name = name; this.GPA = GPA; this.YOB = YOB; } public Student(String name, double GPA) { this.name = name; this.GPA = GPA; this.YOB = 1987 + rGen.nextInt(4); } public String getName() { return name; } public double getGPA() { return GPA; } public int getYOB() { return YOB; } public String toString() { return "(" + name + ", " + GPA + ", " + YOB + ")"; } 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