Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The letters of the alphabet A through Z can be represented in Morse code. Each l

ID: 3554767 • Letter: T

Question

The letters of the alphabet A through Z can be represented in Morse code. Each letter is represented by a combination of up to four dots and/or dashes, as shown below. Use Java to write and run a console-window program that can convert an English message into Morse code or a Morse code message into English. The program must satisfy the following requirements:

Create a class called MorseCode that has at least the following two methods: (1) a method with signature toMorseCode(char) that accepts a single capital letter from the English alphabet and returns an array of 4 characters representing the Morse code equivalent of that letter, and (2) a method with signature fromMorseCode(char[4]) that accepts an array of four Morse code characters (each either a dot, a dash, or space) and returns the English letter equivalent. The MorseCode class will have two static variables. The first, of type char[], will store the 26 letters of the alphabet. The second, of type char[][], will store the Morse code for each of the 26 letters of the alphabet.

Create a driver class called TestMorseCode. At a minimum

Here is an example of its execution:

Conversion Table:

Explanation / Answer

import java.util.Scanner;

public class MorseCode
{

final String[] 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", " "};

final String[] morse = {".-", "-...", "-.-/>.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-/>", ".-..", "--", "-.", "---", ".--.", "--.-/>", ".-.", "...", "-", "..-",
"...-" ,".--" ,"-..-", "-.-/>-", "--..", "|"};
public static void main(String [] args)
{   
Scanner input = new Scanner( System.in );
System.out.print( "Would you like to convert English to Morse (yes or no)? " );
String answer = input.nextLine();
if( answer.equals( "yes" ) )
{
Scanner scan = new Scanner(System.in);
System.out.print("Please enter a String for translation to Morse:");
String letter = scan.nextLine();
System.out.println( toMorse( letter ));
}
if (answer.equalsIgnoreCase( "no" ) )
{
System.out.print( "Morse to English? " );
String answer2 = input.nextLine();
if (answer2.equalsIgnoreCase( "yes" ) )
{
Scanner scan = new Scanner(System.in);
System.out.print("Please enter a String for translation to English:");
String dot = scan.nextLine();

for (int j=0; j < 26; ++j)
{
System.out.println( toAlpha( dot ));
}

}
}
}

public static String toMorse(String letter)
{
final String[] 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", " "};

final String[] morse = {".-", "-...", "-.-/>.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-/>", ".-..", "--", "-.", "---", ".--.", "--.-/>", ".-.", "...", "-", "..-",
"...-" ,".--" ,"-..-", "-.-/>-", "--..", "|"};

for(int i = 0; i < alpha.length; ++i)
{   
char [] chars = letter.toCharArray();

if(letter.equals(alpha[i]))
{
return morse;
}
}
return " "; // not found
}
public static String toAlpha(String dot)
{
final String[] 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", " "};

final String[] morse = {".-", "-...", "-.-/>.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-/>", ".-..", "--", "-.", "---", ".--.", "--.-/>", ".-.", "...", "-", "..-",
"...-" ,".--" ,"-..-", "-.-/>-", "--..", "|"};


for(int i = 0; i < morse.length; ++i)
{
if(dot.equals(morse[i]))
return alpha[i];
}
return " "; // not found
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote