In this project, you will write a program to decode the Morse code and translate
ID: 657834 • Letter: I
Question
In this project, you will write a program to decode the Morse code and translate it into plain text.
Instruction
In this project, you will write a program to translate the Morse code of text messages back into plain texts. For simplicity, your program only processes the Morse code for text messages consists of only letters and digits. For example, the Morse code '-- .- - .-.. .- -... / .--. .-. --- --. .-. .- -- -- .. -. --.' will be translated into 'MATLAB PROGRAMMING'. Because Morse code is case insensitive, it is also correct translation if some or all of the letters in the resulting plain text are in lowercases.
At the beginning part the program, you may copy and reuse the following code snippet to define the mapping between letters/digits and their Morse code. Please note that, in the following code, Num1, Num2,..., A, B, ..., Z, etc, are variable names. You should not append them into the plain text (e.g. plain_text = [plain_text A]). Instead, you should add to the plain text the corresponding letters or digits (e.g. plain_text = [plain_text 'A']).
Explanation / Answer
Morse program in Java
import java.util.Scanner;
public class Morse
{
public static void main ( String [] args )
{
Scanner input = new Scanner( System.in );
System.out.print( "Morse to English? " );
String answer2 = input.nextLine();
System.out.println( "Please enter the Morse code you want to translate into English: " );
String code = input.nextLine();
System.out.println( stringToEnglish( code ) );
}
public static String decode (String toEncode) {
String morse = toEncode;
if (toEncode.equalsIgnoreCase(".- "))
morse = "a";
if (toEncode.equalsIgnoreCase("-..."))
morse = "b";
if (toEncode.equalsIgnoreCase("-.-."))
morse = "c";
if (toEncode.equalsIgnoreCase("-.."))
morse = "d";
if (toEncode.equalsIgnoreCase("."))
morse = "e";
if (toEncode.equalsIgnoreCase("..-."))
morse = "f";
if (toEncode.equalsIgnoreCase("--."))
morse = "g";
if (toEncode.equalsIgnoreCase("...."))
morse = "h";
if (toEncode.equalsIgnoreCase(".."))
morse = "i";
if (toEncode.equalsIgnoreCase(".---"))
morse = "j";
if (toEncode.equalsIgnoreCase("-.-"))
morse = "k";
if (toEncode.equalsIgnoreCase(".-.."))
morse = "l";
if (toEncode.equalsIgnoreCase("--"))
morse = "m";
if (toEncode.equalsIgnoreCase("-."))
morse = "n";
if (toEncode.equalsIgnoreCase("---"))
morse = "o";
if (toEncode.equalsIgnoreCase(".--."))
morse = "p";
if (toEncode.equalsIgnoreCase("--.-"))
morse = "q";
if (toEncode.equalsIgnoreCase(".-."))
morse = "r";
if (toEncode.equalsIgnoreCase("..."))
morse = "s";
if (toEncode.equalsIgnoreCase("-"))
morse = "t";
if (toEncode.equalsIgnoreCase("..-"))
morse = "u";
if (toEncode.equalsIgnoreCase("...-"))
morse = "v";
if (toEncode.equalsIgnoreCase(".--"))
morse = "w";
if (toEncode.equalsIgnoreCase("-..-"))
morse = "x";
if (toEncode.equalsIgnoreCase("-.--"))
morse = "y";
if (toEncode.equalsIgnoreCase("--.."))
morse = "z";
if (toEncode.equalsIgnoreCase("-----"))
morse = "0";
if (toEncode.equalsIgnoreCase(".----"))
morse = "1";
if (toEncode.equalsIgnoreCase("..---"))
morse = "2";
if (toEncode.equalsIgnoreCase("...--"))
morse = "3";
if (toEncode.equalsIgnoreCase("....-"))
morse = "4";
if (toEncode.equalsIgnoreCase("....."))
morse = "5";
if (toEncode.equalsIgnoreCase("-...."))
morse = "6";
if (toEncode.equalsIgnoreCase("--..."))
morse = "7";
if (toEncode.equalsIgnoreCase("---.."))
morse = "8";
if (toEncode.equalsIgnoreCase("----."))
morse = "9";
if (toEncode.equalsIgnoreCase(" "))
morse = "";
return morse;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.