I need some help with a java application the idea is to crea
I need some help with a java application the idea is to create an application which is able to translate between back and forth between English – letters A-Z and numbers 0-9 – and Morse code – dots “.” and dashes “-“. The user will be prompted to enter something to translate. The user may enter either English or Morse code. The application will then analyze what the user entered to try and figure out if the user entered English or Morse code. If the analysis determines the user entered something in English, it will then translate it into Morse code. If the analysis determines the user entered something in Morse code, it will then translate it into English. At any time, the user may enter “-1” to exit the application.
HINT: Morse code is only dots “.” Dashes “-“ and spaces “ “. If the user input contains these 3 characters and only these 3 characters, it’s Morse code. Otherwise, assume it’s English.
Solution
MorseTranslator.java
import java.io.*;
import java.util.*;
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;
}
}
__________________________
Main.java
import java.util.*;
class Main {
public static void main(String[] args) {
MorseTranslator message = null;
//Scanner class Object is used to read the inputs entered by the user
Scanner sc = new Scanner(System.in);
while(true)
{
//Getting the String entered by the user
System.out.print(\"\ Enter the string to conver to Morse Code (-1 to exit): \");
String original = sc.nextLine();
if(original.equals(\"-1\"))
{
System.out.println(\":: Program Exit ::\");
System.exit(0);
}
else
{
//Creating the MorseTranslator class object
message = new MorseTranslator(original);
//Displaying the converted string to morse code
System.out.println(\"The morse code equivalent of \" + message.getOriginal() + \" is:\ \" + message.getMorseCode());
}
//Getting the String entered by the user
System.out.print(\"\ Enter the morse code to convert to String (-1 to exit)::\");
String morse=sc.nextLine();
if(morse.equals(\"-1\"))
{
System.out.println(\":: Program Exit ::\");
System.exit(0);
}
else
{
//Displaying the converted morse to string
System.out.println(\"The Equivalent String is :\"+message.getString(morse));
}
}
}
}
______________________
Output:
Enter the string to conver to Morse Code (-1 to exit): hello
The morse code equivalent of hello is:
.... . .-.. .-.. ---
Enter the morse code to convert to String (-1 to exit)::.... . .-.. .-.. ---
The Equivalent String is :hello
Enter the string to conver to Morse Code (-1 to exit): bye
The morse code equivalent of bye is:
-... -.-- .
Enter the morse code to convert to String (-1 to exit)::-... -.-- .
The Equivalent String is :bye
Enter the string to conver to Morse Code (-1 to exit): -1
:: Program Exit ::
_________________Thank You




