/* Post: the string input is parsed to an int if it meets specs */ public void checkAndUpdateArray() { if (arraySizeEntry.getText().length() > 0) { boolean isAnInt = true; String entry = arraySizeEntry.getText(); entry.trim(); for (int i = 0; i < entry.length(); i++) { if (entry.charAt(i) < 48 || entry.charAt(i) > 57 || entry.equals("0")) isAnInt = false; } if (isAnInt) { arraySize = Integer.parseInt(entry); if (arraySize >= 25 && arraySize <= 300) { if (isWorstCase) populateWorstCaseArray(); else populateRandomArray(); VisualSort.updateArrays(); } } } }
/* Pre: action must be performed for this method to fire Post: the appropriate action is selected depending on the source */ public void actionPerformed(ActionEvent event) { if (event.getSource() == chooseCase) { JComboBox cb = (JComboBox) event.getSource(); String caseChosen = (String) cb.getSelectedItem(); if (caseChosen.equals("Worst Case")) isWorstCase = true; else isWorstCase = false; if (arraySize >= 25 && arraySize <= 300) { if (isWorstCase) populateWorstCaseArray(); else populateRandomArray(); VisualSort.updateArrays(); } } else if (event.getSource() == chooseTopAlgo) { JComboBox cb = (JComboBox) event.getSource(); String caseChosen = (String) cb.getSelectedItem(); if (caseChosen.equals("Selection Sort")) { selectionIsOnTop = true; insertionIsOnTop = false; bubbleIsOnTop = false; mergeIsOnTop = false; } else if (caseChosen.equals("Insertion Sort")) { selectionIsOnTop = false; insertionIsOnTop = true; bubbleIsOnTop = false; mergeIsOnTop = false; } else if (caseChosen.equals("Bubble Sort")) { selectionIsOnTop = false; insertionIsOnTop = false; bubbleIsOnTop = true; mergeIsOnTop = false; } else if (caseChosen.equals("Merge Sort")) { selectionIsOnTop = false; insertionIsOnTop = false; bubbleIsOnTop = false; mergeIsOnTop = true; } } else if (event.getSource() == chooseBottomAlgo) { JComboBox cb = (JComboBox) event.getSource(); String caseChosen = (String) cb.getSelectedItem(); if (caseChosen.equals("Selection Sort")) { selectionIsOnBottom = true; insertionIsOnBottom = false; bubbleIsOnBottom = false; mergeIsOnBottom = false; } else if (caseChosen.equals("Insertion Sort")) { selectionIsOnBottom = false; insertionIsOnBottom = true; bubbleIsOnBottom = false; mergeIsOnBottom = false; } else if (caseChosen.equals("Bubble Sort")) { selectionIsOnBottom = false; insertionIsOnBottom = false; bubbleIsOnBottom = true; mergeIsOnBottom = false; } else if (caseChosen.equals("Merge Sort")) { selectionIsOnBottom = false; insertionIsOnBottom = false; bubbleIsOnBottom = false; mergeIsOnBottom = true; } } else if (event.getSource() == run) { if (arraySize >= 25 && arraySize <= 300) VisualSort.run(); } else { checkAndUpdateArray(); } }