Objective: The letters of the alphabet A through Z can be represented in Morse c
ID: 3567899 • Letter: O
Question
Objective:
The letters of the alphabet A through Z can be represented in Morse code. Each letter is represented by a combination of up to four dots and/or dashes, as shown below. Use Java to write and run a console-window program that can convert an English message into Morse code or a Morse code message into English. The program must satisfy the following requirements:
Describe:
Create a class called MorseCode that at least has methods with the following signatures:
The MorseCode class will have two static variables:
To save you time typing, the code for the above table is available here
Create a class AlphabetException. This class should extend Exception, and be used in case the user enters an invalid character in EITHER mode. For example, if the user wants to convert from English to Morse and enters a lower-case letter, your program should throw an AlphabetException.
Create a driver class called TestMorseCode. At a minimum
Example dialogue:
Conversion Table:
Explanation / Answer
Here you go :)
Please comment if you have any doubts.
//MorseCode class
package lab8;
import java.util.Arrays;
public class MorseCode
{
/*
A .- H .... O --- V ...-
B -... I .. P .--. W .--
C -.-. J .--- Q --.- X -..-
D -.. K -.- R .-. Y -.--
E . L .-.. S ... Z --..
F ..-. M -- T -
G --. N -. U ..-
*/
private static char[] alphabet;
private static char[][] morseCodeAlphabet=
{{'.','-'},{'-','.','.','.'},{'-','.','-','.'},{'-','.','.'},{'.'},
{'.','.','-','.'},{'-','-','.'},{'.','.','.','.'},{'.','.'},{'.','-','-','-'},
{'-','.','-'},{'.','-','.','.'},{'-','-'},{'-','.'},{'-','-','-'},
{'.','-','-','.'},{'-','-','.','-'},{'.','-','.'},{'.','.','.'},{'-'},
{'.','.','-'},{'.','.','.','-'},{'.','-','-'},{'-','.','.','-'},{'-','.','-','-'},
{'-','-','.','.'},{' '}};
public MorseCode()
{
alphabet=new char[27];
for(int i=0;i<26;i++)
{
alphabet[i]=(char)(i+65);
}
alphabet[26]='/';
}
/**
* @param Takes a character that needs to be converted to morsecode
* @return Returns a char array that has the morsecode for the given input char
* @throws AlphabetExcaption when the input char is not in the specified list
*/
public static char[] toMorseCode(char letter) throws AlphabetException
{
char[] morse={'/'};
if((letter<65 || letter >90) && letter!=' ')
{
throw new AlphabetException(letter);
}
else
{
if(letter!=' ')morse=morseCodeAlphabet[letter-65];
}
return morse;
}
/**
* @return Returns a char equivalent of the given char array
* @parm Takes a char array that has the morsecode
*/
public static char fromMorseCode(char[] arr)
{
char ch=' ';
for(int i=0;i<morseCodeAlphabet.length;i++)
{
if(Arrays.equals(morseCodeAlphabet[i], arr))
{
ch=alphabet[i];
}
}
return ch;
}
}
//AlphabetException class
package lab8;
public class AlphabetException extends Exception
{
public AlphabetException(char a)
{
super("Letter "+ a +" not in alphabet. Aborting conversion.");
}
}
//TestMorseCode class
package lab8;
import java.io.*;
public class TestMorseCode
{
public static void main(String[] args) throws IOException
{
System.out.println("Morse Code Conversion Program.");
System.out.println("This program reads a phrase in English(or Morse code) and prints its equivalent in Morse code (or English)");
BufferedReader sc=new BufferedReader(new InputStreamReader(System.in));
//Initializing the MorseCode variables
MorseCode m=new MorseCode();
int a=0;
while(true)
{
String s="";
System.out.println("Please select one [1-3]: ");
System.out.println("1: English -> Morse");
System.out.println("2: Morse -> English");
System.out.println("3: Quit");
a=Integer.parseInt(sc.readLine());
if(a==3) break;
else if(a==1)
{
System.out.println("Enter an English phrase:");
s=sc.readLine();
try{
String output="";
for(int i=0;i<s.length();i++)
{
output+=new String(MorseCode.toMorseCode(s.charAt(i)))+" ";
}
System.out.println(output);
}catch(AlphabetException e){System.out.println(e.getMessage());}
}
else if(a==2)
{
System.out.println("Enter a Morse phrase: ");
s=sc.readLine();
String[] words=s.split(" ");
String output="";
for(int i=0;i<words.length;i++)
{
output+=MorseCode.fromMorseCode(words[i].trim().toCharArray());
}
System.out.println(output);
}
else System.out.println("Wrong input!");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.