Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program #1: Morse Code (10 points) Please write a program that will convert a me

ID: 3761014 • Letter: P

Question

Program #1: Morse Code (10 points) Please write a program that will convert a message from Morse code into plain English. You can find information about Morse code at the following web page https:flen wikipedia org/wiki/Morse_code The incoming message will be composed of only the 26 letters of the alphabet, and each letter will be separated by a space. The message itself will contain no spaces, it is the (human) receiver's responsibility to figure out where breaks between words should go Hints 1. Define a two-dimensional array to contain the Morse code lookup table. It should begin something like this: char mor s ecode [26] [5] = { " .-" "-.. . " 2. Don't use scanf to receive the code. Remember that it stops receiving after white space 3. Use strtok () to tokenize the code, using only spaces as the delimeters 4. Use a while loop to look up and print each letter of the message 5. Within the while loop, use a for loop to search through each of the 26 letters of the code, using strcmp to see whether it is equal to the current character code. If it is, print out the corresponding letter using a structure such as 'A+i 6. Copy and paste the test messages below from the electronic copy of the exam, oruse a Morse code translator to generate the original code to be input. Sample output for this program will look like this: $ Programl What is vour message in Morse code? That Morse code message corresponds to: FANTASTIC $ Programl What is vour message in Morse code? That Morse code message corresponds to: EXAMINATIOH $ Programl What is vour message in Morse code? That Morse code message corresponds to: THEQUICKBROWNFOXUUMPEDOVERTHELAZYDOOG Please turn in both your program listing (following all good programming practices we have discussed) and your output. Please clearly indicate them as "Program 1 Code" and "Program 1 Output"

Explanation / Answer

MORSE PROGRAM:

public class ProgramMorse
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

String[] alpha = { "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", "1", "2", "3", "4", "5", "6", "7", "8",
"9", "0", " " };
String[] dottie = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
"....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
"--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
"-.--", "--..", ".----", "..---", "...--", "....-", ".....",
"-....", "--...", "---..", "----.", "-----", "|" };

System.out
.println("specify convertion:");

String ans = input.nextLine();

if (ans.equals("English"))
{
System.out.println("Please enter the text if you like to convert to Morse Code: ");
String english = input.nextLine();

char[] translates = (english.toLowerCase()).toCharArray();
System.out.println(toMorse(translates, dottie));

}
else if (ans.equals("Morse"))
{
System.out
.println("enter text if you like to convert to English separate words with '|' :");
String code = input.nextLine();

String[] translates = (code.split("[|]", 0));
System.out.println(toEnglish(translates, alpha));
}
else

System.out.println("You enter Invalid input, try again.");
}

public static String toMorse(char [] translates, String [] dottie)
{
String morse = "";
for (int p = 0; p < translates.length; p++)
{
char ch = translates[p];
if(Character.isLetter(ch))
{
morse = dottie[ch + 'ch'];
}
}
return morse;
}

public static String toEnglish(String [] translates, String [] alpha)
{
String str;
for (int i = 0; i < translates.length; i++)
{
String ch = translates[i];
str = java.util.Arrays.asList(alpha).(Character.toChars(ch + 'ch')[0]);
}
return str;
}
}


spell check

import java.util.ArrayList;
import java.util.Scanner;

public class SpellCheck
{

private Dictionary dict;
final static String filePath = "d:/desktop/words.txt";
final static char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();

SpellCheck() {
dict = new Dictionary();
dict.build(filePath);

}

void run() {
Scanner scan = new Scanner(System.in);
boolean done = false;
String input;

while (true) {
System.out.print(" Enter a word: ");
input = scan.nextLine();
if (input.equals("")) {
break;
}
if (dict.contains(input)) {
System.out.println(" " + input + " is spelled correctly");
} else {
System.out.print("is not spelled correctly, ");
System.out.println(printSuggestions(input));
}
}
}

String printSuggestions(String input)
{
StringBuilder sbr = new StringBuilder();
ArrayList<String> print = makeSuggestions(input);
if (print.size() == 0)
{
return " I have no idea what word you could mean. ";
}
sbr.append("belike you meant: ");
for (String str : print)
{
sbr.append(" -" + str);
}
return sbr.toString();
}

private ArrayList<String> makeSuggestions(String input) {
ArrayList<String> toReturn = new ArrayList<>();
toReturn.addAll(charAppended(input));
toReturn.addAll(charMissing(input));
toReturn.addAll(charsSwapped(input));
return toReturn;
}

private ArrayList<String> charAppended(String input)
{
ArrayList<String> toReturn = new ArrayList<>();
for (char ch : alphabet) {
String atFront = ch + input;
String atBack = input + ch;
if (dict.contains(atFront)) {
toReturn.add(atFront);
}
if (dict.contains(atBack)) {
toReturn.add(atBack);
}
}
return toReturn;
}

private ArrayList<String> charMissing(String input)
{   
ArrayList<String> toReturn = new ArrayList<>();

int len = input.length() - 1;
  
if (dict.contains(input.substring(1)))
{
toReturn.add(input.substring(1));
}
for (int j = 1; j < len; j++) {
  
String working = input.substring(0, j);
working = working.concat(input.substring((j + 1), input.length()));
if (dict.contains(working))
{
toReturn.add(working);
}
}
if (dict.contains(input.substring(0, len)))
{
toReturn.add(input.substring(0, len));
}
return toReturn;
}

private ArrayList<String> charsSwapped(String input)
{   
ArrayList<String> toReturn = new ArrayList<>();

for (int j = 0; j< input.length() - 1; j++)
{
String working = input.substring(0, j);
working = working + input.charAt(j + 1);
working = working + input.charAt(j);
working = working.concat(input.substring((j + 2)));
if (dict.contains(working))
{
toReturn.add(working);
}
}
return toReturn;
}

public static void main(String[] args)
{
SpellCheck sc = new SpellCheck();
sc.run();
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote