Lab 8
Get started now

Overview

In lab 7 you wrote a Morse Code encoder. For this assignment, you will create a Morse Code decoder. In this lab, you write and test two classes: MorseDecoder and MorseTree<E>.

In order to decode each series of dots and dashes, you must build a binary tree. A partially constructed tree is shown below. The tree contains the letters A to E in the alphabet.

Morse Code Tree

Notice that the resulting binary tree provides a decoding path. For example, the letter D can be decoded from its Morse Code: -... To accomplish this, we start at the root of the tree and traverse to the left or right depending on whether the code begins with a dot or a dash respectively. In this case, -.. begins with a dash followed by two dots, so we would traverse from the root of the tree once to the right and then twice to the left. Doing so causes us to arrive at a node in the tree with the value D.

In fact, once the tree is fully populated, all symbols in it can be decoded by walking the tree by visiting the left child when a dot is encountered and visiting the right child when a dash is encountered. When all of the dots/dashes in the code have been processed, the resulting node in the tree contains the symbol represented by the code.

Procedure

Your program must read in a file produced by the encoder you developed last week and write the decoded result to an output file. Your program must make use of a MorseTree<E> class to store the Morse Code in a binary tree.

The MorseTree<E> class must use generics to denote the type of data stored in the tree.

The MorseTree<E> class must have the following public methods:

  • MorseTree() – constructor that ensures that the content in the root node is set to null.
  • add() accepts a symbol (the letter/number/punctuation) and the Morse Code associated with the symbol as a String and inserts it into the tree. Keep in mind that this may involve adding multiple nodes to the tree. For example, if A is added to the tree first, the following nodes are added to the tree: An empty node (that is, a node whose content is set to null) as the root node's left child. The newly added empty node has a right child that is a node whose content is set to A.
  • decode() which accepts a String representing the code and returns the symbol that the code represents. If a code is not found, the method should return null. This method should throw an IllegalArgumentException if the String passed to the method contains anything other than . or -. Note: This method will function correctly only if the tree has been correctly populated in advance.

Your program should be implemented in the MorseDecoder class. The design for this class is your responsibility; however, you are required to have one method called loadDecoder() that accepts a File object containing the Morse Code file as an argument. The method calls the add() from the MorseTree<E> class multiple times in order to populate the tree.

Notes:

  • Your program should not decode characters that are not found in the tree. A warning message should be displayed to the console for any code encountered that is not in the morse code tree.
  • Line breaks in the input file should be replicated in the decoded file.

Sample Results

Running your program on this file:

.- | ... .--. .- -.-. . | ... .... --- ..- .-.. -.. | -... . | .--. .-.. .- -.-. . -.. | -... . - .-- . . -. | . .- -.-. .... | . -. -.-. --- -.. . -.. | -.-. .... .- .-. .- -.-. - . .-. .-.-.- | 

.- | * | ... .... --- ..- .-.. -.. | -... . | .--. .-.. .- -.-. . -.. | -... . - .-- . . -. | . .- -.-. .... | .-- --- .-. -.. .-.-.- | 

.-.. .. -. . | -... .-. . .- -.- ... | .. -. | - .... . | .. -. .--. ..- - | ..-. .. .-.. . | ... .... --- ..- .-.. -.. | -... . | .-. . .--. .-.. .. -.-. .- - . -.. | .. -. | - .... . | . -. -.-. --- -.. . -.. | ..-. .. .-.. . .-.-.- | 

should display something like this to the console:

Enter an input filename:
encoded.txt
Enter an output filename
decoded.txt
Warning: skipping: *

and produce the following output file:

A SPACE SHOULD BE PLACED BETWEEN EACH ENCODED CHARACTER.

A  SHOULD BE PLACED BETWEEN EACH WORD.

LINE BREAKS IN THE INPUT FILE SHOULD BE REPLICATED IN THE ENCODED FILE.

Acknowledgements

This assignment was written by Dr. Jay Urbain.

Lab Deliverables

See your professor's instructions for details on submission guidelines and due dates.
Dr. Hasker's instructions
Dr. Taylor's class: See below
Dr. Yoder's submission instructions
If you have any questions, consult your instructor.

Tuesday, 16-Feb-2016 00:32:50 EST