Question: Java Programming: Convert Quote6Morse.txt file to English Write a Java program that asks a user for a .txt file, then converts the file into Morse
Java Programming: Convert Quote6Morse.txt file to English
Write a Java program that asks a user for a .txt file, then converts the file into Morse code if it is in English, or converts it into English if it is in Morse code.
Create a class that implements this interface: Use it to encode and decode the .txt files.
Use two HashMaps, one to go from morse to letters, and one to go from letters to morse.:
Read the codes in from the MorseCodeTable file: Pass the filename into the object via the constructor.
------- Converter Class ----------- public interface Converter {
//public Converter(String encodingfile); << good idea /** * Prints a list of the key values pairs used to encode and decode strings. * */ public void printKeyValuePairs(); /** * Encodes a string based on the Key Value Pairs. * For Example if the encode file contained Morse code * the letter a would be replaced with .- wherever it was in the string * @param textToEncode the String to encode * @return returns the encoded version of the string */ public String encode(String textToEncode); /** * Encodes a string based on the Key Value Pairs. * For Example if the encode file contained Morse code .- * it would be replaced with a wherever it was in the string * @param textToDecode the String to decode * @return returns the decode version of the string */ public String decode(String textToDecode); //private boolean saveToFile(String textToSave, String saveToFile ) /** * Encodes a string based on the Key Value Pairs. * For Example if the encode file contained Morse code .- * it would be replaced with a wherever it was in the string * @param textToDecode the String to decode * @param filename the file to save the decoded string in * @return returns true if the file is decoded and saved successfully */ public boolean decodeSaveToFile(String decode, String filename ); /** * Encodes a string based on the Key Value Pairs. * For Example if the encode file contained Morse code .- * it would be replaced with a wherever it was in the string * @param encode the String to encode * @param filename the file to save the encoded string in * @return returns true if the file is encoded and saved successfully */ public boolean encodeSaveToFile(String encode, String filename ); }
--------- Main Class ----------------- import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException;
public class Main {
/** * @param args */ public static void main(String[] args) { MorseConverter mc = new MorseConverter("MorseCodeTable.txt"); String workingDir = System.getProperty("user.dir") + "/"; mc.printKeyValuePairs(); String fileName = "Quote2.txt"; String copyfileName = "Quote2Copy"; String saveFileName = "Quote2Morse.txt"; StringBuilder sb = new StringBuilder(); try (BufferedReader br = new BufferedReader(new FileReader(workingDir + fileName))) { String line = ""; while ((line = br.readLine()) != null) { sb.append(line); } }catch (FileNotFoundException e) { System.out.print(workingDir + fileName + " File Not found"); } catch (IOException e) { e.printStackTrace(); } mc.encodeSaveToFile(sb.toString(), workingDir + saveFileName); String encodeCopy = mc.encode(sb.toString()); mc.decodeSaveToFile(encodeCopy, workingDir + copyfileName); }
}
----------- MorseConverter Class ------------------------- import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.HashMap; import java.util.Scanner;
public class MorseConverter implements Converter { HashMap
------------ Quote2Morse.txt file ------------ - .... . .-. . .- .-. . - .-- --- -- .- .--- --- .-. .--. .-. --- -.. ..- -.-. - ... - .... .- - -.-. --- -- . --- ..- - --- ..-. -... . .-. -.- . .-.. . -.-- ---... .-.. ... -.. .- -. -.. ..- -. .. -..- .-.-.- .-- . -.. --- -. .----. - -... . .-.. .. . ...- . - .... .. ... - --- -... . .- -.-. --- .. -. -.-. .. -.. . -. -.-. . .-.-.- .--- . .-. . -- -.-- ... .-.-.- .- -. -.. . .-. ... --- -.
----------- Quote2Copy.txt file ------------- there are two major products that come out of berkeley: lsd and unix. we don't believe this to be a coincidence. jeremy s. anderson
------------- Quote2.txt file ---------- There are two major products that come out of Berkeley: LSD and UNIX. We don't believe this to be a coincidence. Jeremy S. Anderson
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
