import java.util.*; import java.io.*; public class CodeBreaker { // declare instance variables here DBToolV realWordDB; DBToolV codeWordDB; public CodeBreaker(boolean newdb) { //if true, create new disk based hashtables if(newdb) { realWordDB = new DBToolV("rwDB", true); codeWordDB = new DBToolV("cwDB", true); //if false, use existing disk based hashtables } else { realWordDB = new DBToolV("rwDB", false); codeWordDB = new DBToolV("cwDB", false); } } public void closeDBs() { realWordDB.close(); codeWordDB.close(); } // This is for part (B) // Input: a coded sentence // Output: a decoded English sentence public String translateSentence (String codedSentence) throws NoSuchElementException { StringBuffer words = new StringBuffer(); StringTokenizer st = new StringTokenizer(codedSentence, " "); while (st.hasMoreTokens()) { // fill in code to parse the input coded sentence and translate it words.append(codeWordDB.getDups(st.nextToken() + " " + st.nextToken()) + " "); } return words.toString(); } // This is for part (C) // Input: an English sentence // Output: a coded sentence public String codeSentence (String sentence) throws NoSuchElementException { StringBuffer code = new StringBuffer(); StringTokenizer st = new StringTokenizer(sentence, " "); while (st.hasMoreTokens()) { // fill in code to parse the input sentence and code it code.append(realWordDB.getDups(st.nextToken()) + " "); } return code.toString(); } // Read in the codes and their translations; store in an appropriate data structure public void buildTranslaterFromFile(String fileName) { String str; String cw; String rw; try { BufferedReader fin = new BufferedReader(new FileReader (fileName)); while ((str=fin.readLine())!=null) { StringTokenizer st = new StringTokenizer(str, "|"); // fill in code to read the file into the data structure cw = st.nextToken(); rw = st.nextToken(); codeWordDB.putDups(cw, rw); realWordDB.putDups(rw, cw); } fin.close(); System.out.println("Codes contained in " + fileName + " stored in data structure.\n"); } catch (Exception ioe) { System.err.println("CodeBreaker: " + ioe.toString()); System.exit(1); } } // this is code for the input/output // it lets the user switch between coding and translating sentences // NOTE: THIS DOES NOT WORK IN TEXTPAD IF YOU HAVE // AUTOMATIC CAPTURE SET -- YOU HAVE TO UNSET THIS // IN THE CONFIGURE MENU. YOU KNOW YOU DID IT RIGHT // WHEN YOU SEE THE BLACK BACKGROUND WHEN YOU RUN THE CODE. public void tryCode () throws Exception { String inputLine =""; InputStreamReader isr = new InputStreamReader(System.in); BufferedReader keyboard = new BufferedReader(isr); System.out.println("Type C^D or quit to exit, type switch to change modes."); boolean translate = true; System.out.print("Translate Sentence > "); try { while ((inputLine = keyboard.readLine()) != null) { if (inputLine.equals("quit")) return; if (inputLine.equals("switch")) { if (translate == true) { translate = false; System.out.print("Code Sentence > "); } else { translate = true; System.out.print("Translate Sentence > "); } } else if (translate) { System.out.println("Translation is: " + this.translateSentence(inputLine)); System.out.print("Translate Sentence > "); } else { System.out.println("Coded sentence is: " + this.codeSentence(inputLine)); System.out.print("Code Sentence > "); } } } catch (NoSuchElementException e) { System.out.println("Your input does not exist in the dictionary, try again!"); this.tryCode(); } } // Try out some translations public static void main (String args[]) { CodeBreaker code; // if 1 command line argument is passed, // instantiate new CodeBreaker object using new disk based hashtables // and store code words listed in text file in hashtables, if (args.length == 1) { code = new CodeBreaker(true); code.buildTranslaterFromFile(args[0]); //Otherwise, instantiate new CodeBreaker object using existing disk //based hashtables. } else { code = new CodeBreaker(false); } try { code.tryCode(); } catch (Exception e) { System.err.println("CodeBreaker: " + e.toString()); System.exit(1); } code.closeDBs(); } }