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

14.22 ( Morse Code ) Complete this assignment as a JavaFX application with a But

ID: 3724684 • Letter: 1

Question

  • 14.22 (Morse Code) Complete this assignment as a JavaFX application with a Button event handler; use regular expressions to test whether the user has entered an English-language phrase (only letters and blank spaces) or a Morse code message (only dots (.), dashes (-) and blank spaces) (display an error message for any other invalid input); then perform the appropriate coding or decoding and display the result. 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

Hi,

Here I have written the code, you might want to put more error handling and maybe create a jar out of it.
The code takes a properties file which I have included here as an input from project root folder.

The program takes an inputText as input and I have also given a vice vers situation for you.

config.properties

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

MorseCodeConverter.java

package com.chegg.morse.code;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.Enumeration;

import java.util.HashMap;

import java.util.Map;

import java.util.Properties;

public class MorseCodeConverter {

   public static void main(String[] args) {

       Properties prop = new Properties();

       InputStream input = null;

       try {

           boolean isNotString = false;

           boolean isNotMorse = false;

//           String inputText = "Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi";

           String inputText = "-.. ..- .. ... .- ..- - . -- ...- . .-.. . ..- -- .. .-. .. ..- .-. . -.. --- .-.. --- .-. .. -. .... . -. -.. .-. . .-. .. - .. -. ...- ..- .-.. .--. ..- - .- - . ...- . .-.. .. - . ... ... . -- --- .-.. . ... - .. . -.-. --- -. ... . --.- ..- .- - ...- . .-.. .. .-.. .-.. ..- -- -.. --- .-.. --- .-. . . ..- ..-. . ..- --. .. .- - -. ..- .-.. .-.. .- ..-. .- -.-. .. .-.. .. ... .. ... .- - ...- . .-. --- . .-. --- ... . - .- -.-. -.-. ..- -- ... .- -. . - .. ..- ... - --- --- -.. .. --- -.. .. --. -. .. ... ... .. -- --.- ..- .. -... .-.. .- -. -.. .. - .--. .-. .- . ... . -. - .-.. ..- .--. - .- - ..- -- --.. --.. .-. .. .-.. -.. . .-.. . -. .. - .- ..- --. ..- . -.. ..- .. ... -.. --- .-.. --- .-. . - . ..-. . ..- --. .- .. - -. ..- .-.. .-.. .- ..-. .- -.-. .. .-.. .. ... .. ";

           String outputText = "";

           input = new FileInputStream("config.properties");

           // load a properties file

           prop.load(input);

           Map<String, String> morseMap = new HashMap<String, String>();

           Enumeration<?> alphabets = prop.keys();

           // get the property value and print it out

           while (alphabets.hasMoreElements()) {

               String key = (String) alphabets.nextElement();

               String value = prop.getProperty(key);

               morseMap.put(key, value);

           }

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

               char c = inputText.charAt(i);

               if (!morseMap.keySet().contains(Character.toUpperCase(c) + ""))

                   if (c != ' ') isNotString = true;

               if (c != '.' && c != '-' && c != ' ' )

                   isNotMorse = true;

           }

          

if (isNotMorse && isNotString) {

               throw new Error("Please check the input as it seems to have illegal characters");

           } else if (isNotMorse) {

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

                   char c = inputText.charAt(i);

                   if (c == ' ') outputText = outputText + " ";

                   else outputText = outputText + morseMap.get(Character.toUpperCase(c) + "") + " ";

               }

           } else if (isNotString) {

               String words[] = inputText.split(" ");

               for (String word : words) {

                   String letters[] = word.split(" ");

                   for (String letter : letters) {

                       for (String key : morseMap.keySet()) {

                           if (morseMap.get(key).equals(letter)) outputText += key.toLowerCase();

                       }

                   }

                   outputText += " ";

               }

           }

System.out.println(outputText);

       } catch (IOException ex) {

           ex.printStackTrace();

       } finally {

           if (input != null) {

               try {

                   input.close();

               } catch (IOException e) {

                   e.printStackTrace();

               }

           }

       }

   }

}

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