Lab 8: 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 must be run from the command line and be given the input and
output filenames. (The data/codeword.txt
file can be hardcoded.)
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 something like this to the console:
> java MouseDriver input.txt encoded.txt Input filename: input.txt Output filename: encoded.txt Encoding file...please wait... Warning: skipping: * Warning: skipping: * Warning: skipping: ' Warning: skipping: ' Warning: skipping: ' Warning: skipping: ' Warning: skipping: * Warning: skipping: *
and produce the following output file:
.- .-... ... .--. .- -.-. . .-... ... .... --- ..- .-.. -.. .-... -... . .-... .--. .-.. .- -.-. . -.. .-... -... . - .-- . . -. .-... . .- -.-. .... .-... . -. -.-. --- -.. . -.. .-... -.-. .... .- .-. .- -.-. - . .-. .-.-.- .-.- .-.- .- .-... .-... .-... ... .... --- ..- .-.. -.. .-... -... . .-... .--. .-.. .- -.-. . -.. .-... -... . - .-- . . -. .-... . .- -.-. .... .-... .-- --- .-. -.. .-.-.- .-.- .-.- .-.. .. -. . .-... -... .-. . .- -.- ... .-... .. -. .-... - .... . .-... .. -. .--. ..- - .-... ..-. .. .-.. . .-... ... .... --- ..- .-.. -.. .-... -... . .-... .-. . .--. .-.. .. -.-. .- - . -.. .-... .. -. .-... - .... . .-... . -. -.-. --- -.. . -.. .-... ..-. .. .-.. . .-.-.- .-.-
Implementation
In this lab, you write and test three classes: MorseDriver
, MorseEncoder
,
and HashMap
.
MorseDriver
Your MorseDriver
class must have a main
method that accepts the two
filenames as command line input. See SLM 7. The design of remainder
of this class is your responsibility.
MorseEncoder
Your MorseEncoder
class must contain the following two methods:
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.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.
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.
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.
Additional details:
- The Morse code table must be read in from the
morsecode.txt
file in thedata
directory in the project (this can be hardcoded). - 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. A warning message must be displayed to the console for any character encountered that is not in the Morse code table.
- A space must be placed between each encoded character.
Acknowledgements
This assignment was developed by Dr. Jay Urbain, Dr. Chris Taylor, and Dr. RJ Nowling.