ReceiveMessage and SendMessage Method help: This is the Morse Tree Class complet
ID: 3859847 • Letter: R
Question
ReceiveMessage and SendMessage Method help:
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:
Node Class:
Explanation / Answer
Based on the comments in the code, Completed the TelegraphOperator class. I could not test the method testPoemOperator() because it needs Sonnet18.text which is not provided in the question. So I commented out the code calling testPoemOperator () and tested rest of them. All other test in A4Test passed. Take a look at the output. Please rate the answer if it helped. Thank you.
TelegraphOperator.java
import java.util.HashMap;
import morse.*;
public class TelegraphOperator {
private MorseTree decoder;
private HashMap<Character, 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[])
{
this.decoder = decoder;
//store all the symbols and their corresponding morse code in a hashmap
encoder = new HashMap<Character, String>();
for(int i = 0; i < symbols.length; i++)
encoder.put(symbols[i], morse[i]);
}
/*
* 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) {
String words[] = codedMessage.split(" ");
String str = "";
for(int i = 0; i <words.length; i++)
{
if(i != 0)
str +=" ";
String codes[] = words[i].split(" ");
for(int j = 0; j <codes.length; j++){
Character c = decoder.lookupSymbol(codes[j]);
if(c != null)
str += c;
else //could not find the equivalent morse code in the tree
return "<message garbled>";
}
}
return str;
}
/*
* 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) {
String encodedMsg = "";
for(int i = 0; i < message.length(); i++){
char ch = message.charAt(i);
if(ch == ' ')
encodedMsg +=" ";
else
{
if(i != 0)
encodedMsg += " ";
encodedMsg += encoder.get(ch);//lookup hte morse code from hashmap
}
}
return encodedMsg;
}
}
output
testEmptyMorseTree
Passed test: 1
testSimpleMorseTree
Passed test: 2
Passed test: 3
Passed test: 4
Passed test: 5
testBasicMorseTree
Passed test: 6
Passed test: 7
testBasicOperator
Passed test: 8
Passed test: 9
Passed test: 10
Passed test: 11
Passed test: 12
Passed test: 13
Passed test: 14
Passed test: 15
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.