SlideShare a Scribd company logo
1 of 18
Download to read offline
The Morse code (see Table 6.10 in book) is a common code that is used to encode messages
consisted of letters and digits. Each letter consists of a series of dots and dashes; for example, the
code for the letter a is *- and the code for the letter b is -***. Store each letter of the alphabet in a
node of a binary tree of level 5. The root node is at level 1 and stores no letter. The left node at
level 2 stores the letter e (code is *), and the right node stores letter t (code is -). The 4 nodes at
level 3 store the letters with codes (**, *-, -*, --). To build the tree (see Figure 6.38 in book),
read a file in which each line consists of a letter followed by its code. The letters should be
ordered by tree level. To find the position for a letter in the tree, scan the code and branch left for
a dot and branch right for a dash. Encode a message by replacing each letter by its code symbol.
Then decode the message using the Morse code tree. Make sure you use a delimiter symbol
between coded letters.
Implementation Notes: You will need to complete the generic implementation of the BinaryTree
data structure first (see slides). Once it is complete, solve the problem above (note that the book
uses dots and dashes, we will use the asterisk (*) as the dot, hyphen (-) as a dash, and space ( ) as
the delimiter symbol between coded letters). Provide a menu program that asks the user to 1) test
output for all morse code letters with their respective translated alphabet letters (make sure you
are using the binary tree to do the actual translation, output as a nicely formatted table), 2) enter
an input file name to decode morse code and output the translated text to the screen, or 3) enter
in a line of morse code through the console to decode morse code and output the translated text
to the screen
relevenat picture of both the tree diagram and the table of morse code
Solution
JAVA CODE:
import java.io.*;
import java.util.*;
public class MorseCoder implements MorseCodeInterface {
private MorseNode root;
/**
* constructor to build the tree
*/
public MorseCoder() {
root = new MorseNode();
readTreeInfo();
}
/**
* reads in the tree info from the text file (helper method)
*/
private void readTreeInfo() {
Scanner input = null;
try {
input = new Scanner(new File("encodings.txt"));
} catch (FileNotFoundException exception) {
System.out.println("File not found!");
}
while (input.hasNextLine()) {
String data = input.nextLine().trim();
if (data.length() > 0) {
add(data.substring(1).trim(), data.charAt(0));
}
}
input.close();
}
/**
* adds the letter to the tree based on the mcode string (helper method)
* @param mcode the string being fed in
* @param ltr the letter being added at the node
*/
private void add(String mcode, char ltr) {
MorseNode current = root;
String signal = " ";
for (int i = 0; i < mcode.length(); i++) {
signal = mcode.substring(i, i + 1);
if (signal.equals(".")) {
if (current.getLeft() != null) {
current = current.getLeft();
} else {
current.setLeft(new MorseNode());
current = current.getLeft();
}
} else {
if (current.getRight() != null) {
current = current.getRight();
} else {
current.setRight(new MorseNode());
current = current.getRight();
}
}
}
current.setLetter(ltr);
}
/**
* prints out inorder tree contents
*/
public void inOrderPrint() {
MorseNode current = root;
printInorder(current);
}
/**
* called by inOrderPrint to print tree contents (helper method)
* @param current the node to print
*/
private void printInorder(MorseNode current) {
if (current != null) {
printInorder(current.getLeft());
System.out.print(current.getLetter());
printInorder(current.getRight());
}
}
/**
* decodes a String of morse code to English
* @param str String of morse code
* @return result String of English
*/
public String decode(String str) {
String signal = "";
StringBuffer result = new StringBuffer("");
MorseNode current = root;
for (int i = 0; i < str.length(); i++) {
signal = str.substring(i, i + 1);
if (signal.equals(".")) {
if (current.getLeft() != null) {
current = current.getLeft();
} else {
current.setLeft(new MorseNode());
current = current.getLeft();
}
} else if (signal.equals("-")) {
if (current.getRight() != null) {
current = current.getRight();
} else {
current.setRight(new MorseNode());
current = current.getRight();
}
} else {
result = result.append(current.getLetter());
current = root;
}
}
result = result.append(current.getLetter());
return result.toString();
}
/**
* decodes a String of English to morse code
* @param str String of English
* @return result String of morse code
*/
public String encode(String str) {
MorseNode current = root;
String result = "";
String s = "";
char ltr;
for (int i = 0; i < str.length(); i++) {
ltr = str.charAt(i);
result = searchTree(current, ltr, s);
}
return result;
}
/**
* searches the tree for the letter(s) being inputed and outputs a string of morse (helper
method)
* @param current the node of the tree
* @param ltr the letter being searched for in the tree
* @param s the String being used to build the morse code
* @return the morse code corresponding to the item being checked
*/
public String searchTree(MorseNode current, char ltr, String s) {
char temp = current.getLetter(); //for debugging purposes
if (current.getLetter() == ltr) {
return s;
} else {
if (current.getLeft() != null) {
return searchTree(current.getLeft(), ltr, s + ".");
}
if (current.getRight() != null) {
return searchTree(current.getRight(), ltr, s + "-");
}
return s;
}
}
}
/*Interface*/
public interface MorseCodeInterface {
void inOrderPrint();
String decode(String str);
String encode(String str);
}
/**
* Node class to use with morsecode tree. Do not modify this class.
*/
public class MorseNode {
/** a letter of the alphabet */
private char letter;
/** reference to the left child */
private MorseNode left;
/** reference to the right child */
private MorseNode right;
/** value of letter if empty (not set) */
public static final char EMPTY = ' ';
/*** Default constructor, actual values are set in the MorseCode constructor*/
public MorseNode() {
letter = EMPTY;
left = null;
right = null;
}
/**
* Gets letter contained in node
* @return String - letter value
*/
public char getLetter() {
return letter;
}
/**
* Sets letter in node
* @param String - letter to set
*/
public void setLetter(char letter) {
this.letter = letter;
}
/**
* Gets the node refrenced by left
* @return MorseNode - left
*/
public MorseNode getLeft() {
return left;
}
/**
* Sets left refrence
* @param left - refrence to the node to set
*/
public void setLeft(MorseNode left) {
this.left = left;
}
/**
* Gets the node refrenced by right
* @return MorseNode - right
*/
public MorseNode getRight() {
return right;
}
/**
* Sets right refrence
* @param right - refrence to the node to set
*/
public void setRight(MorseNode right) {
this.right = right;
}}
public class MorseMain {
public static void main(String[] args) {
//Do not modify this file.
MorseCoder mc = new MorseCoder();
mc.inOrderPrint();
// sos decode
System.out.println("Decode Test 1");
String str = "... --- ...";
System.out.println("str = " + str);
System.out.println("str should decode to: sos");
System.out.println("decode(str) = " + mc.decode(str));
testResults("sos", mc.decode(str));
// abcdef...xyz decode
System.out.println("Decode Test 2");
str = ".- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -."
+ " --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..";
System.out.println("str = " + str);
System.out.println("str should decode to: abcdefghijklmnopqrstuvwxyz");
System.out.println("decode(str) = " + mc.decode(str));
testResults("abcdefghijklmnopqrstuvwxyz", mc.decode(str));
// helpmeobiwanyouremyonlyhope decode
System.out.println("Decode Test 3");
str = ".... . .-.. .--. -- . --- -... .. .-- .- -. -.-- --- ..-"
+ " .-. . -- -.-- --- -. .-.. -.-- .... --- .--. .";
System.out.println("str = " + str);
System.out.println("str should decode to: helpmeobiwanyouremyonlyhope");
System.out.println("decode(str) = " + mc.decode(str));
testResults("helpmeobiwanyouremyonlyhope", mc.decode(str));
// --.. encode
System.out.println("Encode Test 1");
str = "z";
System.out.println("str = " + str);
System.out.println("str should encode to: --..");
System.out.println("encode(str) = " + mc.encode(str));
testResults("--..", mc.encode(str));
// ... --- ... encode
System.out.println("Encode Test 2");
str = "sos";
System.out.println("str = " + str);
System.out.println("str should encode to: ... --- ...");
System.out.println("encode(str) = " + mc.encode(str));
testResults("... --- ...", mc.encode(str));
// .-. ..- -. ..-. --- .-. .-. . ... - .-. ..- -. encode
System.out.println("Encode Test 3");
str = "runforrestrun";
System.out.println("str = " + str);
System.out.println("str should encode to: .-. ..- -. ..-. --- .-. .-. . ... - .-. ..- -.");
System.out.println("encode(str) = " + mc.encode(str));
testResults(".-. ..- -. ..-. --- .-. .-. . ... - .-. ..- -.", mc.encode(str));
}
/**
* Tests if the output string matches the expected.
* Prints "Test: Passed" if they match, otherwise prints "Test: Failed"
* @param one String - Expected output
* @param two String - Encoded/Decoded call
*/
public static void testResults(String one, String two) {
if(one.trim().equals(two.trim()))
System.out.println("Test: Passed");
else
System.out.println("Test: Failed");
System.out.println();
}
}
INPUT:
encodings.txt :
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 --..
OUTPUT:
Sample output:
h
s
v
i
f
u
e
l
r
a
p
w
j
b
d
x
n
c
k
y
t
z
g
q
m
o
Decode Test 1
str = ... --- ...
str should decode to: sos
decode(str) = sos
Test: Passed
Decode Test 2
str = .- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..
str should decode to: abcdefghijklmnopqrstuvwxyz
decode(str) = abcdefghijklmnopqrstuvwxyz
Test: Passed
Decode Test 3
str = .... . .-.. .--. -- . --- -... .. .-- .- -. -.-- --- ..- .-. . -- -.-- --- -. .-.. -.-- .... --- .--. .
str should decode to: helpmeobiwanyouremyonlyhope
decode(str) = helpmeobiwanyouremyonlyhope
Test: Passed
Encode Test 1
str = z
str should encode to: --..
encode(str) = --..
Test: Passed
Encode Test 2
str = sos
str should encode to: ... --- ...
encode(str) = ... --- ...
Test: Passed
Encode Test 3
str = runforrestrun
str should encode to: .-. ..- -. ..-. --- .-. .-. . ... - .-. ..- -.
encode(str) = .-. ..- -. ..-. --- .-. .-. . ... - .-. ..- -.
Test: Passed
/**
* Node class to use with morsecode tree. Do not modify this class.
*/
public class MorseNode {
/** a letter of the alphabet */
private char letter;
/** reference to the left child */
private MorseNode left;
/** reference to the right child */
private MorseNode right;
/** value of letter if empty (not set) */
public static final char EMPTY = ' ';
/*** Default constructor, actual values are set in the MorseCode constructor*/
public MorseNode() {
letter = EMPTY;
left = null;
right = null;
}
/**
* Gets letter contained in node
* @return String - letter value
*/
public char getLetter() {
return letter;
}
/**
* Sets letter in node
* @param String - letter to set
*/
public void setLetter(char letter) {
this.letter = letter;
}
/**
* Gets the node refrenced by left
* @return MorseNode - left
*/
public MorseNode getLeft() {
return left;
}
/**
* Sets left refrence
* @param left - refrence to the node to set
*/
public void setLeft(MorseNode left) {
this.left = left;
}
/**
* Gets the node refrenced by right
* @return MorseNode - right
*/
public MorseNode getRight() {
return right;
}
/**
* Sets right refrence
* @param right - refrence to the node to set
*/
public void setRight(MorseNode right) {
this.right = right;
}}
public class MorseMain {
public static void main(String[] args) {
//Do not modify this file.
MorseCoder mc = new MorseCoder();
mc.inOrderPrint();
// sos decode
System.out.println("Decode Test 1");
String str = "... --- ...";
System.out.println("str = " + str);
System.out.println("str should decode to: sos");
System.out.println("decode(str) = " + mc.decode(str));
testResults("sos", mc.decode(str));
// abcdef...xyz decode
System.out.println("Decode Test 2");
str = ".- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -."
+ " --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..";
System.out.println("str = " + str);
System.out.println("str should decode to: abcdefghijklmnopqrstuvwxyz");
System.out.println("decode(str) = " + mc.decode(str));
testResults("abcdefghijklmnopqrstuvwxyz", mc.decode(str));
// helpmeobiwanyouremyonlyhope decode
System.out.println("Decode Test 3");
str = ".... . .-.. .--. -- . --- -... .. .-- .- -. -.-- --- ..-"
+ " .-. . -- -.-- --- -. .-.. -.-- .... --- .--. .";
System.out.println("str = " + str);
System.out.println("str should decode to: helpmeobiwanyouremyonlyhope");
System.out.println("decode(str) = " + mc.decode(str));
testResults("helpmeobiwanyouremyonlyhope", mc.decode(str));
// --.. encode
System.out.println("Encode Test 1");
str = "z";
System.out.println("str = " + str);
System.out.println("str should encode to: --..");
System.out.println("encode(str) = " + mc.encode(str));
testResults("--..", mc.encode(str));
// ... --- ... encode
System.out.println("Encode Test 2");
str = "sos";
System.out.println("str = " + str);
System.out.println("str should encode to: ... --- ...");
System.out.println("encode(str) = " + mc.encode(str));
testResults("... --- ...", mc.encode(str));
// .-. ..- -. ..-. --- .-. .-. . ... - .-. ..- -. encode
System.out.println("Encode Test 3");
str = "runforrestrun";
System.out.println("str = " + str);
System.out.println("str should encode to: .-. ..- -. ..-. --- .-. .-. . ... - .-. ..- -.");
System.out.println("encode(str) = " + mc.encode(str));
testResults(".-. ..- -. ..-. --- .-. .-. . ... - .-. ..- -.", mc.encode(str));
}
/**
* Tests if the output string matches the expected.
* Prints "Test: Passed" if they match, otherwise prints "Test: Failed"
* @param one String - Expected output
* @param two String - Encoded/Decoded call
*/
public static void testResults(String one, String two) {
if(one.trim().equals(two.trim()))
System.out.println("Test: Passed");
else
System.out.println("Test: Failed");
System.out.println();
}
}
INPUT:
encodings.txt :
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 --..
OUTPUT:
Sample output:
h
s
v
i
f
u
e
l
r
a
p
w
j
b
d
x
n
c
k
y
t
z
g
q
m
o
Decode Test 1
str = ... --- ...
str should decode to: sos
decode(str) = sos
Test: Passed
Decode Test 2
str = .- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..
str should decode to: abcdefghijklmnopqrstuvwxyz
decode(str) = abcdefghijklmnopqrstuvwxyz
Test: Passed
Decode Test 3
str = .... . .-.. .--. -- . --- -... .. .-- .- -. -.-- --- ..- .-. . -- -.-- --- -. .-.. -.-- .... --- .--. .
str should decode to: helpmeobiwanyouremyonlyhope
decode(str) = helpmeobiwanyouremyonlyhope
Test: Passed
Encode Test 1
str = z
str should encode to: --..
encode(str) = --..
Test: Passed
Encode Test 2
str = sos
str should encode to: ... --- ...
encode(str) = ... --- ...
Test: Passed
Encode Test 3
str = runforrestrun
str should encode to: .-. ..- -. ..-. --- .-. .-. . ... - .-. ..- -.
encode(str) = .-. ..- -. ..-. --- .-. .-. . ... - .-. ..- -.
Test: Passed

More Related Content

Similar to The Morse code (see Table 6.10 in book) is a common code that is use.pdf

Characters formats &amp; strimgs
Characters formats  &amp; strimgsCharacters formats  &amp; strimgs
Characters formats &amp; strimgsWeslley Assis
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional arrayRajendran
 
I need some help to correct this code Instructions The following progr.docx
I need some help to correct this code Instructions The following progr.docxI need some help to correct this code Instructions The following progr.docx
I need some help to correct this code Instructions The following progr.docxPhil4IDBrownh
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstackThierry Gayet
 
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docxAdamq0DJonese
 
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary dataKernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary dataAnne Nicolas
 
MIS 4310-01 Final Take Home Exam (100 points) DUE Thursd.docx
MIS 4310-01 Final Take Home Exam (100 points) DUE  Thursd.docxMIS 4310-01 Final Take Home Exam (100 points) DUE  Thursd.docx
MIS 4310-01 Final Take Home Exam (100 points) DUE Thursd.docxraju957290
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptxJawadTanvir
 
Common mistakes in C programming
Common mistakes in C programmingCommon mistakes in C programming
Common mistakes in C programmingLarion
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)Saifur Rahman
 
#pragma once#include iostreamclass String { private .docx
#pragma once#include iostreamclass String { private  .docx#pragma once#include iostreamclass String { private  .docx
#pragma once#include iostreamclass String { private .docxgertrudebellgrove
 
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfModify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfarjuncorner565
 
I have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdfI have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdfezzi552
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1Shashwat Shriparv
 

Similar to The Morse code (see Table 6.10 in book) is a common code that is use.pdf (20)

Characters formats &amp; strimgs
Characters formats  &amp; strimgsCharacters formats  &amp; strimgs
Characters formats &amp; strimgs
 
C q 3
C q 3C q 3
C q 3
 
String searching
String searchingString searching
String searching
 
Embedded C - Lecture 2
Embedded C - Lecture 2Embedded C - Lecture 2
Embedded C - Lecture 2
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
I need some help to correct this code Instructions The following progr.docx
I need some help to correct this code Instructions The following progr.docxI need some help to correct this code Instructions The following progr.docx
I need some help to correct this code Instructions The following progr.docx
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstack
 
Interview C++11 code
Interview C++11 codeInterview C++11 code
Interview C++11 code
 
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
 
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary dataKernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
 
MIS 4310-01 Final Take Home Exam (100 points) DUE Thursd.docx
MIS 4310-01 Final Take Home Exam (100 points) DUE  Thursd.docxMIS 4310-01 Final Take Home Exam (100 points) DUE  Thursd.docx
MIS 4310-01 Final Take Home Exam (100 points) DUE Thursd.docx
 
Functions
FunctionsFunctions
Functions
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
Common mistakes in C programming
Common mistakes in C programmingCommon mistakes in C programming
Common mistakes in C programming
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
 
String notes
String notesString notes
String notes
 
#pragma once#include iostreamclass String { private .docx
#pragma once#include iostreamclass String { private  .docx#pragma once#include iostreamclass String { private  .docx
#pragma once#include iostreamclass String { private .docx
 
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfModify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
 
I have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdfI have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdf
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 

More from bhim1213

In 1998, Congress passed legislation concerning shifting the burden .pdf
In 1998, Congress passed legislation concerning shifting the burden .pdfIn 1998, Congress passed legislation concerning shifting the burden .pdf
In 1998, Congress passed legislation concerning shifting the burden .pdfbhim1213
 
If A denotes some event, what does A denote If P(A)equals=0.997, wh.pdf
If A denotes some event, what does A denote If P(A)equals=0.997, wh.pdfIf A denotes some event, what does A denote If P(A)equals=0.997, wh.pdf
If A denotes some event, what does A denote If P(A)equals=0.997, wh.pdfbhim1213
 
How do neutrophils find and get to an infectionSolutionNeutro.pdf
How do neutrophils find and get to an infectionSolutionNeutro.pdfHow do neutrophils find and get to an infectionSolutionNeutro.pdf
How do neutrophils find and get to an infectionSolutionNeutro.pdfbhim1213
 
How many times will the following code print Welcome to Java in.pdf
How many times will the following code print Welcome to Java  in.pdfHow many times will the following code print Welcome to Java  in.pdf
How many times will the following code print Welcome to Java in.pdfbhim1213
 
From your own words not copy past from internet please Talked about.pdf
From your own words not copy past from internet please Talked about.pdfFrom your own words not copy past from internet please Talked about.pdf
From your own words not copy past from internet please Talked about.pdfbhim1213
 
Forms an important part of the subunits of the ribosome. The _ are sp.pdf
Forms an important part of the subunits of the ribosome. The _ are sp.pdfForms an important part of the subunits of the ribosome. The _ are sp.pdf
Forms an important part of the subunits of the ribosome. The _ are sp.pdfbhim1213
 
During DNA replication, a series of steps are required to ensure tha.pdf
During DNA replication, a series of steps are required to ensure tha.pdfDuring DNA replication, a series of steps are required to ensure tha.pdf
During DNA replication, a series of steps are required to ensure tha.pdfbhim1213
 
Disease X occurs when someone has the xx genotype. However, only 20 .pdf
Disease X occurs when someone has the xx genotype. However, only 20 .pdfDisease X occurs when someone has the xx genotype. However, only 20 .pdf
Disease X occurs when someone has the xx genotype. However, only 20 .pdfbhim1213
 
Darwins Theory had five main tenets (or components), what are they.pdf
Darwins Theory had five main tenets (or components), what are they.pdfDarwins Theory had five main tenets (or components), what are they.pdf
Darwins Theory had five main tenets (or components), what are they.pdfbhim1213
 
Define toxic shock syndrome (TSS) impetigo MRSASolutionAnswer.pdf
Define toxic shock syndrome (TSS) impetigo MRSASolutionAnswer.pdfDefine toxic shock syndrome (TSS) impetigo MRSASolutionAnswer.pdf
Define toxic shock syndrome (TSS) impetigo MRSASolutionAnswer.pdfbhim1213
 
A system was set up in parallel with two components, A and B. The pr.pdf
A system was set up in parallel with two components, A and B. The pr.pdfA system was set up in parallel with two components, A and B. The pr.pdf
A system was set up in parallel with two components, A and B. The pr.pdfbhim1213
 
All content is made permanent for future use. All content will be lo.pdf
All content is made permanent for future use.  All content will be lo.pdfAll content is made permanent for future use.  All content will be lo.pdf
All content is made permanent for future use. All content will be lo.pdfbhim1213
 
a. A waitress is serving 5 people at a table. She has the 5 dishes t.pdf
a. A waitress is serving 5 people at a table. She has the 5 dishes t.pdfa. A waitress is serving 5 people at a table. She has the 5 dishes t.pdf
a. A waitress is serving 5 people at a table. She has the 5 dishes t.pdfbhim1213
 
Biochemistry homework Focus concept ripening fruit and the activity.pdf
Biochemistry homework Focus concept ripening fruit and the activity.pdfBiochemistry homework Focus concept ripening fruit and the activity.pdf
Biochemistry homework Focus concept ripening fruit and the activity.pdfbhim1213
 
A process that has its representative PCB in a device queue is in th.pdf
A process that has its representative PCB in a device queue is in th.pdfA process that has its representative PCB in a device queue is in th.pdf
A process that has its representative PCB in a device queue is in th.pdfbhim1213
 
Which of the following statements is (are) trueI Cash flow is a.pdf
Which of the following statements is (are) trueI  Cash flow is a.pdfWhich of the following statements is (are) trueI  Cash flow is a.pdf
Which of the following statements is (are) trueI Cash flow is a.pdfbhim1213
 
What is the relationship of the epithelium of the epidermis to the co.pdf
What is the relationship of the epithelium of the epidermis to the co.pdfWhat is the relationship of the epithelium of the epidermis to the co.pdf
What is the relationship of the epithelium of the epidermis to the co.pdfbhim1213
 
What is a linked listWhat is a linked lists general syntaxCan .pdf
What is a linked listWhat is a linked lists general syntaxCan .pdfWhat is a linked listWhat is a linked lists general syntaxCan .pdf
What is a linked listWhat is a linked lists general syntaxCan .pdfbhim1213
 
What is the output of running class GenericMethodDemo of the followi.pdf
What is the output of running class GenericMethodDemo of the followi.pdfWhat is the output of running class GenericMethodDemo of the followi.pdf
What is the output of running class GenericMethodDemo of the followi.pdfbhim1213
 
What do you think are the roles of professional societies in contemp.pdf
What do you think are the roles of professional societies in contemp.pdfWhat do you think are the roles of professional societies in contemp.pdf
What do you think are the roles of professional societies in contemp.pdfbhim1213
 

More from bhim1213 (20)

In 1998, Congress passed legislation concerning shifting the burden .pdf
In 1998, Congress passed legislation concerning shifting the burden .pdfIn 1998, Congress passed legislation concerning shifting the burden .pdf
In 1998, Congress passed legislation concerning shifting the burden .pdf
 
If A denotes some event, what does A denote If P(A)equals=0.997, wh.pdf
If A denotes some event, what does A denote If P(A)equals=0.997, wh.pdfIf A denotes some event, what does A denote If P(A)equals=0.997, wh.pdf
If A denotes some event, what does A denote If P(A)equals=0.997, wh.pdf
 
How do neutrophils find and get to an infectionSolutionNeutro.pdf
How do neutrophils find and get to an infectionSolutionNeutro.pdfHow do neutrophils find and get to an infectionSolutionNeutro.pdf
How do neutrophils find and get to an infectionSolutionNeutro.pdf
 
How many times will the following code print Welcome to Java in.pdf
How many times will the following code print Welcome to Java  in.pdfHow many times will the following code print Welcome to Java  in.pdf
How many times will the following code print Welcome to Java in.pdf
 
From your own words not copy past from internet please Talked about.pdf
From your own words not copy past from internet please Talked about.pdfFrom your own words not copy past from internet please Talked about.pdf
From your own words not copy past from internet please Talked about.pdf
 
Forms an important part of the subunits of the ribosome. The _ are sp.pdf
Forms an important part of the subunits of the ribosome. The _ are sp.pdfForms an important part of the subunits of the ribosome. The _ are sp.pdf
Forms an important part of the subunits of the ribosome. The _ are sp.pdf
 
During DNA replication, a series of steps are required to ensure tha.pdf
During DNA replication, a series of steps are required to ensure tha.pdfDuring DNA replication, a series of steps are required to ensure tha.pdf
During DNA replication, a series of steps are required to ensure tha.pdf
 
Disease X occurs when someone has the xx genotype. However, only 20 .pdf
Disease X occurs when someone has the xx genotype. However, only 20 .pdfDisease X occurs when someone has the xx genotype. However, only 20 .pdf
Disease X occurs when someone has the xx genotype. However, only 20 .pdf
 
Darwins Theory had five main tenets (or components), what are they.pdf
Darwins Theory had five main tenets (or components), what are they.pdfDarwins Theory had five main tenets (or components), what are they.pdf
Darwins Theory had five main tenets (or components), what are they.pdf
 
Define toxic shock syndrome (TSS) impetigo MRSASolutionAnswer.pdf
Define toxic shock syndrome (TSS) impetigo MRSASolutionAnswer.pdfDefine toxic shock syndrome (TSS) impetigo MRSASolutionAnswer.pdf
Define toxic shock syndrome (TSS) impetigo MRSASolutionAnswer.pdf
 
A system was set up in parallel with two components, A and B. The pr.pdf
A system was set up in parallel with two components, A and B. The pr.pdfA system was set up in parallel with two components, A and B. The pr.pdf
A system was set up in parallel with two components, A and B. The pr.pdf
 
All content is made permanent for future use. All content will be lo.pdf
All content is made permanent for future use.  All content will be lo.pdfAll content is made permanent for future use.  All content will be lo.pdf
All content is made permanent for future use. All content will be lo.pdf
 
a. A waitress is serving 5 people at a table. She has the 5 dishes t.pdf
a. A waitress is serving 5 people at a table. She has the 5 dishes t.pdfa. A waitress is serving 5 people at a table. She has the 5 dishes t.pdf
a. A waitress is serving 5 people at a table. She has the 5 dishes t.pdf
 
Biochemistry homework Focus concept ripening fruit and the activity.pdf
Biochemistry homework Focus concept ripening fruit and the activity.pdfBiochemistry homework Focus concept ripening fruit and the activity.pdf
Biochemistry homework Focus concept ripening fruit and the activity.pdf
 
A process that has its representative PCB in a device queue is in th.pdf
A process that has its representative PCB in a device queue is in th.pdfA process that has its representative PCB in a device queue is in th.pdf
A process that has its representative PCB in a device queue is in th.pdf
 
Which of the following statements is (are) trueI Cash flow is a.pdf
Which of the following statements is (are) trueI  Cash flow is a.pdfWhich of the following statements is (are) trueI  Cash flow is a.pdf
Which of the following statements is (are) trueI Cash flow is a.pdf
 
What is the relationship of the epithelium of the epidermis to the co.pdf
What is the relationship of the epithelium of the epidermis to the co.pdfWhat is the relationship of the epithelium of the epidermis to the co.pdf
What is the relationship of the epithelium of the epidermis to the co.pdf
 
What is a linked listWhat is a linked lists general syntaxCan .pdf
What is a linked listWhat is a linked lists general syntaxCan .pdfWhat is a linked listWhat is a linked lists general syntaxCan .pdf
What is a linked listWhat is a linked lists general syntaxCan .pdf
 
What is the output of running class GenericMethodDemo of the followi.pdf
What is the output of running class GenericMethodDemo of the followi.pdfWhat is the output of running class GenericMethodDemo of the followi.pdf
What is the output of running class GenericMethodDemo of the followi.pdf
 
What do you think are the roles of professional societies in contemp.pdf
What do you think are the roles of professional societies in contemp.pdfWhat do you think are the roles of professional societies in contemp.pdf
What do you think are the roles of professional societies in contemp.pdf
 

Recently uploaded

Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use CasesTechSoup
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfNirmal Dwivedi
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonhttgc7rh9c
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 

Recently uploaded (20)

Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 

The Morse code (see Table 6.10 in book) is a common code that is use.pdf

  • 1. The Morse code (see Table 6.10 in book) is a common code that is used to encode messages consisted of letters and digits. Each letter consists of a series of dots and dashes; for example, the code for the letter a is *- and the code for the letter b is -***. Store each letter of the alphabet in a node of a binary tree of level 5. The root node is at level 1 and stores no letter. The left node at level 2 stores the letter e (code is *), and the right node stores letter t (code is -). The 4 nodes at level 3 store the letters with codes (**, *-, -*, --). To build the tree (see Figure 6.38 in book), read a file in which each line consists of a letter followed by its code. The letters should be ordered by tree level. To find the position for a letter in the tree, scan the code and branch left for a dot and branch right for a dash. Encode a message by replacing each letter by its code symbol. Then decode the message using the Morse code tree. Make sure you use a delimiter symbol between coded letters. Implementation Notes: You will need to complete the generic implementation of the BinaryTree data structure first (see slides). Once it is complete, solve the problem above (note that the book uses dots and dashes, we will use the asterisk (*) as the dot, hyphen (-) as a dash, and space ( ) as the delimiter symbol between coded letters). Provide a menu program that asks the user to 1) test output for all morse code letters with their respective translated alphabet letters (make sure you are using the binary tree to do the actual translation, output as a nicely formatted table), 2) enter an input file name to decode morse code and output the translated text to the screen, or 3) enter in a line of morse code through the console to decode morse code and output the translated text to the screen relevenat picture of both the tree diagram and the table of morse code Solution JAVA CODE: import java.io.*; import java.util.*; public class MorseCoder implements MorseCodeInterface { private MorseNode root; /** * constructor to build the tree */ public MorseCoder() { root = new MorseNode();
  • 2. readTreeInfo(); } /** * reads in the tree info from the text file (helper method) */ private void readTreeInfo() { Scanner input = null; try { input = new Scanner(new File("encodings.txt")); } catch (FileNotFoundException exception) { System.out.println("File not found!"); } while (input.hasNextLine()) { String data = input.nextLine().trim(); if (data.length() > 0) { add(data.substring(1).trim(), data.charAt(0)); } } input.close(); } /** * adds the letter to the tree based on the mcode string (helper method) * @param mcode the string being fed in * @param ltr the letter being added at the node */ private void add(String mcode, char ltr) { MorseNode current = root; String signal = " "; for (int i = 0; i < mcode.length(); i++) { signal = mcode.substring(i, i + 1); if (signal.equals(".")) { if (current.getLeft() != null) { current = current.getLeft(); } else { current.setLeft(new MorseNode()); current = current.getLeft();
  • 3. } } else { if (current.getRight() != null) { current = current.getRight(); } else { current.setRight(new MorseNode()); current = current.getRight(); } } } current.setLetter(ltr); } /** * prints out inorder tree contents */ public void inOrderPrint() { MorseNode current = root; printInorder(current); } /** * called by inOrderPrint to print tree contents (helper method) * @param current the node to print */ private void printInorder(MorseNode current) { if (current != null) { printInorder(current.getLeft()); System.out.print(current.getLetter()); printInorder(current.getRight()); } } /** * decodes a String of morse code to English * @param str String of morse code * @return result String of English */ public String decode(String str) {
  • 4. String signal = ""; StringBuffer result = new StringBuffer(""); MorseNode current = root; for (int i = 0; i < str.length(); i++) { signal = str.substring(i, i + 1); if (signal.equals(".")) { if (current.getLeft() != null) { current = current.getLeft(); } else { current.setLeft(new MorseNode()); current = current.getLeft(); } } else if (signal.equals("-")) { if (current.getRight() != null) { current = current.getRight(); } else { current.setRight(new MorseNode()); current = current.getRight(); } } else { result = result.append(current.getLetter()); current = root; } } result = result.append(current.getLetter()); return result.toString(); } /** * decodes a String of English to morse code * @param str String of English * @return result String of morse code */ public String encode(String str) { MorseNode current = root; String result = "";
  • 5. String s = ""; char ltr; for (int i = 0; i < str.length(); i++) { ltr = str.charAt(i); result = searchTree(current, ltr, s); } return result; } /** * searches the tree for the letter(s) being inputed and outputs a string of morse (helper method) * @param current the node of the tree * @param ltr the letter being searched for in the tree * @param s the String being used to build the morse code * @return the morse code corresponding to the item being checked */ public String searchTree(MorseNode current, char ltr, String s) { char temp = current.getLetter(); //for debugging purposes if (current.getLetter() == ltr) { return s; } else { if (current.getLeft() != null) { return searchTree(current.getLeft(), ltr, s + "."); } if (current.getRight() != null) { return searchTree(current.getRight(), ltr, s + "-"); } return s; } } } /*Interface*/ public interface MorseCodeInterface { void inOrderPrint(); String decode(String str);
  • 6. String encode(String str); } /** * Node class to use with morsecode tree. Do not modify this class. */ public class MorseNode { /** a letter of the alphabet */ private char letter; /** reference to the left child */ private MorseNode left; /** reference to the right child */ private MorseNode right; /** value of letter if empty (not set) */ public static final char EMPTY = ' '; /*** Default constructor, actual values are set in the MorseCode constructor*/ public MorseNode() { letter = EMPTY; left = null; right = null; } /** * Gets letter contained in node * @return String - letter value */ public char getLetter() { return letter; } /** * Sets letter in node * @param String - letter to set */ public void setLetter(char letter) { this.letter = letter; } /** * Gets the node refrenced by left
  • 7. * @return MorseNode - left */ public MorseNode getLeft() { return left; } /** * Sets left refrence * @param left - refrence to the node to set */ public void setLeft(MorseNode left) { this.left = left; } /** * Gets the node refrenced by right * @return MorseNode - right */ public MorseNode getRight() { return right; } /** * Sets right refrence * @param right - refrence to the node to set */ public void setRight(MorseNode right) { this.right = right; }} public class MorseMain { public static void main(String[] args) { //Do not modify this file. MorseCoder mc = new MorseCoder(); mc.inOrderPrint(); // sos decode System.out.println("Decode Test 1"); String str = "... --- ..."; System.out.println("str = " + str); System.out.println("str should decode to: sos");
  • 8. System.out.println("decode(str) = " + mc.decode(str)); testResults("sos", mc.decode(str)); // abcdef...xyz decode System.out.println("Decode Test 2"); str = ".- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -." + " --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --.."; System.out.println("str = " + str); System.out.println("str should decode to: abcdefghijklmnopqrstuvwxyz"); System.out.println("decode(str) = " + mc.decode(str)); testResults("abcdefghijklmnopqrstuvwxyz", mc.decode(str)); // helpmeobiwanyouremyonlyhope decode System.out.println("Decode Test 3"); str = ".... . .-.. .--. -- . --- -... .. .-- .- -. -.-- --- ..-" + " .-. . -- -.-- --- -. .-.. -.-- .... --- .--. ."; System.out.println("str = " + str); System.out.println("str should decode to: helpmeobiwanyouremyonlyhope"); System.out.println("decode(str) = " + mc.decode(str)); testResults("helpmeobiwanyouremyonlyhope", mc.decode(str)); // --.. encode System.out.println("Encode Test 1"); str = "z"; System.out.println("str = " + str); System.out.println("str should encode to: --.."); System.out.println("encode(str) = " + mc.encode(str)); testResults("--..", mc.encode(str)); // ... --- ... encode System.out.println("Encode Test 2"); str = "sos"; System.out.println("str = " + str); System.out.println("str should encode to: ... --- ..."); System.out.println("encode(str) = " + mc.encode(str)); testResults("... --- ...", mc.encode(str));
  • 9. // .-. ..- -. ..-. --- .-. .-. . ... - .-. ..- -. encode System.out.println("Encode Test 3"); str = "runforrestrun"; System.out.println("str = " + str); System.out.println("str should encode to: .-. ..- -. ..-. --- .-. .-. . ... - .-. ..- -."); System.out.println("encode(str) = " + mc.encode(str)); testResults(".-. ..- -. ..-. --- .-. .-. . ... - .-. ..- -.", mc.encode(str)); } /** * Tests if the output string matches the expected. * Prints "Test: Passed" if they match, otherwise prints "Test: Failed" * @param one String - Expected output * @param two String - Encoded/Decoded call */ public static void testResults(String one, String two) { if(one.trim().equals(two.trim())) System.out.println("Test: Passed"); else System.out.println("Test: Failed"); System.out.println(); } } INPUT: encodings.txt : a .- b -... c -.-. d -.. e . f ..-. g --. h .... i ..
  • 10. j .--- k -.- l .-.. m -- n -. o --- p .--. q --.- r .-. s ... t - u ..- v ...- w .-- x -..- y -.-- z --.. OUTPUT: Sample output: h s v i f u e l r a p w j b
  • 11. d x n c k y t z g q m o Decode Test 1 str = ... --- ... str should decode to: sos decode(str) = sos Test: Passed Decode Test 2 str = .- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --.. str should decode to: abcdefghijklmnopqrstuvwxyz decode(str) = abcdefghijklmnopqrstuvwxyz Test: Passed Decode Test 3 str = .... . .-.. .--. -- . --- -... .. .-- .- -. -.-- --- ..- .-. . -- -.-- --- -. .-.. -.-- .... --- .--. . str should decode to: helpmeobiwanyouremyonlyhope decode(str) = helpmeobiwanyouremyonlyhope Test: Passed Encode Test 1 str = z str should encode to: --.. encode(str) = --.. Test: Passed Encode Test 2 str = sos
  • 12. str should encode to: ... --- ... encode(str) = ... --- ... Test: Passed Encode Test 3 str = runforrestrun str should encode to: .-. ..- -. ..-. --- .-. .-. . ... - .-. ..- -. encode(str) = .-. ..- -. ..-. --- .-. .-. . ... - .-. ..- -. Test: Passed /** * Node class to use with morsecode tree. Do not modify this class. */ public class MorseNode { /** a letter of the alphabet */ private char letter; /** reference to the left child */ private MorseNode left; /** reference to the right child */ private MorseNode right; /** value of letter if empty (not set) */ public static final char EMPTY = ' '; /*** Default constructor, actual values are set in the MorseCode constructor*/ public MorseNode() { letter = EMPTY; left = null; right = null; } /** * Gets letter contained in node * @return String - letter value */ public char getLetter() { return letter; } /** * Sets letter in node * @param String - letter to set
  • 13. */ public void setLetter(char letter) { this.letter = letter; } /** * Gets the node refrenced by left * @return MorseNode - left */ public MorseNode getLeft() { return left; } /** * Sets left refrence * @param left - refrence to the node to set */ public void setLeft(MorseNode left) { this.left = left; } /** * Gets the node refrenced by right * @return MorseNode - right */ public MorseNode getRight() { return right; } /** * Sets right refrence * @param right - refrence to the node to set */ public void setRight(MorseNode right) { this.right = right; }} public class MorseMain { public static void main(String[] args) { //Do not modify this file. MorseCoder mc = new MorseCoder();
  • 14. mc.inOrderPrint(); // sos decode System.out.println("Decode Test 1"); String str = "... --- ..."; System.out.println("str = " + str); System.out.println("str should decode to: sos"); System.out.println("decode(str) = " + mc.decode(str)); testResults("sos", mc.decode(str)); // abcdef...xyz decode System.out.println("Decode Test 2"); str = ".- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -." + " --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --.."; System.out.println("str = " + str); System.out.println("str should decode to: abcdefghijklmnopqrstuvwxyz"); System.out.println("decode(str) = " + mc.decode(str)); testResults("abcdefghijklmnopqrstuvwxyz", mc.decode(str)); // helpmeobiwanyouremyonlyhope decode System.out.println("Decode Test 3"); str = ".... . .-.. .--. -- . --- -... .. .-- .- -. -.-- --- ..-" + " .-. . -- -.-- --- -. .-.. -.-- .... --- .--. ."; System.out.println("str = " + str); System.out.println("str should decode to: helpmeobiwanyouremyonlyhope"); System.out.println("decode(str) = " + mc.decode(str)); testResults("helpmeobiwanyouremyonlyhope", mc.decode(str)); // --.. encode System.out.println("Encode Test 1"); str = "z"; System.out.println("str = " + str); System.out.println("str should encode to: --.."); System.out.println("encode(str) = " + mc.encode(str)); testResults("--..", mc.encode(str)); // ... --- ... encode
  • 15. System.out.println("Encode Test 2"); str = "sos"; System.out.println("str = " + str); System.out.println("str should encode to: ... --- ..."); System.out.println("encode(str) = " + mc.encode(str)); testResults("... --- ...", mc.encode(str)); // .-. ..- -. ..-. --- .-. .-. . ... - .-. ..- -. encode System.out.println("Encode Test 3"); str = "runforrestrun"; System.out.println("str = " + str); System.out.println("str should encode to: .-. ..- -. ..-. --- .-. .-. . ... - .-. ..- -."); System.out.println("encode(str) = " + mc.encode(str)); testResults(".-. ..- -. ..-. --- .-. .-. . ... - .-. ..- -.", mc.encode(str)); } /** * Tests if the output string matches the expected. * Prints "Test: Passed" if they match, otherwise prints "Test: Failed" * @param one String - Expected output * @param two String - Encoded/Decoded call */ public static void testResults(String one, String two) { if(one.trim().equals(two.trim())) System.out.println("Test: Passed"); else System.out.println("Test: Failed"); System.out.println(); } } INPUT: encodings.txt : a .- b -... c -.-.
  • 16. d -.. e . f ..-. g --. h .... i .. j .--- k -.- l .-.. m -- n -. o --- p .--. q --.- r .-. s ... t - u ..- v ...- w .-- x -..- y -.-- z --.. OUTPUT: Sample output: h s v i f u e l r
  • 17. a p w j b d x n c k y t z g q m o Decode Test 1 str = ... --- ... str should decode to: sos decode(str) = sos Test: Passed Decode Test 2 str = .- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --.. str should decode to: abcdefghijklmnopqrstuvwxyz decode(str) = abcdefghijklmnopqrstuvwxyz Test: Passed Decode Test 3 str = .... . .-.. .--. -- . --- -... .. .-- .- -. -.-- --- ..- .-. . -- -.-- --- -. .-.. -.-- .... --- .--. . str should decode to: helpmeobiwanyouremyonlyhope decode(str) = helpmeobiwanyouremyonlyhope Test: Passed Encode Test 1
  • 18. str = z str should encode to: --.. encode(str) = --.. Test: Passed Encode Test 2 str = sos str should encode to: ... --- ... encode(str) = ... --- ... Test: Passed Encode Test 3 str = runforrestrun str should encode to: .-. ..- -. ..-. --- .-. .-. . ... - .-. ..- -. encode(str) = .-. ..- -. ..-. --- .-. .-. . ... - .-. ..- -. Test: Passed