HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Morse code translator GUI

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
codeguimorsetranslator

Problem

I made a Morse code translator in Java in which a user inputs a word/words into a text field and the program converts it to Morse code and beeps the Morse code. I have a feeling that my code could be much better, and that it is not efficient enough. Please tell me if there is anything wrong with my code, including efficiency and coding conventions.

```
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class MorseCode extends JFrame implements ActionListener{

char[] alpha = { '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', '1', '2', '3', '4', '5', '6', '7', '8',
'9', '0', ' ' };

String[] dottie = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
"....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
"--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
"-.--", "--..", ".----", "..---", "...--", "....-", ".....",
"-....", "--...", "---..", "----.", "-----", "|" };

public static void play(String filename)
{
try
{
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(new File(filename)));
clip.start();
}
catch (Exception exc)
{
exc.printStackTrace(System.out);
}
}

JTextField wordField;
JTextField morseField;
JButton convertButton;
public MorseCode() {
getContentPane().setLayout(null);
setSize(300, 300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);

JLabel lblEnglish = new JLabel("English");
getContentPane().add(lblEnglish);
lblEnglish.setBounds(10, 11, 46, 14);

wordField = new JTextField();
getContentPane().add(wordField);
wordField.setBounds(10, 36,

Solution

One thing that catches my eye is how you have two arrays of chars/strings mapped to each other through related index locations. I did this myself back in the day, and it quickly became a nightmare to maintain. In your case, you should create a HashMap instead. I would expect your code to look like:

HashMap charToCodeMap = new HashMap();
charToCodeMap.put('a', ".-");
// Initialize here...


Now, instead of iterating through both lists for every character, you can just get the character you need to translate and call, for example, String codeForA = charToCodeMap.get('a'). This will (theoretically) reduce the time needed to run your code drastically, as map access is \$O(1)\$ (unless there are hash collisions), while iterating through a list is \$O(n)\$. However, as Klitos Kyriacou noted in the comments, the actual performance and theoretical performance are not necessarily the same.

Code Snippets

HashMap<char, String> charToCodeMap = new HashMap<char, String>();
charToCodeMap.put('a', ".-");
// Initialize here...

Context

StackExchange Code Review Q#138007, answer score: 7

Revisions (0)

No revisions yet.