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

JAVA: (Morse Code) Perhaps the most famous of all coding schemes is the Morse co

ID: 3698704 • Letter: J

Question

JAVA:

(Morse Code) Perhaps the most famous of all coding schemes is the Morse code, developed by Samuel Morse in 1832 for use with the telegraph system. The Morse code assigns a series of dots and dashes to each letter of the alphabet, each digit, and a few special characters (e.g., period, comma, colon, semicolon). In sound-oriented systems, the dot represents a short sound and the dash a long sound. Other representations of dots and dashes are used with light-oriented systems and signal-flag systems. Separation between words is indicated by a space or, simply, the absence of a dot or dash. In a sound-oriented system, a space is indicated by a short time during which no sound is transmitted. Write an application that reads an English-language phrase and encodes it into Morse code. Also write an application that reads a phrase in Morse code and converts it into the English-language equivalent. Use one blank between each Morse-coded letter and three blanks between each Morse-coded word.

Explanation / Answer

//MorseCode.Java

import java.io.*;

import java.util.*;

public class MorseCode{

   /* main method throws FileNotFoundException if the input file is not found */

   public static void main(String[] args) throws FileNotFoundException{

       Scanner input = new Scanner(System.in);    

       String userData;

       String result = "";

       String choice = "Y";

       do{

           // prompts the user to enter a character/word/sentence

           System.out.print("Enter a character/word/sentence: ");    

           userData = input.nextLine();

           // create an object for Translate class with user data

           TranslateToMorse trans = new TranslateToMorse(userData);

           // call the readFile method to read the file

           trans.readFile();

           // call the convert method to get the corresponding morse code

           result = trans.convert();

           // display the results

           /* the morse code includes a space between every characters and three spaces instead of input space character */

           System.out.println("User Input: " + userData);

           System.out.println("Morse code: " + result);

           // prompt the user for repetition of the process

           System.out.print("Do you want to repeat? (Y/N): ");

           choice = input.nextLine();

           System.out.println();

       }while(choice.equalsIgnoreCase("Y"));

       System.out.println("Thank you!");

   }

}

//End of MorseCode.Java

//TranslateToMorse.Java

import java.io.*;

import java.util.*;

public class TranslateToMorse{

   // variable to store user input

   private String userInput;

   // parallel arrays to store English and Morse codes

    private String[] charCodes;

    private String[] morseCodes;

    // default constructor

    public TranslateToMorse(){

       userInput = "";

       charCodes = new String[36];

       morseCodes = new String[36];

    }

    // constructor which accepts the input from the user

    public TranslateToMorse(String inputFromUser){

       userInput = inputFromUser;

       charCodes = new String[36];

       morseCodes = new String[36];

    }

    /* passOneChar method accepts one character and appends it to the user input */

    public void passOneChar(char ch){

       userInput += ch;

    }

    /* setUserInput accepts accepts entire input from the user and set to the user input */

    public void setUserInput(String inputFromUser){

       userInput = inputFromUser;

    }

    /* readFile method reads file and stores the data into parallel arrays */

    /* it throws FileNotFoundException if the "Morse.txt" file is not found */

    public void readFile() throws FileNotFoundException{

       Scanner inFile = new Scanner(new File("D:/Morse.txt"));

       String line;

        int count = 0;

        // repeat the loop for all lines in the file

       while(inFile.hasNextLine()){

            line = inFile.nextLine();

            String[] tokens = line.split(" ");

            charCodes[count] = tokens[0];

            morseCodes[count] = tokens[1];

            count++;       

        }

    }

    /* convert method convert the user input into the corresponding morse code and returns the result */

    /* the morse code includes a space between every characters and three spaces instead of input space character */

    public String convert(){

       String result = "";

        for(int i = 0; i < userInput.length(); i++)

           result += getMorseCode(userInput.charAt(i)) + " ";

        return result;

    }

    /* getMorseCode accepts one character and returns its corresponding morse code value */

    /* it returns a space if the input character is a space */

    /* it return "NoCode" if the character is not found the character codes array */

    public String getMorseCode(char ch){

       String str = Character.toString(ch);

       for(int i = 0; i < charCodes.length; i++){

           if(charCodes[i].equalsIgnoreCase(str))

               return morseCodes[i];

       }

       if(ch == ' ')

           return " ";

       return "NoCode";

    }

}// End of TranslateToMorse.java

Morse.txt goes like this which I saved in D drive

1 .----

2 ..---

3 ...--

4 ....-

5 .....

6 -....

7 --...

8 ---..

9 ----.

0 -----

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 --..