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: 3555495 • 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 signaturefromMorseCode(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. TheMorseCode 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:

The code for the conversion tabel above is given below:

Explanation / Answer

The following is the complete program as per your requirements.

import java.util.Scanner;

public class TestMorseCode
{
   public static void main(String[] args)
   {
       char ch;
       String message;
       MorseCode m = new MorseCode();
       Scanner in = new Scanner(System.in);
       System.out.println("Morse Code Conversion Program.");
       System.out.println("This program reads a phrase in English "
               + "(or Morse code) and prints its equivalent "
               + "in Morse code (or English)");
       System.out.println("If your original message is in "
                   + "English, please enter an E. If it"
                   + "is in Morse code, please enter an M.");
       System.out.print("Enter any other character to quit: ");
       ch = in.next().charAt(0);  
       if (ch == 'E')
       {
           System.out.println("Please enter a phrase in English, "
                   + "followed by a carriage return: ");
           message = in.next();
           System.out.println("The Morse code for your phrase is:");
           for(int i=0; i<message.length(); i++)
           {
               System.out.print(m.toMorseCode(message.charAt(i)));
           }
           System.out.println("Goodbye.");
       }
       else if (ch == 'M')
       {
           System.out.println("Please enter a phrase in Morse "
                   + "code, followed by a carriage return:");
           message = in.nextLine();
          
           for(int i=0; i<message.length(); i++)
           {
               char l = message.charAt(i);
               if(l!=' ' || l!='.'||l!='-' )
               {
                   message=message.substring(0, i);
                   break;
               }
           }
           System.out.println("Your phrase in English is:");
           for(int i=0; i<message.length(); i=i+4)
           {
               char msg[] = new char[4];
               msg = message.substring(i, i+4).toCharArray();
               System.out.print(m.fromMorseCode(msg));
           }
           System.out.println("Goodbye.");
       }
       else
           System.out.println("Goodbye.");
  

   }

}

class MorseCode
{
   static char letters[]={'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'};
   static char morse[][];

   MorseCode()
   {
       morse = new char[26][4];
       for (int i = 0; i < 26; i++)
       {
           for (int j = 0; j < 4; j++)
           {
               morse[i][j] = ' ';
           }
       }
       morse[0][0] = '.'; // A
       morse[0][1] = '-';
       morse[1][0] = '-'; // B
       morse[1][1] = '.';
       morse[1][2] = '.';
       morse[1][3] = '.';
       morse[2][0] = '-'; // C
       morse[2][1] = '.';
       morse[2][2] = '-';
       morse[2][3] = '.';
       morse[3][0] = '-'; // D
       morse[3][1] = '.';
       morse[3][2] = '.';
       morse[4][0] = '.'; // E
       morse[5][0] = '.'; // F
       morse[5][1] = '.';
       morse[5][2] = '-';
       morse[5][3] = '.';
       morse[6][0] = '-'; // G
       morse[6][1] = '-';
       morse[6][2] = '.';
       morse[7][0] = '.'; // H
       morse[7][1] = '.';
       morse[7][2] = '.';
       morse[7][3] = '.';
       morse[8][0] = '.'; // I
       morse[8][1] = '.';
       morse[9][0] = '.'; // J
       morse[9][1] = '-';
       morse[9][2] = '-';
       morse[9][3] = '-';
       morse[10][0] = '-'; // K
       morse[10][1] = '.';
       morse[10][2] = '-';
       morse[11][0] = '.'; // L
       morse[11][1] = '-';
       morse[11][2] = '.';
       morse[11][3] = '.';
       morse[12][0] = '-'; // M
       morse[12][1] = '-';
       morse[13][0] = '-'; // N
       morse[13][1] = '.';
       morse[14][0] = '-'; // O
       morse[14][1] = '-';
       morse[14][2] = '-';
       morse[15][0] = '.'; // P
       morse[15][1] = '-';
       morse[15][2] = '-';
       morse[15][3] = '.';
       morse[16][0] = '-'; // Q
       morse[16][1] = '-';
       morse[16][2] = '.';
       morse[16][3] = '-';
       morse[17][0] = '.'; // R
       morse[17][1] = '-';
       morse[17][2] = '.';
       morse[18][0] = '.'; // S
       morse[18][1] = '.';
       morse[18][2] = '.';
       morse[19][0] = '-'; // T
       morse[20][0] = '.'; // U
       morse[20][1] = '.';
       morse[20][2] = '-';
       morse[21][0] = '.'; // V
       morse[21][1] = '.';
       morse[21][2] = '.';
       morse[21][3] = '-';
       morse[22][0] = '.'; // W
       morse[22][1] = '-';
       morse[22][2] = '-';
       morse[23][0] = '-'; // X
       morse[23][1] = '.';
       morse[23][2] = '.';
       morse[23][3] = '-';
       morse[24][0] = '-'; // Y
       morse[24][1] = '.';
       morse[24][2] = '-';
       morse[24][3] = '-';
       morse[25][0] = '-'; // Z
       morse[25][1] = '-';
       morse[25][2] = '.';
       morse[25][3] = '.';
   }
  
   public char[] toMorseCode(char ch)
   {
       char mcode[] = new char[4];
       switch(ch)
       {
       case 'A':
           mcode = morse[0];
           break;
       case 'B':
           mcode = morse[1];
           break;
       case 'c':
           mcode = morse[2];
           break;
       case 'D':
           mcode = morse[3];
           break;
       case 'E':
           mcode = morse[4];
           break;
       case 'F':
           mcode = morse[5];
           break;
       case 'G':
           mcode = morse[6];
           break;
       case 'H':
           mcode = morse[7];
           break;
       case 'I':
           mcode = morse[8];
           break;
       case 'J':
           mcode = morse[9];
           break;
       case 'K':
           mcode = morse[10];
           break;
       case 'L':
           mcode = morse[11];
           break;
       case 'M':
           mcode = morse[12];
           break;
       case 'N':
           mcode = morse[13];
           break;
       case 'O':
           mcode = morse[14];
           break;
       case 'P':
           mcode = morse[15];
           break;
       case 'Q':
           mcode = morse[16];
           break;
       case 'R':
           mcode = morse[17];
           break;
       case 'S':
           mcode = morse[18];
           break;
       case 'T':
           mcode = morse[19];
           break;
       case 'U':
           mcode = morse[20];
           break;
       case 'V':
           mcode = morse[21];
           break;
       case 'W':
           mcode = morse[22];
           break;
       case 'X':
           mcode = morse[23];
           break;
       case 'Y':
           mcode = morse[24];
           break;
       case 'Z':
           mcode = morse[25];
           break;
       default:
           break;
              
       }
       return mcode;
   }
  
   public static char fromMorseCode(char code[])
   {
       for(int i = 0; i<26; i++)
           if(code.equals(morse[i]))
               return letters[i];
       return ' ';
   }
}


Sample output:

Morse Code Conversion Program.
This program reads a phrase in English (or Morse code) and prints its equivalent in Morse code (or English)
If your original message is in English, please enter an E. If itis in Morse code, please enter an M.
Enter any other character to quit: E
Please enter a phrase in English, followed by a carriage return:
HELLO
The Morse code for your phrase is:
.....   .-...-..--- Goodbye.

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