I need help with receiveMessage and sendMessage methods: * This is used as a bin
ID: 3859777 • Letter: I
Question
I need help with receiveMessage and sendMessage methods:
* 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() {
}
/*
* 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) {
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) {
return ' ';
}
}
The 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;
}
}
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 "";
}
}
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 =
"... .... .- .-.. .-.. .. -.-. --- -- .--. .- .-. . - .... . . - --- .- ... ..- -- -- . .-. .----. ... -.. .- -.-- ..--.. "
+ "-..-. -..-. - .... --- ..- .- .-. . -- --- .-. . .-.. --- ...- . .-.. -.-- .- -. -.. -- --- .-. . "
+ "- . -- .--. . .-. .- - . .-.-.- -..-. -..-. .-. --- ..- --. .... .-- .. -. -.. ... - --- ... .... .- -.- . - .... . "
+ "-.. .- .-. .-.. .. -. --. -... ..- -.. ... --- ..-. -- .- -.-- --..-- -..-. -..-. .- -. -.. ... ..- -- -- . .-. .----. ... "
+ ".-.. . .- ... . .... .- - .... .- .-.. .-.. - --- --- ... .... --- .-. - .- -.. .- - . .-.-.- -..-. -..-. "
+ "... --- -- . - .. -- . - --- --- .... --- - - .... . . -.-- . --- ..-. .... . .- ...- . -. "
+ "... .... .. -. . ... --..-- -..-. -..-. .- -. -.. --- ..-. - . -. .. ... .... .. ... --. --- .-.. -.. "
+ "-.-. --- -- .--. .-.. . -..- .. --- -. -.. .. -- -- . -.. -.-.-. -..-. -..-. .- -. -.. . ...- . .-. -.-- ..-. .- .. .-. "
+ "..-. .-. --- -- ..-. .- .. .-. ... --- -- . - .. -- . ... -.. . -.-. .-.. .. -. . ... --..-- -..-. -..-. -... -.-- "
+ "-.-. .... .- -. -.-. . --..-- --- .-. -. .- - ..- .-. . .----. ... -.-. .... .- -. --. .. -. --. -.-. --- ..- .-. ... . --..-- "
+ "..- -. - .-. .. -- -- . -.. -.-.-. -..-. -..-. -... ..- - - .... . -.-- . - . .-. -. .- .-.. ... ..- -- -- . .-. "
+ "... .... .- .-.. .-.. -. --- - ..-. .- -.. . --..-- "
+ "-..-. -..-. -. --- .-. .-.. --- ... . .--. --- ... ... . ... ... .. --- -. --- ..-. - .... .- - ..-. .- .. .-. - .... --- ..- "
+ "-..-. -..-. --- .-- .----. ... - --..-- -..-. -..-. -. --- .-. ... .... .- .-.. .-.. -.. . .- - .... -... .-. .- --. - .... --- ..- "
+ "-..-. -..-. .-- .- -. -.. .----. .-. . ... - .. -. .... .. ... ... .... .- -.. . --..-- -..-. -..-. .-- .... . -. .. -. "
+ "-..-. -..-. . - . .-. -. .- .-.. .-.. .. -. . ... - --- - .. -- . - .... --- ..- --. .-. --- .-- .----. ... - .-.-.- "
+ "-..-. -..-. -..-. -..-. ... --- .-.. --- -. --. .- ... -- . -. -.-. .- -. -... .-. . .- - .... --..-- --- .-. . -.-- . ... "
+ "-..-. -..-. -.-. .- -. ... . . --..-- -..-. -..-. ... --- .-.. --- -. --. .-.. .. ...- . ... - .... .. ... --..-- .- -. -.. "
+ "-..-. -..-. - .... .. ... --. .. ...- . ... .-.. .. ...- . - --- - .... . . .-.-.-"
;
}
the Tester class 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.
*
*
* 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);
}
}
}
Explanation / Answer
public class MorseTree {
MorseTreeNode root;
public MorseTree() {
}
/*
* 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.
*
* Refer to the A#4 description (PDF) for more details.
*
*/
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 void insertSymbol(char symbol, String code) {
MorseTreeNode current = root;
String signal = " ";
for (int i = 0; i < code.length(); i++) {
signal = code.substring(i, i + 1);
if (signal.equals(".")) {
if (current.getLeft() != null) {
current = current.getLeft();
} else {
current.setLeft(new MorseTreeNode());
current = current.getLeft();
}
} else {
if (current.getRight() != null) {
current = current.getRight();
} else {
current.setRight(new MorseTreeNode());
current = current.getRight();
}
}
}
current.setLetter(symbol);
}
public Character lookupSymbol(String code) {
for(int i=0;i<itu_morse.lengh;i++) {
if(itu_morse[i].equals(code)) {
return itu_symbol[i];
}
}
return ' ';
}
}