Java morse code to english and english to morse code translator Morse code, deve
ID: 3873722 • Letter: J
Question
Java morse code to english and english to morse code translator
Morse code, developed in 1832 by Samuel Morse, is one of the most famous of all coding schemes ever developed. Morse code assigns a series of dots and dashes to each letter of the alphabet, each digit, and a few other punctuation characters. The international version of Morse code for alphabetic characters and digits is shown in the table below. Character code Character 4 5 6 03 ED 0 Thisproject ihes witing, progam to tandlat Mone code into tngih and tngh into Morse code. Your program shall prompt the user to specify the desired type of translation, input a string of Morse code characters or English characters, then display the translated results. The Morse code pattern and English letter translations must be kept and processed using either two one-dimensional or one two-dimensional arrays. When you input Morse code, separate each letter/digit with a single space, and delimit the sentence "to be". Your program only needs to handle a single sentence and can ignore punctuation symbols. When you input English, separate each word with a blank space.Explanation / Answer
MorseTranslato.java
public class MorseTranslator {
static int NUM_CHARS = 40;
private String original;
private String mcode;
private char[] regular;
private char[] morse;
//Constructs a string original with the given message.
MorseTranslator(String m) {
original = m;
}
//Takes a character, and converts it into its equivalent morse code.
public String toMorse(char ch) {
switch (ch) {
case ' ':
return " ";
case ',':
return "--..--";
case '.':
return ".-.-.-";
case '?':
return "..--..";
case '0':
return "-----";
case '1':
return ".----";
case '2':
return "..---";
case '3':
return "...--";
case '4':
return "....-";
case '5':
return ".....";
case '6':
return "-....";
case '7':
return "--...";
case '8':
return "---..";
case '9':
return "----.";
case 'A':
return ".-";
case 'B':
return "-...";
case 'C':
return "-.-.";
case 'D':
return "-..";
case 'E':
return ".";
case 'F':
return "..-.";
case 'G':
return "--.";
case 'H':
return "....";
case 'I':
return "..";
case 'J':
return ".---";
case 'K':
return "-.-";
case 'L':
return ".-..";
case 'M':
return "--";
case 'N':
return "-.";
case 'O':
return "---";
case 'P':
return ".--.";
case 'Q':
return "--.-";
case 'R':
return ".-.";
case 'S':
return "...";
case 'T':
return "-";
case 'U':
return "..-";
case 'V':
return "...-";
case 'W':
return ".--";
case 'X':
return "-..-";
case 'Y':
return "-.--";
case 'Z':
return "--..";
}
return " ";
}
//Converts the original string, into its equivalent morsecode, and returns.
public String getMorseCode() {
mcode = "";
for (int i = 0; i < original.length(); i++) {
mcode += toMorse(Character.toUpperCase(original.charAt(i))) + " ";
}
return mcode;
}
public String getOriginal() {
return original;
}
public String morseToString(String morse) {
String str = "";
if (morse.equals(""))
str = " ";
else if (morse.equals("--..--"))
str = ",";
else if (morse.equals(".-.-.-"))
str = ".";
else if (morse.equals("..--.."))
str = "?";
else if (morse.equals("-----"))
str = "0";
else if (morse.equals(".----"))
str = "1";
else if (morse.equals(".----"))
str = "2";
else if (morse.equals("..---"))
str = "3";
else if (morse.equals("....-"))
str = "4";
else if (morse.equals("....."))
str = "5";
else if (morse.equals("-...."))
str = "6";
else if (morse.equals("--..."))
str = "7";
else if (morse.equals("---.."))
str = "8";
else if (morse.equals("----."))
str = "9";
else if (morse.equals(".-"))
str = "A";
else if (morse.equals("-..."))
str = "B";
else if (morse.equals("-.-."))
str = "C";
else if (morse.equals("-.."))
str = "D";
else if (morse.equals("."))
str = "E";
else if (morse.equals("..-."))
str = "F";
else if (morse.equals("--."))
str = "G";
else if (morse.equals("...."))
str = "H";
else if (morse.equals(".."))
str = "I";
else if (morse.equals(".---"))
str = "J";
else if (morse.equals("-.-"))
str = "K";
else if (morse.equals(".-.."))
str = "L";
else if (morse.equals("--"))
str = "M";
else if (morse.equals("-."))
str = "N";
else if (morse.equals("---"))
str = "O";
else if (morse.equals(".--."))
str = "P";
else if (morse.equals("--.-"))
str = "Q";
else if (morse.equals(".-."))
str = "R";
else if (morse.equals("..."))
str = "S";
else if (morse.equals("-"))
str = "T";
else if (morse.equals("..-"))
str = "U";
else if (morse.equals("...-"))
str = "V";
else if (morse.equals(".--"))
str = "W";
else if (morse.equals("-..-"))
str = "X";
else if (morse.equals("-.--"))
str = "Y";
else if (morse.equals("--.."))
str = "Z";
return str.toLowerCase();
}
public String getString(String morse) {
String str = "";
String args[] = morse.split(" ");
for (int i = 0; i < args.length; i++) {
str += morseToString(args[i]);
}
return str;
}
}
_______________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int choice;
MorseTranslator message = null;
// Scanner class Object is used to read the inputs entered by the user
Scanner sc = new Scanner(System.in);
String str;
String morse;
String arr[];
String morseCode[];
while (true) {
System.out.println(" 1.String To Morse Code");
System.out.println("2.Morse Code To String");
System.out.println("3.Exit");
System.out.print("Enter choice :");
choice = sc.nextInt();
switch (choice) {
case 1:
{
sc.nextLine();
System.out.print(" Enter String :");
str = sc.nextLine();
arr = str.split(" ");
morseCode = new String[arr.length];
for (int i = 0; i < arr.length; i++) {
message = new MorseTranslator(arr[i]);
morseCode[i] = message.getMorseCode();
}
System.out.println("The correspondiong Morse Code is :");
for (int i = 0; i < arr.length; i++) {
System.out.print(morseCode[i] + " ");
}
continue;
}
case 2:
{
sc.nextLine();
System.out.print(" Enter Morse Code :");
morse = sc.nextLine();
morseCode = morse.split(" ");
arr = new String[morseCode.length];
for (int i = 0; i < morseCode.length; i++) {
message = new MorseTranslator(morseCode[i]);
arr[i] = message.morseToString(morseCode[i]);
}
System.out.println("The correspondiong String is :");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
continue;
}
case 3:
{
System.out.println(" ** Program Exit **");
break;
}
default:
{
System.out.println(" ** Invalid.Must be either 1 or 2 **");
continue;
}
}
break;
}
}
}
___________________
Output:
1.String To Morse Code
2.Morse Code To String
3.Exit
Enter choice :1
Enter String :Hello Sachin.How are you?
The correspondiong Morse Code is :
.... . .-.. .-.. --- ... .- -.-. .... .. -. .-.-.- .... --- .-- .- .-. . -.-- --- ..- ..--..
1.String To Morse Code
2.Morse Code To String
3.Exit
Enter choice :2
Enter Morse Code :.... . .-.. .-.. --- ... .- -.-. .... .. -. .-.-.- .... --- .-- .- .-. . -.-- --- ..- ..--..
The correspondiong String is :
hello sachin.how are you?
1.String To Morse Code
2.Morse Code To String
3.Exit
Enter choice :3
** Program Exit **
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.