Edit View History Bookmarks People Window Help Computer Science I-Sp 1 tps:/Monl
ID: 3800638 • Letter: E
Question
Edit View History Bookmarks People Window Help Computer Science I-Sp 1 tps:/Monline.okstate.edu/d2llewoontent/78689Mullscreenve39747New m4Morsee Code Computer Science 1 Java Programming Dr. David Cline Program 4: Morse Code Morse code represents letters and numbers using two symbols (dots and dashes), with a space between encodings of letters. In this assignment, you will convert messages to and from morse code. Things you will learn Input and output redirection Working in multiple modes based on command line arguments Reading input and formatting output. Extracting characters from an input file Specification In this assignment, you will implement an encoder/decoder for morse code. Each letter or digit in morse code is specified as a sequence of dots and dashes Symbols are separated by a space. We have also augmented normal Morse code to include a few extra codes for spaces and punctuation. The encoding of the letters and numbers is givenExplanation / Answer
package Code.graphics.morseCode;
import java.awt.TextArea;
import java.awt.TextField;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import edu.cmu.ri.createlab.terk.robot.finch.Finch;
public class MorseCodeBeeper
{
static final String[] MorseLookup = {".-", "-...", "-.-.", "-..", ".", "..-.",
"--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--",
"....-", ".....", "-....", "--...", "---..", "----."};
static final String[] asciiLookup = {"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", "0", "1", "2", "3", "4",
"5", "6", "7", "8", "9"};
final int ENCRYPT = 'e';
final int DECRYPT = 'd';
final int EXIT = 'x';
boolean STRICTMORSE = false;
TextArea messageOutput;
TextField morseOutput;
public MorseCodeBeeper() throws IOException
{
final Finch finch = new Finch();
final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String Message;
String Morse;
boolean isValid;
int edxOpt:
double[] accelVals;
double beepThresh, beepDiff;
System.out.println("Hello, Dave.");
System.out.println("Can you set up my accelerometer, please?");
accelVals = MCB_setAccelerometer(in, finch);
beepThresh = (accelVals[0] + accelVals[1]) / 2;
beepDiff = accelVals[0] - accelVals[1];
if (beepDiff < 0)
{
beepDiff = -beepDiff;
}
System.out.println("Now then, what can I help you with today?");
while (true)
do
{
isValid = true;
System.out.println("Enter 'e' to encrypt, 'd' to decrypt, 'i' to input by" +
" bouncing the finch, or 'x' to exit:");
0 edxOpt = in.read();
if (edxOpt == EXIT)
{
System.out.println("Daisy, Daisy, give me your answer truuuuuuuuuu...");
finch.quit();
System.exit(0);
}
else if (edxOpt != 'e' && edxOpt != 'd' && edxOpt != 'i')
{
System.out.println("I can't let you do that, Dave.");
isValid = false;
}
while (in.ready())
{
in.read();
}
}
while (!isValid);
if (edxOpt == 'e')
{
finch.setLED(255, 0, 0);
Morse = MCB_GetMessage(in);
System.out.println("The encrypted message is");
System.out.println(Morsse);
MCB_PlayMorse(Morse, finch);
}
if (edxOpt == 'd')
{
finch.setLED(0, 0, 255);
Message = MCB_Decrypt(in);
System.out.println("The decrypted message is");
System.out.println(Message);
}
if (edxOpt == 'i')
{
System.out.println("Use the tail of the finch to input your message. Press any" +
" key to end.");
double pressed;
boolean wasPressed = false;
finch.setLED(0, 255, 0);
MorseCodeFrame frame = new MorseCodeFrame();
frame.setVisible(true);
while (true)
{
pressed = finch.getXAcceleration();
if (pressed > beepThresh)
frame.beepStateChanged(true, MorseCodeFrame.TAIL);
finch.buzz(frame.getFreq(), 50);
}
else
{
frame.beepStateChanged(false, MorseCodeFrame.TAIL);
}
if (in.ready())
{
in.read();
break;
}
}
frame.dispose();
System.out.println("All I can do is beep, Dave.");
}
System.out.println("");
}
}
double[] MCB_setAccelerometer(BufferedReader in, Finch finch) throws IOException
{
double[] minMax = {0, 0};
System.out.println("Place the tail of the Finch onto something you can press it" +
" down on and will bounch the tail back up when you release it.");
System.out.println("Let it sit unpressed and hit return.");
in.readLine();
minMax[1] = finch.getXAcceleration();
System.out.println("Now hold the tail of the finch down and hit return again.");
in.readLine();
minMax[0] = finch.getXAcceleration();
return minMax;
}
String MCB_GetMessage(BufferedReader in) throws IOException
{
String Morse = "";
int currChar;
int MorseIndex;
System.out.println("Enter the message to encrypt. Only letters, spaces, and" +
" numbers will be processed. All other characters will be" +
" ignored. Press 'Enter' when finished.");
do
{
currChar = in.read();
if (currChar >= 'a' && currChar <= 'z')
{
MorseIndex = currChar - 'a';
}
else if (currChar >= 'A' && currChar <= 'Z')
{
MorseIndex = currChar - 'A';
}
else if (currChar >= '0' && currChar <= '9')
{
MorseIndex = currChar - '0' + 26;
}
else if (currChar == ' ')
{
Morse = Morse + " ";
continue;
}
else
{
continue;
}
Morse = Morse + MorseLookup[MorseIndex] + " ";
}
while (currChar != 13 && currChar != 10);
return Morse;
}
String MCB_Decrypt(BufferedReader in) throws IOException
{
String Message = "";
int currChar;
boolean wasSpace = false;
String currLetter = "";
System.out.println("Enter your message in morse code using '.' for dots and '-' for" +
" dashes. Put a single space between letters and a double space" +
" between words.");
do
{
currChar = in.read:
if (currChar == '.')
{
currLetter += ".";
}
else if (currChar == '-')
{
currLetter += "-";
}
if (currChar == ' ' || currChar == 13 || currChar == 10)
{
if (currLetter.length() > 0)
{
for (int i = 0; i < MorseLookup.length; i++)
{
if (currLetter.compareTo(MorseLookup[i]) == 0)
{
Message += asciiLookup[i];
letterFound = true;
break;
}
}
if (!letterFound)
{
System.out.printf("%s was not recognized and will be ignored ", currLetter);
}
currLetter = "";
}
if (wasSpace)
{
Message += "
wasSpace = false;
}
else
{
wasSpace = true;
}
}
else
{
wasSpace = false;
}
}
while (currChar != 13 && currChar != 10)"
return Message;
void MCB_PlayMorse(String Morse, Finch finch)
{
int> int frequency = 600;
for (int i = 0; i < Morse.length(); i++)
{
if (Morse.charAt(i) == ' ')
{
if ((i + 1) < Morse.length() && Morse.charAt(i + 1) == ' ')
{
MCB_DelayTicks(6);
while (i + 1 < Morse.length() && Morse.charAt(i + 1) == ' ')
{
i++;
}
}
else
{
MCB_DelayTicks(2);
}
}
else if (Morse.charAt(i) == '.')
{
finch.buzz(frequency, oneTick);
MCB_DelayTicks(2 * oneTick);
}
else if (Morse.charAt(i) == '-')
{
finch.buzz(frequency, oneTick * 3);
MCB_DelayTicks(4 * oneTick);
}
}
}
void MCB_DelayTicks(int ticks)
{
try
{
Thread.sleep(ticks);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
public static void main(final String[] args) throws IOException
{
new MorseCodeBeeper();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.