Lab 11: Morse Code Encoder
Learning Outcomes
- Have a thorough understanding of commonly used library data structures
- Use data structures in software design and implementation
- Describe how elements are added to a hash table
- Describe the chaining method for dealing with collisions within a hash table
- Write a hash table implementation (using chaining) that includes the following methods:
put(K key, V value)
,containsKey(Object key)
, andget(Object key)
. - Use a hash table to implement a
Map
- Apply the
Map<K, V>
interface defined in the Java Collections Framework
Overview
In this lab, you are going to implement a program that encodes messages written in morse code. You will use a map (that you will implement) to look up the morse code associated with each English character.
Your program's GUI will appear similar to the one from lab 9, however we will be implementing menus in place of buttons to select actions.
Sample Results
Running your program on this 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.
should display the following in an Alert after encoding:
Warning: skipping: * Warning: skipping: * Warning: skipping: ' Warning: skipping: ' Warning: skipping: ' Warning: skipping: ' Warning: skipping: * Warning: skipping: *
and produce the following output:
.- .-... ... .--. .- -.-. . .-... ... .... --- ..- .-.. -.. .-... -... . .-... .--. .-.. .- -.-. . -.. .-... -... . - .-- . . -. .-... . .- -.-. .... .-... . -. -.-. --- -.. . -.. .-... -.-. .... .- .-. .- -.-. - . .-. .-.-.- .-.- .-.- .- .-... .-... .-... ... .... --- ..- .-.. -.. .-... -... . .-... .--. .-.. .- -.-. . -.. .-... -... . - .-- . . -. .-... . .- -.-. .... .-... .-- --- .-. -.. .-.-.- .-.- .-.- .-.. .. -. . .-... -... .-. . .- -.- ... .-... .. -. .-... - .... . .-... .. -. .--. ..- - .-... ..-. .. .-.. . .-... ... .... --- ..- .-.. -.. .-... -... . .-... .-. . .--. .-.. .. -.-. .- - . -.. .-... .. -. .-... - .... . .-... . -. -.-. --- -.. . -.. .-... ..-. .. .-.. . .-.-.- .-.-
Implementation
In this lab, in addition to your MorseDriver
class that will launch your GUI, you write and test three classes: MorseEncoder
, HashMap
, and your Controller
class.
MorseEncoder
Your MorseEncoder
class must contain the following three methods:
static void loadTable(Path path)
— Accepts aPath
object containing themorsecode.txt
file as and argument and populates aMap
object with the codes and their corresponding symbols. The method calls theput()
method from theHashMap
class multiple times in order to populate the hash table.static String encodeMessage(String message)
— Accepts a message and returns the encoded message. Note that this method must handle lowercase characters even if not in the table.playTone(boolean dot)
— This method is provded for you to assist in playing back your encoded message. If thedot
parameter is true, it will play a shortdot
sound. If false, it will play adash
sound that is three times the length of thedot
sound.
The MorseEncoder
class will also contain a HashMap
called MAP
that will store the morse code information.
HashMap
Your HashMap
class must implement the
Map<K, V>
interface
to store each code in a key/value pair where the key is the character
to be encoded and the value is the code associated with the key.
Your implementation of the HashMap
class must include implementations of
the following methods (the remaining methods may throw an
UnsupportedOperationException
).
clear()
containsKey(Object key)
get(Object key)
isEmpty()
put(K key, V value)
remove(Object key)
size()
The HashMap
will consist of a set of HashMapEntry
objects that map the
key (character) to the corresponding symbol (Morse code). For example, one
element in the table will store a mapping from A
(the key) to
.-
(the value); Another element will store a mapping from
B
(the key) to -...
(the value); Etc... The
elements in the table will be sorted based on the keys. E.g., A
,
B
, etc...
Your HashMap
class must have an array of length 1024 for the hash table and be called entries
.
You must handle collisions using chaining. Every element of the array
must be a list of Map.Entry<K, V>
references. You can use the hash code
of the key to determine the array index and store the entry in the associated
list. Multiple keys may map to the same array index but keys may not be
duplicated. You should not worry about tracking the collision rate or
resizing the array.
Controller
Your Controller
class will need to implement the Initializable
interface and define at mimimmum the following functionality
-
public void initialize(URL url, ResourceBundle resourceBundle)
— Loads the Morse Code table once the GUI has been loaded. -
void open()
— will ask the user for the text file to encode, display the message, encode the message, and display the encoded message. -
void save()
— will ask the user for a filename, then save the encoded message as that file. -
void quit()
— will exit the program -
void clear()
— will clear the input and output text from the GUI. -
void about()
— will generate aDialog
window with instructions on how to use the program -
void play()
— will play the encoded message.
Additional details:
- The Morse code table must be read in from the
morsecode.txt
file in thedata
directory in the project. - Your program must convert all characters to uppercase before looking up the corresponding Morse code.
- Your program must not encode characters that are not found in the lookup table. Any characters not in the dictionary must be displayed in an
Alert
window after encoding the message - A space must be placed between each encoded character.
- There will be three menus in your GUI,
File
,Edit
, andHelp
.Clear
will be under theEdit
menu andAbout
will be under theHelp
menu. - The
Save
andPlay
items will be disabled whenever there is no encoded message displayed. - The menu items should be able to be activated either by selecting them with a mouse or using the following keyboard shortcuts.
- meta-O: Open
- meta-S: Save
- meta-P: Play
- meta-Q: Quit
- meta-delete: Clear
- meta-H: About
- When playing the message, use
Thread.sleep(int milliseconds)
to "play" silence between lines. You should play the code for spaces and newline characters, but also pause after each newline. Standard morse code uses the following timings:- Dot: 50 ms
- Dash: 150 ms
- Time between characters: 150 ms
- Time between lines: 450 ms
Acknowledgements
This assignment was developed by Dr. Jay Urbain, Dr. Chris Taylor, Dr. RJ Nowling, and Prof. Sean Jones.