Lab 11: Morse Code Encoder

Learning Outcomes

Overview

Video overview

In this lab, you will implement a program that encodes text messages into Morse code. You will use a map that you implement yourself to look up the Morse code associated with each character.

Your program's GUI will be similar to the one from Lab 9, with menu items used to select actions. The interface should match the supplied example as closely as reasonably possible. Minor platform-specific differences in fonts, spacing, and native control appearance are acceptable.

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 addition to the MorseEncoderDriver class that launches the GUI, you will write and test three classes: MorseEncoder, HashMap, and Controller.

MorseEncoderDriver

Create a MorseEncoderDriver class that extends Application.

It must:

MorseEncoder

MorseEncoder is a utility class that loads the Morse Code table, encodes messages, and provides the tone-playing method used by the GUI.

The class must contain a private static map named MAP:

private static final HashMap<Character, String> MAP = new HashMap<>();

MorseEncoder must contain the following methods:

HashMap

Your HashMap<K, V> class must implement the Map<K, V> interface.

This implementation uses chaining to handle collisions. The backing table must be an array named entries with a length of 1024. Each array location is either null or refers to a list of Map.Entry<K, V> objects. Keys that hash to the same array index are stored in the same list.

The class must define a private nested HashMapEntry<K, V> class that implements Map.Entry<K, V>.

Use the key's hashCode() to determine the array index. Negative hash codes must still result in valid non-negative array indexes.

Keys may not be duplicated. Calling put() with a key that is already present must replace the value associated with that key without changing the size of the map.

This implementation does not need to resize the backing array or track collision statistics.

null keys are not supported. null values are permitted.

Your implementation must provide working implementations of the following Map methods. The remaining Map methods may throw an UnsupportedOperationException.

Follow the Map contract for return values. In particular:

Controller

Your Controller class must implement the Initializable interface and provide at minimum the following functionality:

If any characters cannot be encoded, the Controller must skip them, continue encoding the remaining message, and display one Alert after encoding is complete containing all warning messages returned by encodeMessage().

GUI Requirements

The GUI must be defined in morseEncoder.fxml.

The interface should contain two large, non-editable text areas:

There must be three menus: File, Edit, and Help.

File contains:

Edit contains:

Help contains:

Save and Play must be disabled whenever there is no encoded message displayed, including when the application first starts and after Clear is selected.

Each menu item must be usable with either the mouse or the platform shortcut key (Command on macOS, Ctrl on Windows/Linux):

The contents of an opened text file must be preserved exactly. Do not add or remove line breaks while reading the source file.

Morse Playback

When playing the encoded message, use Thread.sleep(int milliseconds) to produce silence between encoded characters and between lines.

Use the following timing:

Spaces and newline characters have their own Morse codes and must be played as normal encoded tokens. In addition, pause between encoded characters and after each encoded line as described above.

See your professor's instructions for details on submission guidelines and due dates.

Acknowledgements

This assignment was developed by Dr. Jay Urbain, Dr. Chris Taylor, Dr. RJ Nowling, and Prof. Sean Jones.