Morse code is a code where each letter of the English alphabet, each digit, and
ID: 3604689 • Letter: M
Question
Morse code is a code where each letter of the English alphabet, each digit, and various punctuation characters are represented by a series of dots and dashes. Write a program that asks the user to enter a file name containing morse code, and then converts that code to text and prints it on the screen. The Morse code table is given in a text file morse.txt. When printing resulting text, display one sentence on each line. There should be no extra spaces at the beginning and at the end of the output.(i need java code)
output should be like this
Explanation / Answer
Note : Could you please check the output .If you required any changes Just intimate.I will modify it.Thank You.
_______________
morse.txt
.... . .-.. .-.. --- ... .- -.-. .... .. -. .-.-.- .... --- .-- .- .-. . -.-- --- ..- ..--..
_______________
MorseTranslator.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;
}
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;
}
}
____________________
MorseToText.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class MorseToText {
public static void main(String[] args) {
MorseTranslator message = null;
//Declaring variables
String filename;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc1=new Scanner(System.in);
Scanner sc;
//Getting the valid filename entered by the user
while(true)
{
System.out.print("Enter filename :");
filename=sc1.next();
try {
sc = new Scanner(new File(filename));
break;
} catch (FileNotFoundException e) {
System.out.println("File '"+filename+"' is not found.");
continue;
}
}
String str;
String morse;
String arr[];
String morseCode[];
String line="";
System.out.println("The correspondiong String is :");
//Converting the morse code to String
while(sc.hasNext())
{
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]);
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
}
sc.close();
sc1.close();
}
}
_____________________
Output:
Enter filename :input2.txt
File 'input2.txt' is not found.
Enter filename :morse.txt
The correspondiong String is :
hello sachin.how are you?
______________Thank YOu
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.