Program Description: A Java program is to be created to produce Morse code. The
ID: 3632975 • Letter: P
Question
Program Description: A Java program is to be created to produce Morse code. The Morse code assigns aseries of dots and dashes to each letter of the alphabet, each digit, and a few special characters (such as
period, comma, colon, and semicolon). In sound-oriented systems, the dot represents a short sound and the
dash represents a long sound. Separation between words is indicated by a space, or, quite simply, the
absence of a dot or dash. In a sound-oriented system, a space is indicated by a short period of time during
which no sound is transmitted. The international version of the Morse code is stored in the data file Morse.txt.
To process the program, the Morse code data file (Morse.txt) should be read and stored in memory for fast
access to the code. Each letter of the alphabet has a Morse code equivalent. After the code table is stored, the
user should be prompted for an English language sentence to be entered from the keyboard. This sentence
should be encoded into Morse and displayed. One blank should be used to separate each Morse-coded letter
and three blanks should be used to separate each Morse-coded word. The user should be allowed to continue
with the process of entering a sentence and having it encoded until a sentinel value (0) is received.
Input: The letters and their equivalents are stored in a data file named Morse.txt. Input will consist of the
Morse.txt file and well as the sentences entered from the keyboard. The data file should be read and loaded
into memory at the beginning of the program before the sentences can be read and encoded. Each line of the
data file contains the letter of the alphabet followed by the code equivalent. The data file should be read and
stored in an array for fast and easy access during the program duration. When a sentence is read from the
keyboard, it can be translated from the data stored from the Morse code file.
Sample of Morse.txt:
A .-
B -…
C -.-.
D -..
E .
.
.
.
Z --..
Output: Output will consist of the display of the original sentence and it Morse code equivalent.
Requirements:
? Create a driver (main) class named Morse.java.
? Create a utility class named Translate.java which translates the sentences inputted from the user into
Morse code.
? You may design your utility class where you pass the entire sentence into its constructor or pass one
character at a time. You may elect to do output from either the driver or utility class.
? Include comments defining the purpose of the code.
? Include a comment stating your name.
? Use all Java conventions such as naming conventions and indentation.
Hints:
? In the beginning of the program, read the Morse.txt file and store the data items in parallel arrays.
? When reading the data from the line, the next from the Scanner class can be used since the data items
are String data type.
? After the file is read and loaded into memory, the user can begin to enter the sentences to be coded.
? Continue to process until the user has finished entering sentences.
Morse txt
1 .----
2 ..---
3 ...--
4 ....-
5 .....
6 -....
7 --...
8 ---..
9 ----.
0 -----
A .-
B -...
C -.-.
D -..
E .
F ..-.
G --.
H ....
I ..
J .---
K -.-
L .-..
M --
N -.
O ---
P .--.
Q --.-
R .-.
S ...
T -
U ..-
V ...-
W .--
X -..-
Y -.--
Z --..
Explanation / Answer
//Morse.java
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
/**
* @author (Replace your name here)
*/
public class Morse
{
//Main function to start the execution
public static void main(String[] args) throws IOException
{
//opening the file and creating the reader for reading the text from the file
FileInputStream fInputStream = new FileInputStream("c:\Morse.txt");
DataInputStream in = new DataInputStream(fInputStream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
//Parallel arrays for storing Character set and their Morse equivalents
String[] CharSet = new String[36];
String[] MorseCharSet = new String[36];
int nCounter = 0;//for tracking array indexes
//Reading the input file line by line and populate the arrays
String line = "";
while ((line = br.readLine()) != null)
{
//Split the string based on space
//First element normal char and second is Morse equivalent
String[] temp= line.split(" ");
CharSet[nCounter] = temp[0];
MorseCharSet[nCounter] = temp[1];
nCounter++;
}
System.out.println("Enter a input string to convert >> ");
Scanner input = new Scanner(System.in);
StringBuilder inputString = new StringBuilder();
//Reading multiple lines and creating the input string for conversion
while(true)
{
String tmpString = input.nextLine().toString();
if(tmpString.contains("(0)"))
{
tmpString = tmpString.replace("(0)", "");
inputString.append(tmpString);
break;
}
inputString.append(tmpString).append(" ");
}
//Creating translate object to convert the text
Translate oTranslate = new Translate(inputString.toString(), CharSet, MorseCharSet);
String result = oTranslate.Convert();
System.out.println(inputString.toString()+ " " + result);
}
}
//Translate.java
/**
* @author (Replace your name here)
*
* This class is used to convert the plain text to morse code
*/
public class Translate
{
//Private variable for storing the input
private String _input = "";
//Parallel arrays for storing characters and their morse equivalents
private String[] _chars = null;
private String[] _morseChars = null;
//Constructor
public Translate(String strinput,String[] arrChar, String[] arrMorseChar)
{
_input = strinput;
_chars = arrChar;
_morseChars = arrMorseChar;
}
//Constructor
public Translate(char ch, String[] arrChar, String[] arrMorseChar)
{
_input = "" + ch;
}
//This function converts the string to morse and returns the converted morse
public String Convert()
{
//Length of the input sentence
int nInputStringLength = _input.length();
//Convert input sentence to Char array
char[] inputChar = _input.toCharArray();
//String builder variable for storing the output
StringBuilder sb = new StringBuilder();
//Loop through each character and convert to morse. save the result
//back to result object
for(int nIndex = 0; nIndex < nInputStringLength; nIndex++)
{
//if the input character is a space then add three spaces to the output.
if(("" + inputChar[nIndex]) == " ")
{
sb.append(" ");
}
else
{
//If input is not a space then convert it to morse code and
//add that to output and also add a space for each morse character
String tmp = getMorseChar(inputChar[nIndex]);
sb.append(tmp).append(" ");
}
}
return sb.toString();
}
//This function converts a single character to morse equivalent
private String getMorseChar(char ch)
{
//Length of the morse codes array
int nLength = _chars.length;
//Loop through the morse code array and search for the input character
//In it founds then get the morse code and return to the caller
for(int nIndex = 0; nIndex < nLength; nIndex++)
{
if(_chars[nIndex].toLowerCase().equals((""+ch).toLowerCase()))
{
return _morseChars[nIndex];
}
}
return "";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.