In Java:This project involves writing a program to translate Morse code into Eng
ID: 3909341 • Letter: I
Question
In Java:This project involves writing a program to translate Morse code into English and English 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. Source code must include assignment of characters into array. Do not use a file reader.When you input Morse code, separate each letter/digit with a single space, and delimit multiple words with a “|”. For example, - --- | -... . would be the Morse code input for 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.
Character code Character code DigitsExplanation / Answer
The Below Java Code functions well. The only thing to do is add all the values from the Given Table to the Array present in the constructor. Few inputs could be taken from User to make it more compact.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class TranslationImpl{
public static void main(String s[]){
try{
System.out.println("Please select the type of Translation 1. English to Morse or 2. Morse to English");
Translation t=new Translation();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String userInput = br.readLine();
//String userInput="1";
if(userInput=="1"){
t.englishToMorse("abc A"); // this can be promted from the user rather than a hardcode
}
else if(userInput=="2"){
t.morseToEnglish(".- -... -.-.|.-"); // this can be promted from the user rather than a hardcode
}
else{
System.out.println("Please select a valid Input");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
class Translation{
private String[] english;
private String[] morse;
Translation()
{
english=new String[]{"A","B","C"}; // Add all the english aplhabet given in the array from Table D,E,F,......1,2,...9,0
morse=new String[]{".-","-...","-.-."}; // Add corresponding morse alphabet in this array in the same order
}
public void englishToMorse(String s)
{
String words[]=s.split(" ");
for(int i=0;i<words.length;i++)
{
int wordLength= words[i].length();
for(int j=0;j<wordLength;j++)
{
for(int k=0;k<english.length;k++)
{
if(Character.toString(words[i].charAt(j)).equalsIgnoreCase(english[k])){
System.out.print(morse[k]);
k=english.length;
}
if(j!=wordLength-1)
System.out.print(" ");
}
}
if(i!=words.length-1)
System.out.print("|");
}
}
public void morseToEnglish(String s){
String words[]=s.split("\|");
for(int i=0;i<words.length;i++){
String letters[]=words[i].split(" ");
for(int j=0;j<letters.length;j++)
{
for(int k=0;k<morse.length;k++){
if(letters[j].equals(morse[k]))
{
System.out.print(english[k]);
k=morse.length;
}
}
}
if(i!=words.length-1)
System.out.print(" ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.