package prog11; import prog10.BTree; import prog11.*; import java.io.File; import java.util.Scanner; import prog02.UserInterface; import prog02.ConsoleUI; import prog02.GUI; import java.util.Map; import java.util.TreeMap; import java.util.Arrays; import java.util.ArrayList; import prog08.Tree; public class Jumble { /** * Sort the letters in a word. * @param word Input word to be sorted, like "computer". * @return Sorted version of word, like "cemoptru". */ public static String sort (String word) { char[] sorted = new char[word.length()]; for (int i = 0; i < word.length(); i++) sorted[i] = word.charAt(i); Arrays.sort(sorted); return new String(sorted, 0, word.length()); } public static void main (String[] args) { UserInterface ui = new GUI(); Map> map = new ChainedHashTable>(); Scanner in = null; do { try { in = new Scanner(new File(ui.getInfo("Enter word file."))); } catch (Exception e) { System.out.println(e); System.out.println("Try again."); } } while (in == null); int n = 0; while (in.hasNextLine()) { ArrayList words = new ArrayList(); String word = in.nextLine(); if (n++ % 1000 == 0) System.out.println(word + " sorted is " + sort(word)); if(map.get(sort(word)) == null) { words.add(word); map.put(sort(word), words); } else map.get(sort(word)).add(word); // EXERCISE: Insert an entry for word into map. // What is the key? What is the value? } String sorted; while (true) { String jumble = ui.getInfo("Enter a jumble to unjumble it or press cancel to solve for the pun."); if (jumble == null) break; else sorted = sort(jumble); // EXERCISE: Look up the jumble in the map. // What key do you use? ArrayList word = map.get(sorted); if (word == null) ui.sendMessage("No match for " + jumble); else ui.sendMessage(jumble + " unjumbled is " + word); } while(true){ //SOLVE PUN String clue = ui.getInfo("Please enter the clue letters."); while(clue == null) { ui.sendMessage("Please try again."); clue = ui.getInfo("Please enter the clue letters."); } clue = sort(clue); break; } } }