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

Morse Code and Exceptions Objective: in eclipse The letters of the alphabet A th

ID: 3683618 • Letter: M

Question

Morse Code and Exceptions

Objective: in eclipse

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:

Description:

Create a class called MorseCode that at least has methods with the following signatures:

- toMorseCode(char letter) - accepts a single capital letter from the English alphabet and returns an array of 4 characters representing the Morse code equivalent of that letter.

- fromMorseCode(char[] arr) - 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:

- char[] alphabet - An array of all 26 letters in the alphabet.

- char[][] morseCodeAlphabet - an array of arrays of char, storing the morse code entries for all 26 letters of the alphabet.

Create a class AlphabetException. This class should extend Exception, and be used in case the user enters an invalid character in EITHER mode. For example, if the user wants to convert from English to Morse and enters a lower-case letter, your program should throw an AlphabetException.

Create a driver class called TestMorseCode. At a minimum

1. The program should print its name

2. The program should (continually) ask the user to whether the original phrase will be in English or Morse

3. For each iteration, the program should ask the user for a phrase (either in English or Morse, depending on the previous choice)

4. For each iteration, the program will print the result of the conversion (either English to Morse or Morse to English)

- If the user enters an invalid character, your program should check for an AlphabetException and print an appropriate message.

- After the error, your program should continue on the next iteration of the main loop (i.e. your program should NOT quit if the user enters an invalid phrase)

5. Your source code must conform to good programming style, with Javadoc comments on all public methods.

Example:

Conversion Table:

To save you time typing, the code for the above table is available below

Explanation / Answer

// MorseCode.java

public class MorseCode{
   private static char[][] morseCodeAlphabet = { { '/' }
   , { '.', '-' }
   , { '-', '.', '.', '.' }
   , { '-', '.', '-', '.' }
   , { '-', '.', '.' }
   , { '.' }
   , { '.', '.', '-', '.' }
   , { '-', '-', '.' }
   , { '.', '.', '.', '.' }
   , { '.', '.' }
   , { '.', '-', '-', '-' }
   , { '-', '.', '-' }
   , { '.', '-', '.', '.' }
   , { '-', '-' }
   , { '-', '.' }
   , { '-', '-', '-' }
   , { '.', '-', '-', '.' }
   , { '-', '-', '.', '-' }
   , { '.', '-', '.' }
   , { '.', '.', '.' }
   , { '-' }
   , { '.', '.', '-' }
   , { '.', '.', '.', '-' }
   , { '.', '-', '-' }
   , { '-', '.', '.', '-' }
   , { '-', '.', '-', '-' }
   , { '-', '-', '.', '.' }
   };

   private static char[] alphabet = { ' ', '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' };
   char[] toMorseCode(char letter){
       int i;
       for(i = 0; i < alphabet.length; i++){
           if(alphabet[i] == letter){
               break;
           }
       }
       return morseCodeAlphabet[i];
   }
   char fromMorseCode(char[] arr){
       int i;
       for(i = 0; i < morseCodeAlphabet.length; i++){
           if(morseCodeAlphabet[i].equals(arr)){
               break;
           }
       }
       return alphabet[i];
   }
}

// AlphabetException.java

public class AlphabetException extends Exception{

   public AlphabetException(char charAt) {
       System.out.println("Letter '" + charAt + "' not in alphabet Aborting conversion");
   }
  
}

// TestMorseCode.java

import java.util.Scanner;

public class TestMorseCode{
   public static void main(String args[]){
       MorseCode object = 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)");
       while(true){
           System.out.println("Please select one [1-3]:");
           System.out.println("1: English -> Morse");
           System.out.println("2: Morse -> English");
           System.out.println("3: Quit");
           int choice = in.nextInt();
           String english, morse;
           switch (choice) {
           case 1:
               System.out.println("Enter an English phrase:");
               in.nextLine();
               english = in.nextLine();
               for(int i = 0; i < english.length(); i++){
                   if(english.charAt(i) >= 'A' && english.charAt(i) <= 'Z') System.out.print(object.toMorseCode(english.charAt(i)));
                   else if(english.charAt(i) == ' ') System.out.print(english.charAt(i));
                   else
                       try{
                           throw new AlphabetException(english.charAt(i));
                       } catch (AlphabetException e){
                          
                       }
               }
               System.out.println();
               break;
           case 2:
               // This case may have multiple outputs, based on the arrangement of the morse code. Please specify your algorithm for finding the most suitable one for this, then I'll do it
               break;
           case 3:
               return;
           default:
               System.out.println("Invalid choice");
               break;
           }
       }
   }
}

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