Instruction The main body of the program you wrote in the first step translates
ID: 3767465 • Letter: I
Question
Instruction The main body of the program you wrote in the first step translates a single character into its Morse code. Thus, the basic idea in this step is to put this part into a loop, so that this part of the code can be repeatedly executed to translate the characters one by one. Please note that you cannot just copy and reuse the program in step 1 without any modification. You need to pay attention to and modify the input and output parts since (1) the individual characters to be translated in the loop are now from the text string, and (2) your program now needs to put the Morse code on one line. You may use either a for loop or a while loop. The number of iterations is determined by the length of the text string. A framework of the program would be as follows. Please note that it is NOT a finished program and it uses for-construct. You may replace the for-construct with a while-construct. MC_1='.----'; MC_2= ... ... Word=input(...); Word=upper(Word); for index= ... % find out the Morse code of the corresponding character in Word. ... % print out the Morse code ... end ...
Explanation / Answer
import java.util.*;
public class MorseCode
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String userResponse = "";
String english = "English";
String morse = "Morse-Code";
String morseChars = "";
String morseMultiWords = "";
String morseWords = "";
String phrase = "";
String answer = "";
int loop = 0;
final String[] englishArray = {"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"};
final String[] morseArray = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", " ",
".----", "..---", "...--", "....-", ".....", "-....", "--...",
"---..", "----.", "-----"};
while(loop == 0)
{
System.out.print(" Would you like to enter a phrase in English or in Morse-code? ");
userResponse = input.next();
while(!(userResponse.equalsIgnoreCase(english) || userResponse.equalsIgnoreCase(morse)))
{
System.out.println(" Invalid response. Please enter 'English' or 'Morse-code'. ");
System.out.print("Would you like to enter a phrase in English or in Morse-code? ");
userResponse = input.next();
}
if(userResponse.equalsIgnoreCase(english))
{
System.out.print(" Please enter your English phrase: ");
input.nextLine();
phrase = input.nextLine();
System.out.println(" You entered: " + phrase.toUpperCase());
phrase = phrase.toUpperCase();
System.out.print("In morse code, this is: ");
for(int count = 0; count < phrase.length(); count++ )
{
for(int index = 0; index < englishArray.length; index++)
{
if(phrase.substring(count, (count+1)).equals(englishArray[index]))
System.out.print(morseArray[index] + " ");
}
}
}
else if(userResponse.equalsIgnoreCase(morse))
{
System.out.print(" Please enter your Morse-code phrase: ");
input.nextLine();
phrase = input.nextLine();
String[] morseMultipleWords = phrase.split(" ");
System.out.println(" You entered: " + phrase);
System.out.print("In English, this is: ");
for(int i = 0; i < morseMultipleWords.length; i++)
{
morseMultiWords = morseMultipleWords[i];
String[] morseCharacters = morseMultiWords.split(" ");
for(int j = 0; j < morseCharacters.length; j++)
{
morseChars += morseCharacters[j];
for(int index = 0; index < morseArray.length; index++)
{
if(morseChars.equals(morseArray[index]))
morseWords += englishArray[index];
}
morseChars = "";
}
morseWords += " ";
morseMultiWords = "";
}
System.out.println(morseWords);
}
loop++;
System.out.print(" Would you like to enter another phrase? (Y or N): ");
answer = input.next();
while(!(answer.equalsIgnoreCase("Y") || answer.equalsIgnoreCase("N")))
{
System.out.print(" Incorrect input. Please enter either 'Y' or 'N'.");
System.out.print("Would you like to create 20 sentences? (Y or N): ");
answer = input.next();
}
if(answer.equalsIgnoreCase("Y"))
{
morseWords = "";
loop = 0;
}
else
{
System.out.println("Program ended.");
input.close();
}
}
}