Any attempts must follow the instructions exactly, and must utilize JOptionPane
ID: 3536211 • Letter: A
Question
Any attempts must follow the instructions exactly, and must utilize JOptionPane.
This program will only convert English to Morse Code. It will use JOptionPane. The program will loop until QUIT is requested. eee is the command to Quit. Create separate files for each of the classes described in the attached UML Class diagram.
Following is the Object Oriented design to 'send' the Morse Code. Create the Elements exactly as specified in this design and
it will work.
http://en.wikipedia.org/wiki/Morse_code
International Morse code is composed of five elements:
The unit length in Element is based on the following: "PARIS or CODEX are frequently used as a Morse code standard word. Using the word PARIS as a standard, the number of dot units is 50 and a simple calculation shows that the dot length at 20 words per minute is 60 milliseconds. Using the word CODEX with 60 dot units, the dot length at 20 words per minute is 50 milliseconds."
One simplification is to include an inter-element gap after the dit and the dah. However, the last dit or dah in a Morse character
will have this extra gap which must then be compensated for in the gap between letters and words.
Note that the play() method in the subclasses is a public class method() (static). This being the case, the base class getters and
setters are not accessible because they are NOT class methods.
Either of two things:
Here is the Tone code:
import java.util.*;
import javax.sound.sampled.*;
public class Tone
{
public static float SAMPLE_RATE = 8000f;
public static void sound(int hz, int msecs, double vol)
{
byte[] buf = new byte[(int)SAMPLE_RATE * msecs / 1000];
for (int i=0; i<buf.length; i++) {
double angle = i / (SAMPLE_RATE / hz) * 2.0 * Math.PI;
buf[i] = (byte)(Math.sin(angle) * 127.0 * vol);
}
// shape the front and back 10ms of the wave form
for (int i=0; i < SAMPLE_RATE / 100.0 && i < buf.length / 2; i++)
{
buf[i] = (byte)(buf[i] * i / (SAMPLE_RATE / 100.0));
buf[buf.length-1-i] =
(byte)(buf[buf.length-1-i] * i / (SAMPLE_RATE / 100.0));
}
AudioFormat af = new AudioFormat(SAMPLE_RATE,8,1,true,false);
try
{
SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
sdl.open(af);
sdl.start();
sdl.write(buf,0,buf.length);
sdl.drain();
sdl.close();
}
catch (LineUnavailableException e)
{
System.out.println(e.getMessage());
}
} // end Sound
} // end Tone
Thanks in advance for any help.
Explanation / Answer
import java.io.*;
import java.util.Scanner;
//file writer and print writer to save the output
public class Assignment4
{
@SuppressWarnings("null")
public static void main (String [] args) throws IOException
{
//arrays for letters and Morse Code values
String[] letter = new String[39], morseCode = new String[39];
//this is the file with the letters and morse code equivalent
File file1 = new File ("c:/morseCode.txt");
Scanner in = new Scanner(file1);
int i = 0;
while (in.hasNext())
{
//read in letter
letter[i] = in.next();
//read in Morse Code
morseCode[i] = in.next();
i++;
}
//this is the file with encrypted message
String morseLine, morseLetter, theLetter = " ";
//this file contains the encrypted message
File file2 = new File ("c:/morse.dat");
Scanner data = new Scanner(file2);
//this appends the data to the file and keeps a running input
FileWriter fWriter = new FileWriter("c:/Message.txt", true);
PrintWriter outPutFile = new PrintWriter(fWriter);
boolean found;
int number = morseCode.length;
while (data.hasNext())
{
//reads each line of mesage
morseLine = data.nextLine();
i = 0;
int j = 0, k = 0;
j = morseLine.indexOf(" ");
while (j != -1)
{
//determines the end of a letter in Morse code and stores the
morseLetter = morseLine.substring(i, j);
found = false;
k = 0;
theLetter = " ";
//this loop searches the array of morseCode for the equal Morse code letter
//then it assigns the corresponding letter that is equivalent
while (k < number && !found)
{
if (morseLetter.equals(morseCode[k]))
{
theLetter = letter[k];
found = true;
}
else
{
k++;
}
}
//this condition prints the equivalent letter of the Morse code letter
//on the message
if (!theLetter.equals(" "))
{
System.out.print(theLetter);
outPutFile.print(theLetter);
}
i = j + 1;
j = morseLine.indexOf(" ", i);
}
if(theLetter.equals("."))
{
//moves to the next line at the end of the sentence
System.out.println("");
outPutFile.println("");
}
else
{
//this separates the letters into words
System.out.print(" ");
outPutFile.print(" ");
}
}
outPutFile.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.