Lab 1760 - Morse Code

Description

In this lab, you will write and test two classes that work with Morse Code: an encoder and a decoder. Here is the Morse Code table associating a letter or symbol with a sequence of dots and/or dashes:


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
--..
0
-----
1
.----
2
..---
3
...--
4
....-
5
.....
6
-....
7
--...
8
---..
9
----.
Period
.-.-.-
Comma
--..--
Slash
-..-.
Query
..--..

The class MorseEncode will provide a method that accepts a text string, such as  PONDEROSA  and returns the Morse Code equivalent. It requies a constructor that creates and populates a TreeMap and one method, encode, that accepts a text string and returns a Morse Code string.

Here is a prototype for the MorseEncode class:

import java.util.TreeMap;

public class MorseEncode
{
    private TreeMap codeMap;

    public MorseEncode()
    {
        . . .
    }

    /**
     *  Inserts a letter and its Morse code string into the encoding map.
     */
    private void addSymbol(char letter, String code)
    {
        . . .
    }

    /**
     *   Converts text into a Morse code message.
     *   Returns the encoded message.
     */
    public String encode(String text)
    {
        . . .
    }

    public static void main(String[] args)
    {
        MorseEncode encoder = new MorseEncode();
        System.out.println(  Ponderosa encodes to   + encoder.encode( Ponderosa ) );
    }
}

The MorseDecode class is more interesting. Here, the task is to convert a series of dots and dashes to their letter, number or symbol equivalents. To do this, build a tree using a TreeNode object and use it the complete tree as a decoder. Here is the tree, partially constructed after processing the letters A through E:

tree

Notice that the resulting tree provides a decoding path. For example, if after becoming fully populated the tree is used to decode a dash-dot-dash-dot sequence, it will go right-left-right-left and recover the letter C. The boxes without letters have not been processed yet. For example, when later processing a single dash, the currently empty box in the upper-right corner will get the character T, which is corresponds to a single dash.

The MorseDecode class is structured much like the MorseEncode class, but it uses a user-made tree of TreeNode objects. The constructor creates the tree and populates it with the help of a private method, treeInsert(char letter, String code). Once the tree has been constructed, the main program can call decode(String morse) to recover the message. Here is a prototype for the MorseDecode class:

public class MorseDecode
{
    private TreeNode decodeTree;
    private static final char DOT = '.';
    private static final char DASH = '-';

    public MorseDecode()
    {
        . . .
    }

    /**
     *  Inserts a letter into the decoding map based on the dot-dash code.
     */
    private void treeInsert(char letter, String code)
    {
        . . .
    }

    /**
     *   Converts Morse code message into text.
     */
    public String decode(String morse)
    {
        . . .
    }

    public static void main(String[] args)
    {
        MorseDecode decoder = new MorseDecode();
        String testString =  .--. --- -. -.. . .-. --- ... .-  ;
        System.out.println( testString +   decodes to   + decoder.decode(testString) );
    }
}

Assignment

Complete the MorseDecode and the MorseEncode classes and test them. You should use the TreeNode class in this project. To save typing, I have created a list of letters, numbers and symbols and their Morse Code equivalent here.

This project was inspired by a lab assignment in Chapter 5 of Litvin's Java Methods AB text.


Copyright © 2010 by Asylum Computer Services LLC Return to CS Labs