Question
ReceiveMessage and SendMessage Method help: import morse.*; public class TelegraphOperator { private MorseTree decoder; private String[] encoder; /* * Constructor accepts an already-constructed instance of
ReceiveMessage and SendMessage Method help:
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 Morse Tree Class completed and needed to pass first 7 tests:
package morse;
public class MorseTree { MorseTreeNode root;
public MorseTree() { root = new MorseTreeNode(' '); //dummy character at root }
/* * 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; } }
The Tester Class:
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); } } }
Node Class:
/* * MorseTreeNode.java * * Used to implement the binary tree for morse-signal decoding. * Rather than having a "left" and "right" references, we * instead have the names "dot" and "dash". */ 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