Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Class below - a morse code decode/encode method: import morse.*; public class TelegraphOperator { private MorseTree decoder; private String[] encoder; /* * Constructor accepts an
Class below - a morse code decode/encode method:
import morse.*; public class TelegraphOperator { private MorseTree decoder; private String[] encoder; /* * Constructor accepts an already-constructed instance of * MorseTree (in "decoder") and two arrays -- one of * chars, one of strings, where the same index for each * array correspond to each other (i.e., the char * at symbol[i] matches the morse-code sequence in * morse[i]). */ public TelegraphOperator(MorseTree decoder, char symbols[], String morse[]) { } /* * PURPOSE: * * Using the MorseTree instance provided when to the constructor, * convert the sequence of dots, dashes and spaces in codedMessage * nto a human-readable version of the message. A single space * separates morse-code sequences; two spaces indicate * not only the end of a morse-code sequence but also * the end of a word. For example: * * "... --- ..." is "sos" * * but: * * "... --- ..." is "so s" (notice space after the 'o') * * The codedMessage string can be tokenized into an array * of strings using the instructions provided within the PDF * description for Assignment #4. * * EXAMPLES: * * If codedMessage = "...-- .-.-.- .---- ....-" then * the string returned is "3.14". * * If codedMessage = "..- ...- .. -.-. .-. ..- .-.. --.. ..--.." * then the string returned is "uvic rulz?" * */ public String receiveMessage(String codedMessage) { return ""; } /* * PURPOSE: * * Using the HashMap instance created in the constructor, * convert the sequence of characters and spaces into a * a morse code message. A single space separate the morse code * for each character in message, and two spaces separate * words in the morse-code sequence. For example: * * "sos" is "... --- ..." * * but: * * "so s" is "... --- ..." (notice two spaces after ---) * * EXAMPLES: * If message is "3.141", then the string returned is * "...-- .-.-.- .---- ....-" * * If message is "uvic rulz?" then the string returned is * "..- ...- .. -.-. .-. ..- .-.. --.. ..--.." */ public String sendMessage(String message) { return ""; } }
This is the Mores Tree file required to pass first 7 test:
/* This is used as a binary tree representing the decoding of morse * "dot / dash" sequences (e.g., "." or "-"). (Morse *encoding* is * not done in this class.) * * The idea is that a sequence of dots and dashes can be use to * determine a path from the tree root to some node -- internal * or leaf -- at which the symbol corresponding to that dot/dash * sequence is stored. Rather than naming the subtrees "left" * and "right", there are instead named within a MorseTreeNode as * "dot" and "dash". * * Note: Not every internal node will correspond to the end of some * dot/dash path of a legitimate symbol; therefore if an internal * node stores *null* as the symbol, then this means there is no * morse code sequence from the root to this node that corresponds * to a legitimate symbol. * */ package morse; public class MorseTree { MorseTreeNode root; public MorseTree() { root = new MorseTreeNode(' '); } /* * PURPOSE: * * Given a one-character symbol and its corresponding Morse code * equivalent, update a binary tree to enable decoding of that * equivalent. * * Unlike operations on a binary search tree where an insert() * operation normally results in at most one new node, this * insert might result in more than one. In essence, after * the insertion is complete, there will be a path from the * root to some node (not necessarily a leaf node!) where * the combination of dot and dash edges matches the code * given as the parameter. The node at the end of the path will * be set to contain the symbol. * * * */ public void insertSymbol(char symbol, String code) { MorseTreeNode current = root; for(int i = 0; i < code.length(); i++){ char ch = code.charAt(i); if(ch == '.'){ if(current.dot == null) current.dot = new MorseTreeNode('.'); current = current.dot; } else if(ch == '-'){ if(current.dash == null) current.dash = new MorseTreeNode('-'); current = current.dash; } } current.symbol = symbol; return; } /* * PURPOSE: * * Given some sequence of dots (".") and dashes ("-"), * traverse the binary tree at root from the start to the end * of the sequence. "Traverse" here means follow the "dot" * link if the char in the sequence is the "." character, and * follow the "dash" link if the char in the sequence is the * "-" character. Examine the node at the end of that sequence. * There are three possibilities: * * (1) The node's symbol is not null. In this case, the * morse-code sequence is meaningful, and we return * the symbol at that node. * * (2) The node's symbol *is* null. In this case, the * morse-code sequence is *not* meaningful. We * return null (and the caller of lookupSymbol() must * explicitly check for this possibility. * * (3) The sequence is longer than any path in the * tree. As with (2), this means the morse-code * sequence is not meaningful (i.e., garbage?) and * so we return null. Again as with (2) the caller * of lookupSymbol must */ public Character lookupSymbol(String code) { MorseTreeNode curr = root; for(int i = 0; curr != null && i < code.length(); i++){ char ch = code.charAt(i); if(ch == '.') curr = curr.dot; else if(ch == '-') curr = curr.dash; } if(curr == null || curr == root) return null; else return curr.symbol; }
This is the tester file for this:
import java.util.List; import java.util.LinkedList; import morse.*; public class A4test { private static int testCount = 1; private static boolean testArraySolution = false; private static int stressTestSize = 200; /* itu_symbol[] and itu_morse[] are based on the International * Telecommunication Union's standard for Morse code. * * Reference docuemtns may be found at: * https://www.itu.int/rec/R-REC-M.1677-1-200910-I/en * * This information has been broken into two arrays -- one * of characters, and one of strings. The arrays are exactly * the same size. For example, if index = 2, then the * symbol is 'c', and the morse code for 'c' is at itu_morse[2] * (i.e., "-.-." or "dash dot dash dot"). */ private static char[] itu_symbol = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '?', '!', '(', ')', ':', ';', ',', '\'', '/', '&', '-' }; private static String[] itu_morse = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", ".-.-.-", "..--..", "-.-.--", "-.--.", "-.--.-", "---...", "-.-.-.", "--..--", ".----.", "-..-.", ".-...", "-....-" }; public static void displayResults (boolean passed) { /* Using some ideas from http://bit.ly/2jCNQ1L that helps * identify the line number corresponding to some run-time * code point. Exceptions are needed here (i.e., we need * to throw an exception at the code point of interest). */ if (passed) { System.out.println ("Passed test: " + testCount); } else { System.out.println ("Failed test: " + testCount + " at line " + Thread.currentThread().getStackTrace()[2].getLineNumber() ); System.exit(1); } testCount++; } public static void testEmptyMorseTree() { System.out.println("testEmptyMorseTree"); MorseTree mt = new MorseTree(); displayResults(mt.lookupSymbol("--") == null); } public static void testSimpleMorseTree() { System.out.println("testSimpleMorseTree"); MorseTree mt = new MorseTree(); mt.insertSymbol('e', "."); mt.insertSymbol('t', "-"); displayResults(mt.lookupSymbol(".").equals('e')); displayResults(mt.lookupSymbol("-").equals('t')); displayResults(mt.lookupSymbol(".-") == null); displayResults(mt.lookupSymbol("-.") == null); } public static void testBasicMorseTree() { System.out.println("testBasicMorseTree"); boolean passTest; MorseTree mt = new MorseTree(); for (int i = 0; i < itu_symbol.length; i++) { mt.insertSymbol(itu_symbol[i], itu_morse[i]); } passTest = true; for (int i = 0; i < itu_symbol.length; i++) { if (!mt.lookupSymbol(itu_morse[i]).equals(itu_symbol[i])) { passTest = false; } } displayResults(passTest); passTest = true; for (int i = itu_symbol.length - 1; i <= 0; i--) { if (!mt.lookupSymbol(itu_morse[i]).equals(itu_symbol[i])) { passTest = false; } } displayResults(passTest); } public static void testBasicOperator() { System.out.println("testBasicOperator"); String before, after; MorseTree mt = new MorseTree(); for (int i = 0; i < itu_symbol.length; i++) { mt.insertSymbol(itu_symbol[i], itu_morse[i]); } TelegraphOperator operator = new TelegraphOperator(mt, itu_symbol, itu_morse); before = ".- -... -.-. -..- -.-- --.."; after = operator.receiveMessage(before); displayResults(after.equals("abcxyz")); before = "...-- .-.-.- .---- ....- .---- ..... ----."; after = operator.receiveMessage(before); displayResults(after.equals("3.14159")); before = "..-. --- --- ... -... .- .-.. .-.."; after = operator.receiveMessage(before); displayResults(after.equals("foos ball")); before = "canada"; after = operator.sendMessage(before); displayResults(after.equals("-.-. .- -. .- -.. .-")); before = "mr. dressup"; after = operator.sendMessage(before); displayResults(after.equals( "-- .-. .-.-.- -.. .-. . ... ... ..- .--." )); before = "'call me at 1:00'"; after = operator.sendMessage(before); displayResults(after.equals( ".----. -.-. .- .-.. .-.. -- . .- - .---- ---... -----" + " ----- .----." )); before = ".-.-.-.-.-.-.-.-"; after = operator.receiveMessage(before); displayResults(after.equals("")); before = "... --- ... ..........."; after = operator.receiveMessage(before); displayResults(after.equals(" ")); } public static void testPoemOperator() { System.out.println("testPoemOperator"); String before, after; MorseTree mt = new MorseTree(); for (int i = 0; i < itu_symbol.length; i++) { mt.insertSymbol(itu_symbol[i], itu_morse[i]); } TelegraphOperator operator = new TelegraphOperator(mt, itu_symbol, itu_morse); before = Sonnet18.morse; after = operator.receiveMessage(before); displayResults(after.equals(Sonnet18.text)); before = Sonnet18.text; after = operator.sendMessage(before); displayResults(after.equals(Sonnet18.morse)); } public static void main (String[] args) { try { testEmptyMorseTree(); testSimpleMorseTree(); testBasicMorseTree(); testBasicOperator(); testPoemOperator(); } catch (Exception e) { System.out.println("Your code threw an Exception."); System.out.println("Perhaps a stack trace will help:"); e.printStackTrace(System.out); } } }
A sonnet class file to see if telegraphoperator works and to pass test 16 and 17:
public class Sonnet18 { public static String text = "shall i compare thee to a summer's day?" + "// thou are more lovely and more" + "temperate. // rough winds to shake the" + "darling buds of may, // and summer's" + "lease hath all too short a date. //" + "sometime too hot the eye of heaven" + "shines, // and often is his gold" + "complexion dimmed; // and every fair" + "from fair sometimes declines, // by" + "chance, or nature's changing course," + "untrimmed; // but they eternal summer" + "shall not fade," + "// nor lose possession of that fair thou" + "// ow'st, // nor shall death brag thou" + "// wand'rest in his shade, // when in" + "// eternal lines to time thou grow'st." + "// // so long as men can breath, or eyes" + "// can see, // so long lives this, and" + "// this gives live to thee." ; public static String morse = "... .... .- .-.. .-.. .. -.-. --- -- .--. .- .-. . - .... . . - --- .- ... ..- -- -- . .-. .----. ... -.. .- -.-- ..--.. " + "-..-. -..-. - .... --- ..- .- .-. . -- --- .-. . .-.. --- ...- . .-.. -.-- .- -. -.. -- --- .-. . " + "- . -- .--. . .-. .- - . .-.-.- -..-. -..-. .-. --- ..- --. .... .-- .. -. -.. ... - --- ... .... .- -.- . - .... . " + "-.. .- .-. .-.. .. -. --. -... ..- -.. ... --- ..-. -- .- -.-- --..-- -..-. -..-. .- -. -.. ... ..- -- -- . .-. .----. ... " + ".-.. . .- ... . .... .- - .... .- .-.. .-.. - --- --- ... .... --- .-. - .- -.. .- - . .-.-.- -..-. -..-. " + "... --- -- . - .. -- . - --- --- .... --- - - .... . . -.-- . --- ..-. .... . .- ...- . -. " + "... .... .. -. . ... --..-- -..-. -..-. .- -. -.. --- ..-. - . -. .. ... .... .. ... --. --- .-.. -.. " + "-.-. --- -- .--. .-.. . -..- .. --- -. -.. .. -- -- . -.. -.-.-. -..-. -..-. .- -. -.. . ...- . .-. -.-- ..-. .- .. .-. " + "..-. .-. --- -- ..-. .- .. .-. ... --- -- . - .. -- . ... -.. . -.-. .-.. .. -. . ... --..-- -..-. -..-. -... -.-- " + "-.-. .... .- -. -.-. . --..-- --- .-. -. .- - ..- .-. . .----. ... -.-. .... .- -. --. .. -. --. -.-. --- ..- .-. ... . --..-- " + "..- -. - .-. .. -- -- . -.. -.-.-. -..-. -..-. -... ..- - - .... . -.-- . - . .-. -. .- .-.. ... ..- -- -- . .-. " + "... .... .- .-.. .-.. -. --- - ..-. .- -.. . --..-- " + "-..-. -..-. -. --- .-. .-.. --- ... . .--. --- ... ... . ... ... .. --- -. --- ..-. - .... .- - ..-. .- .. .-. - .... --- ..- " + "-..-. -..-. --- .-- .----. ... - --..-- -..-. -..-. -. --- .-. ... .... .- .-.. .-.. -.. . .- - .... -... .-. .- --. - .... --- ..- " + "-..-. -..-. .-- .- -. -.. .----. .-. . ... - .. -. .... .. ... ... .... .- -.. . --..-- -..-. -..-. .-- .... . -. .. -. " + "-..-. -..-. . - . .-. -. .- .-.. .-.. .. -. . ... - --- - .. -- . - .... --- ..- --. .-. --- .-- .----. ... - .-.-.- " + "-..-. -..-. -..-. -..-. ... --- .-.. --- -. --. .- ... -- . -. -.-. .- -. -... .-. . .- - .... --..-- --- .-. . -.-- . ... " + "-..-. -..-. -.-. .- -. ... . . --..-- -..-. -..-. ... --- .-.. --- -. --. .-.. .. ...- . ... - .... .. ... --..-- .- -. -.. " + "-..-. -..-. - .... .. ... --. .. ...- . ... .-.. .. ...- . - --- - .... . . .-.-.-" ; }
Mores Tree Node class:
package morse;
public class MorseTreeNode { Character symbol; MorseTreeNode dot; MorseTreeNode dash;
MorseTreeNode(Character symbol) { this.symbol = symbol; dot = dash = null; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started