From 498d77a3ae81597deb8a4137b48448a76161ac7a Mon Sep 17 00:00:00 2001 From: Ben Birkett Date: Tue, 26 Mar 2019 20:00:51 -0400 Subject: [PATCH 1/4] File and class structure laid out --- .gitignore | 2 ++ morseCodeTranslator/MorseCodeDictionary.java | 21 +++++++++++++++++ morseCodeTranslator/MorseCodeInput.java | 19 ++++++++++++++++ morseCodeTranslator/MorseCodeOutput.java | 18 +++++++++++++++ morseCodeTranslator/MorseCodeTranslator.java | 24 ++++++++++++++++++++ 5 files changed, 84 insertions(+) create mode 100644 .gitignore create mode 100644 morseCodeTranslator/MorseCodeDictionary.java create mode 100644 morseCodeTranslator/MorseCodeInput.java create mode 100644 morseCodeTranslator/MorseCodeOutput.java create mode 100644 morseCodeTranslator/MorseCodeTranslator.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..20e8d28 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# ignore compiled Java class files +*.class \ No newline at end of file diff --git a/morseCodeTranslator/MorseCodeDictionary.java b/morseCodeTranslator/MorseCodeDictionary.java new file mode 100644 index 0000000..9b6d058 --- /dev/null +++ b/morseCodeTranslator/MorseCodeDictionary.java @@ -0,0 +1,21 @@ +package morseCodeTranslator; + +/** + * Class representing a dictionary of morse code + * strings to character translations. + */ +final class MorseCodeDictionary { + + private MorseCodeDictionary() { + + } + + /** + * Returns character translation of the given morse code letter. + * @param morseCodeInput string containing a morse code letter + * @return translated char + */ + public static char getCharacter(String morseCodeInput) { + return 'a'; + } +} \ No newline at end of file diff --git a/morseCodeTranslator/MorseCodeInput.java b/morseCodeTranslator/MorseCodeInput.java new file mode 100644 index 0000000..116ce03 --- /dev/null +++ b/morseCodeTranslator/MorseCodeInput.java @@ -0,0 +1,19 @@ +package morseCodeTranslator; + +// Will abstract out file reading +class MorseCodeInput { + + public MorseCodeInput(String inputFileName) { + + } + + /** + * Reads the next morse code character as a string. + * @return string containing next morse code character, + * null if end of file is reached + */ + public String readNextCharacter() { + return null; + } + +} \ No newline at end of file diff --git a/morseCodeTranslator/MorseCodeOutput.java b/morseCodeTranslator/MorseCodeOutput.java new file mode 100644 index 0000000..0eb4e1f --- /dev/null +++ b/morseCodeTranslator/MorseCodeOutput.java @@ -0,0 +1,18 @@ +package morseCodeTranslator; + +// Will abstract out file output +class MorseCodeOutput { + + public MorseCodeOutput(String outputFileName) { + + } + + /** + * Outputs the given ASCII character to file. + * @param outputChar character to output + */ + public void outputCharacter(char outputChar) { + + } + +} \ No newline at end of file diff --git a/morseCodeTranslator/MorseCodeTranslator.java b/morseCodeTranslator/MorseCodeTranslator.java new file mode 100644 index 0000000..797e3d6 --- /dev/null +++ b/morseCodeTranslator/MorseCodeTranslator.java @@ -0,0 +1,24 @@ +package morseCodeTranslator; + +public class MorseCodeTranslator { + + /** + * Members + */ + private static MorseCodeDictionary morseCodeDictionary; + + private MorseCodeTranslator() { + + } + + /** + * Translates file containing morse code and writes the results to + * an output file. + * @param inputFileName name of input file containing morse code + * @param outputFileName intended name of output file for translation + */ + public static void translateFile(String inputFileName, String outputFileName) { + MorseCodeInput morseCodeInput = new MorseCodeInput(inputFileName); + MorseCodeOutput morseCodeOutput = new MorseCodeOutput(outputFileName); + } +} \ No newline at end of file From 6970c5ef5e50aa12f6e4a5035728f440bd4f1d9e Mon Sep 17 00:00:00 2001 From: Ben Birkett Date: Wed, 27 Mar 2019 00:33:59 -0400 Subject: [PATCH 2/4] Implemented everything, added custom exceptions, added demo --- MorseCodeReaderDemo.java | 14 +++ morseCodeTranslator/InputFileIOException.java | 21 +++++ .../InputFileNotFoundException.java | 21 +++++ .../InvalidMorseCodeInputException.java | 21 +++++ morseCodeTranslator/MorseCodeDictionary.java | 49 +++++++++- morseCodeTranslator/MorseCodeInput.java | 94 +++++++++++++++++-- morseCodeTranslator/MorseCodeOutput.java | 49 +++++++++- morseCodeTranslator/MorseCodeTranslator.java | 53 ++++++++++- .../MorseCodeTranslatorException.java | 20 ++++ .../OutputFileIOException.java | 21 +++++ sampleFile1.txt | 7 ++ 11 files changed, 353 insertions(+), 17 deletions(-) create mode 100644 MorseCodeReaderDemo.java create mode 100644 morseCodeTranslator/InputFileIOException.java create mode 100644 morseCodeTranslator/InputFileNotFoundException.java create mode 100644 morseCodeTranslator/InvalidMorseCodeInputException.java create mode 100644 morseCodeTranslator/MorseCodeTranslatorException.java create mode 100644 morseCodeTranslator/OutputFileIOException.java create mode 100644 sampleFile1.txt diff --git a/MorseCodeReaderDemo.java b/MorseCodeReaderDemo.java new file mode 100644 index 0000000..ca18ec1 --- /dev/null +++ b/MorseCodeReaderDemo.java @@ -0,0 +1,14 @@ +import java.io.IOException; + +import morseCodeTranslator.*; + +public class MorseCodeReaderDemo { + public static void main(String[] args) { + try { + MorseCodeTranslator.translateFile("sampleFile1.txt", "sampleOut1.txt"); + } + catch (MorseCodeTranslatorException e) { + System.out.println(e); + } + } +} \ No newline at end of file diff --git a/morseCodeTranslator/InputFileIOException.java b/morseCodeTranslator/InputFileIOException.java new file mode 100644 index 0000000..5e99c63 --- /dev/null +++ b/morseCodeTranslator/InputFileIOException.java @@ -0,0 +1,21 @@ +package morseCodeTranslator; + +public class InputFileIOException extends MorseCodeTranslatorException { + + public InputFileIOException() { + super(); + } + + public InputFileIOException(String message) { + super(message); + } + + public InputFileIOException(String message, Throwable cause) { + super(message, cause); + } + + public InputFileIOException(Throwable cause) { + super(cause); + } + +} \ No newline at end of file diff --git a/morseCodeTranslator/InputFileNotFoundException.java b/morseCodeTranslator/InputFileNotFoundException.java new file mode 100644 index 0000000..3d1d948 --- /dev/null +++ b/morseCodeTranslator/InputFileNotFoundException.java @@ -0,0 +1,21 @@ +package morseCodeTranslator; + +public class InputFileNotFoundException extends MorseCodeTranslatorException { + + public InputFileNotFoundException() { + super(); + } + + public InputFileNotFoundException(String message) { + super(message); + } + + public InputFileNotFoundException(String message, Throwable cause) { + super(message, cause); + } + + public InputFileNotFoundException(Throwable cause) { + super(cause); + } + +} \ No newline at end of file diff --git a/morseCodeTranslator/InvalidMorseCodeInputException.java b/morseCodeTranslator/InvalidMorseCodeInputException.java new file mode 100644 index 0000000..fcdb947 --- /dev/null +++ b/morseCodeTranslator/InvalidMorseCodeInputException.java @@ -0,0 +1,21 @@ +package morseCodeTranslator; + +public class InvalidMorseCodeInputException extends MorseCodeTranslatorException { + + public InvalidMorseCodeInputException() { + super(); + } + + public InvalidMorseCodeInputException(String message) { + super(message); + } + + public InvalidMorseCodeInputException(String message, Throwable cause) { + super(message, cause); + } + + public InvalidMorseCodeInputException(Throwable cause) { + super(cause); + } + +} \ No newline at end of file diff --git a/morseCodeTranslator/MorseCodeDictionary.java b/morseCodeTranslator/MorseCodeDictionary.java index 9b6d058..c5862b4 100644 --- a/morseCodeTranslator/MorseCodeDictionary.java +++ b/morseCodeTranslator/MorseCodeDictionary.java @@ -1,21 +1,66 @@ package morseCodeTranslator; +import java.util.Map; +import java.util.HashMap; + /** * Class representing a dictionary of morse code * strings to character translations. */ final class MorseCodeDictionary { + /* + * Members + */ + + // mapping of morse code strings to their translated characters + private static final Map morseCodeMap = new HashMap<>(); + + // initialize the morseCodeMap + static { + // ordered array of morse code strings + String[] morseCodeStrings = new String[] { + ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", + ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", + "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", + ".----", "..---", "...--", "....-", ".....", "-....", "--...", + "---..", "----.", ".-.-.-", "--..--", "..--..", "-...-" + }; + // ordered array of morse code translation characters + char[] morseCodeChars = new char[] { + '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', '.', ',', '?', '=' + }; + + // arrays are ordered such that: + // morseCodeStrings[i] translates to morseCodeChars[i] + + for (int i=0; i < morseCodeStrings.length; i++) { + morseCodeMap.put(morseCodeStrings[i], morseCodeChars[i]); + } + } + private MorseCodeDictionary() { } + /* + * Public Functions + */ + /** * Returns character translation of the given morse code letter. * @param morseCodeInput string containing a morse code letter * @return translated char */ - public static char getCharacter(String morseCodeInput) { - return 'a'; + public static char getCharacter(String morseCodeInput) throws InvalidMorseCodeInputException { + Character msCharacter = morseCodeMap.get(morseCodeInput); + + // msCharacter will be null if we're given invalid input + if (msCharacter == null) + throw new InvalidMorseCodeInputException("Invalid morse code input: " + morseCodeInput); + + return msCharacter; } } \ No newline at end of file diff --git a/morseCodeTranslator/MorseCodeInput.java b/morseCodeTranslator/MorseCodeInput.java index 116ce03..cb5f639 100644 --- a/morseCodeTranslator/MorseCodeInput.java +++ b/morseCodeTranslator/MorseCodeInput.java @@ -1,19 +1,99 @@ package morseCodeTranslator; -// Will abstract out file reading +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.FileNotFoundException; +import java.io.IOException; + +/** + * Class for reading in a morse code input file. + */ class MorseCodeInput { - public MorseCodeInput(String inputFileName) { + /* + * Members + */ + private BufferedReader fileReader; + private static final String inputFileDelimiter = "\\|\\|"; + /* + * Public functions + */ + + /** + * MorseCodeInput constructor. + * @param inputFileName name of morse code input file + * @throws InputFileNotFoundException if input morse code + * file is not found + */ + public MorseCodeInput(String inputFileName) throws InputFileNotFoundException { + // initialize the file reader + try { + this.fileReader = new BufferedReader(new FileReader(inputFileName)); + } + catch (FileNotFoundException e) { + throw new InputFileNotFoundException("Input file \""+inputFileName+"\" not found"); + } } /** - * Reads the next morse code character as a string. - * @return string containing next morse code character, - * null if end of file is reached + * Gets all morse code characters in a file. + * @return String array with all morse code characters. + * May contain '\n': this indicates a newline + * May contain empty strings: this indicates a space */ - public String readNextCharacter() { - return null; + public String[] getMorseCodeCharacters() throws InputFileIOException { + // initialize array of morse code characters + String[] morseCodeCharacters = new String[0]; + + // parse the file + try { + // read in each line of input file + String line = this.fileReader.readLine(); + while (line != null) { + // split line into morse code characters using delimiter + String[] lineCharacters = line.split(this.inputFileDelimiter); + morseCodeCharacters = this.concatArraysAndNewline(morseCodeCharacters, lineCharacters); + line = this.fileReader.readLine(); + } + + this.fileReader.close(); + } + catch (IOException e) { + throw new InputFileIOException("IOException occurred with the input file", e); + } + + return morseCodeCharacters; } + /* + * Helper functions + */ + + /** + * Concatenates two string arrays + a newline and returns the resulting array. + */ + private String[] concatArraysAndNewline(String[] array1, String[] array2) { + int a1Length = array1.length; + int a2Length = array2.length; + String[] resultArray = new String[a1Length + a2Length + 1]; + + // copy array1 and array2 into result array + System.arraycopy(array1, 0, resultArray, 0, a1Length); + System.arraycopy(array2, 0, resultArray, a1Length, a2Length); + // newline + resultArray[resultArray.length - 1] = "\n"; + + return resultArray; + } + + /** + * Removes first element of the given string array and returns it. + */ + // private String[] removeFirstArrayElement(String[] sArray) { + // int aLength = sArray.length - 1; + // String[] resultArray = new String[aLength]; + // System.arraycopy(sArray, 1, resultArray, 0, aLength); + // return resultArray; + // } } \ No newline at end of file diff --git a/morseCodeTranslator/MorseCodeOutput.java b/morseCodeTranslator/MorseCodeOutput.java index 0eb4e1f..ae03b5d 100644 --- a/morseCodeTranslator/MorseCodeOutput.java +++ b/morseCodeTranslator/MorseCodeOutput.java @@ -1,18 +1,59 @@ package morseCodeTranslator; -// Will abstract out file output +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; + +/** + * Class for outputting morse code translations to file. + */ class MorseCodeOutput { - public MorseCodeOutput(String outputFileName) { + /* + * Members + */ + private BufferedWriter fileWriter; + + /* + * Public Functions + */ + public MorseCodeOutput(String outputFileName) throws OutputFileIOException { + // initialize the file writer + try { + this.fileWriter = new BufferedWriter(new FileWriter(outputFileName)); + } + catch (IOException e) { + throw new OutputFileIOException("IOException occurred when initializing the output file", e); + } } /** - * Outputs the given ASCII character to file. + * Outputs the given character to file and prints to console. * @param outputChar character to output */ - public void outputCharacter(char outputChar) { + public void outputCharacter(char outputChar) throws OutputFileIOException { + String outCharacter = Character.toString(outputChar); + System.out.print(outCharacter); + try { + this.fileWriter.write(outCharacter); + } + catch(IOException e) { + throw new OutputFileIOException("IOException occurred when writing to the output file", e); + } + } + /** + * Call this function to finish writing to the output file. + */ + public void finishWriting() throws OutputFileIOException { + // close the fileWriter + try { + this.fileWriter.close(); + } + catch (IOException e) { + throw new OutputFileIOException("IOException occurred when closing the output file", e); + } } } \ No newline at end of file diff --git a/morseCodeTranslator/MorseCodeTranslator.java b/morseCodeTranslator/MorseCodeTranslator.java index 797e3d6..587e5d3 100644 --- a/morseCodeTranslator/MorseCodeTranslator.java +++ b/morseCodeTranslator/MorseCodeTranslator.java @@ -1,8 +1,14 @@ package morseCodeTranslator; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Arrays; + public class MorseCodeTranslator { - /** + /* * Members */ private static MorseCodeDictionary morseCodeDictionary; @@ -11,14 +17,53 @@ private MorseCodeTranslator() { } + /* + * Public Functions + */ + /** * Translates file containing morse code and writes the results to * an output file. * @param inputFileName name of input file containing morse code * @param outputFileName intended name of output file for translation + * @throws MorseCodeTranslatorException if any exception related to + * file input, file output, or morse code to character translation + * occurs. Specific exception inherited from MorseCodeTranslatorException + * will be thrown. */ - public static void translateFile(String inputFileName, String outputFileName) { - MorseCodeInput morseCodeInput = new MorseCodeInput(inputFileName); - MorseCodeOutput morseCodeOutput = new MorseCodeOutput(outputFileName); + public static void translateFile(String inputFileName, String outputFileName) throws MorseCodeTranslatorException { + + MorseCodeInput morseCodeInput; + MorseCodeOutput morseCodeOutput; + String[] morseCodeCharacters; + + try { + // initialize input and output, get array of morse code characters + morseCodeInput = new MorseCodeInput(inputFileName); + morseCodeOutput = new MorseCodeOutput(outputFileName); + morseCodeCharacters = morseCodeInput.getMorseCodeCharacters(); + + // translate morse code character strings into characters and output them + for (int i=0; i < morseCodeCharacters.length; i++) { + String msCharacter = morseCodeCharacters[i]; + + // empty string indicates a space + if (msCharacter.length() == 0) { + morseCodeOutput.outputCharacter(' '); + } + // string "\n" indicates a newline + else if (msCharacter.length() == 1 && (char)msCharacter.charAt(0) == '\n') { + morseCodeOutput.outputCharacter('\n'); + } + else { + // translate morse code string to character and output it + morseCodeOutput.outputCharacter(morseCodeDictionary.getCharacter(msCharacter.trim())); + } + } + morseCodeOutput.finishWriting(); + } + catch (MorseCodeTranslatorException e) { + throw e; + } } } \ No newline at end of file diff --git a/morseCodeTranslator/MorseCodeTranslatorException.java b/morseCodeTranslator/MorseCodeTranslatorException.java new file mode 100644 index 0000000..811855f --- /dev/null +++ b/morseCodeTranslator/MorseCodeTranslatorException.java @@ -0,0 +1,20 @@ +package morseCodeTranslator; + +public abstract class MorseCodeTranslatorException extends Exception { + + public MorseCodeTranslatorException() { + super(); + } + + public MorseCodeTranslatorException(String message) { + super(message); + } + + public MorseCodeTranslatorException(String message, Throwable cause) { + super(message, cause); + } + + public MorseCodeTranslatorException(Throwable cause) { + super(cause); + } +} \ No newline at end of file diff --git a/morseCodeTranslator/OutputFileIOException.java b/morseCodeTranslator/OutputFileIOException.java new file mode 100644 index 0000000..a3b8658 --- /dev/null +++ b/morseCodeTranslator/OutputFileIOException.java @@ -0,0 +1,21 @@ +package morseCodeTranslator; + +public class OutputFileIOException extends MorseCodeTranslatorException { + + public OutputFileIOException() { + super(); + } + + public OutputFileIOException(String message) { + super(message); + } + + public OutputFileIOException(String message, Throwable cause) { + super(message, cause); + } + + public OutputFileIOException(Throwable cause) { + super(cause); + } + +} \ No newline at end of file diff --git a/sampleFile1.txt b/sampleFile1.txt new file mode 100644 index 0000000..3593895 --- /dev/null +++ b/sampleFile1.txt @@ -0,0 +1,7 @@ +.-||-...||-.-.||-..||.||..-.||--.||....||..||.---||-.-||.-..||--||-. +---||.--.||--.-||.-.||...||-||..-||...-||.--||-..-||-.--||--.. +-----||.----||..---||...--||....-||.....||-....||--...||---.. +----.||.-.-.-||--..--||..--..||-...- + +-..||---||--. +....||.||.-..||.-..||---||||.--||---||.-.||.-..||-.. \ No newline at end of file From 002ad77e964acd3782c462b04b29cd62f7782403 Mon Sep 17 00:00:00 2001 From: Ben Birkett Date: Wed, 27 Mar 2019 14:56:08 -0400 Subject: [PATCH 3/4] Expanded demo, improved comments --- MorseCodeReaderDemo.java | 57 ++++++++++++++++++- input_files/allWhiteSpace.morsecode | 2 + input_files/emptyFile.morsecode | 0 input_files/invalidInput.morsecode | 1 + input_files/invalidInput2.morsecode | 1 + input_files/invalidInput3.morsecode | 1 + .../sampleFile1.morsecode | 0 input_files/sampleFile2.morsecode | 1 + morseCodeTranslator/InputFileIOException.java | 4 ++ .../InputFileNotFoundException.java | 3 + .../InvalidMorseCodeInputException.java | 4 ++ morseCodeTranslator/MorseCodeDictionary.java | 8 ++- morseCodeTranslator/MorseCodeInput.java | 14 +---- morseCodeTranslator/MorseCodeOutput.java | 13 ++++- morseCodeTranslator/MorseCodeTranslator.java | 25 ++++++-- .../MorseCodeTranslatorException.java | 6 ++ .../OutputFileIOException.java | 4 ++ 17 files changed, 121 insertions(+), 23 deletions(-) create mode 100644 input_files/allWhiteSpace.morsecode create mode 100644 input_files/emptyFile.morsecode create mode 100644 input_files/invalidInput.morsecode create mode 100644 input_files/invalidInput2.morsecode create mode 100644 input_files/invalidInput3.morsecode rename sampleFile1.txt => input_files/sampleFile1.morsecode (100%) create mode 100644 input_files/sampleFile2.morsecode diff --git a/MorseCodeReaderDemo.java b/MorseCodeReaderDemo.java index ca18ec1..b0a9baf 100644 --- a/MorseCodeReaderDemo.java +++ b/MorseCodeReaderDemo.java @@ -1,13 +1,64 @@ -import java.io.IOException; - import morseCodeTranslator.*; +import java.io.IOException; +import java.util.Scanner; +import java.io.File; +import java.io.FileNotFoundException; +/** + * Demo to demonstrate use of MorseCodeTranslator package. + * Compile and run this file to see the translator in action. + */ public class MorseCodeReaderDemo { public static void main(String[] args) { + + File outputDir; // output file directory + + // run valid input files + try { + // make output directory + outputDir = new File("output"); + if (outputDir.exists() || outputDir.mkdir()) { + // translate sample files (no exceptions should be thrown) + MorseCodeTranslator.translateFile("input_files/sampleFile1.morsecode", "output/sampleOut1.translation"); + MorseCodeTranslator.translateFile("input_files/sampleFile2.morsecode", "output/sampleOut2.translation"); + MorseCodeTranslator.translateFile("input_files/emptyFile.morsecode", "output/emptyOutput.translation"); + MorseCodeTranslator.translateFile("input_files/allWhiteSpace.morsecode", "output/allWhiteSpace.translation"); + + // print output files to console + for (File file : outputDir.listFiles()) { + Scanner fileScanner = new Scanner(file); + System.out.println("\n" + file.getName() + ":"); + while (fileScanner.hasNextLine()) { + System.out.println(fileScanner.nextLine()); + } + fileScanner.close(); + } + } + else { + System.out.println("Cannot create output directory"); + } + } + catch (MorseCodeTranslatorException | FileNotFoundException e) { + e.printStackTrace(); + } + + // run invalid input files + translateInvalidFile("input_files/nonExistentFile.morsecode", "output/shouldNotExist.translation"); + translateInvalidFile("input_files/invalidInput.morsecode", "output/invalid1.translation"); + translateInvalidFile("input_files/invalidInput2.morsecode", "output/invalid2.translation"); + translateInvalidFile("input_files/invalidInput3.morsecode", "output/invalid3.translation"); + } + + /** + * Runs MorseCodeTranslator.translateFile() on an input file that's intended to be + * invalid and prints exception. + */ + private static void translateInvalidFile(String inputFileName, String outputFileName) { try { - MorseCodeTranslator.translateFile("sampleFile1.txt", "sampleOut1.txt"); + MorseCodeTranslator.translateFile(inputFileName, outputFileName); } catch (MorseCodeTranslatorException e) { + System.out.println("\nSuccessfully caught exception:"); System.out.println(e); } } diff --git a/input_files/allWhiteSpace.morsecode b/input_files/allWhiteSpace.morsecode new file mode 100644 index 0000000..2f2fed0 --- /dev/null +++ b/input_files/allWhiteSpace.morsecode @@ -0,0 +1,2 @@ +|||||||||| +|||||| \ No newline at end of file diff --git a/input_files/emptyFile.morsecode b/input_files/emptyFile.morsecode new file mode 100644 index 0000000..e69de29 diff --git a/input_files/invalidInput.morsecode b/input_files/invalidInput.morsecode new file mode 100644 index 0000000..074045a --- /dev/null +++ b/input_files/invalidInput.morsecode @@ -0,0 +1 @@ +..-.||..||.-?..||. \ No newline at end of file diff --git a/input_files/invalidInput2.morsecode b/input_files/invalidInput2.morsecode new file mode 100644 index 0000000..fcaec5d --- /dev/null +++ b/input_files/invalidInput2.morsecode @@ -0,0 +1 @@ +--||---|-.. \ No newline at end of file diff --git a/input_files/invalidInput3.morsecode b/input_files/invalidInput3.morsecode new file mode 100644 index 0000000..8ee3589 --- /dev/null +++ b/input_files/invalidInput3.morsecode @@ -0,0 +1 @@ +-..||....||-.-.-||. \ No newline at end of file diff --git a/sampleFile1.txt b/input_files/sampleFile1.morsecode similarity index 100% rename from sampleFile1.txt rename to input_files/sampleFile1.morsecode diff --git a/input_files/sampleFile2.morsecode b/input_files/sampleFile2.morsecode new file mode 100644 index 0000000..8ee8b4f --- /dev/null +++ b/input_files/sampleFile2.morsecode @@ -0,0 +1 @@ +--||---||.-.||...||.||||-.-.||---||-..||. \ No newline at end of file diff --git a/morseCodeTranslator/InputFileIOException.java b/morseCodeTranslator/InputFileIOException.java index 5e99c63..5faf586 100644 --- a/morseCodeTranslator/InputFileIOException.java +++ b/morseCodeTranslator/InputFileIOException.java @@ -1,5 +1,9 @@ package morseCodeTranslator; +/** + * Exception raised when an IOException occurs while reading a morse + * code input file. + */ public class InputFileIOException extends MorseCodeTranslatorException { public InputFileIOException() { diff --git a/morseCodeTranslator/InputFileNotFoundException.java b/morseCodeTranslator/InputFileNotFoundException.java index 3d1d948..ba80be7 100644 --- a/morseCodeTranslator/InputFileNotFoundException.java +++ b/morseCodeTranslator/InputFileNotFoundException.java @@ -1,5 +1,8 @@ package morseCodeTranslator; +/** + * Exception raised when the input morse code file is not found. + */ public class InputFileNotFoundException extends MorseCodeTranslatorException { public InputFileNotFoundException() { diff --git a/morseCodeTranslator/InvalidMorseCodeInputException.java b/morseCodeTranslator/InvalidMorseCodeInputException.java index fcdb947..74dbf4e 100644 --- a/morseCodeTranslator/InvalidMorseCodeInputException.java +++ b/morseCodeTranslator/InvalidMorseCodeInputException.java @@ -1,5 +1,9 @@ package morseCodeTranslator; +/** + * Exception raised when an any invalid morse code or characters is + * read from the morse code input file. + */ public class InvalidMorseCodeInputException extends MorseCodeTranslatorException { public InvalidMorseCodeInputException() { diff --git a/morseCodeTranslator/MorseCodeDictionary.java b/morseCodeTranslator/MorseCodeDictionary.java index c5862b4..20a406f 100644 --- a/morseCodeTranslator/MorseCodeDictionary.java +++ b/morseCodeTranslator/MorseCodeDictionary.java @@ -14,6 +14,7 @@ final class MorseCodeDictionary { */ // mapping of morse code strings to their translated characters + // static so that we only create one instance of this private static final Map morseCodeMap = new HashMap<>(); // initialize the morseCodeMap @@ -41,6 +42,9 @@ final class MorseCodeDictionary { } } + // This class is meant to expose a static function, so the + // user only needs one instance of this class. + // Therefore the constructor private. private MorseCodeDictionary() { } @@ -53,11 +57,13 @@ private MorseCodeDictionary() { * Returns character translation of the given morse code letter. * @param morseCodeInput string containing a morse code letter * @return translated char + * @throws InvalidMorseCodeInputException if given an invalid morse code + * string as parameter */ public static char getCharacter(String morseCodeInput) throws InvalidMorseCodeInputException { Character msCharacter = morseCodeMap.get(morseCodeInput); - // msCharacter will be null if we're given invalid input + // msCharacter will be null if we're given invalid input (input not in the morse code map) if (msCharacter == null) throw new InvalidMorseCodeInputException("Invalid morse code input: " + morseCodeInput); diff --git a/morseCodeTranslator/MorseCodeInput.java b/morseCodeTranslator/MorseCodeInput.java index cb5f639..f699240 100644 --- a/morseCodeTranslator/MorseCodeInput.java +++ b/morseCodeTranslator/MorseCodeInput.java @@ -6,7 +6,7 @@ import java.io.IOException; /** - * Class for reading in a morse code input file. + * Class for reading a morse code input file. */ class MorseCodeInput { @@ -51,7 +51,7 @@ public String[] getMorseCodeCharacters() throws InputFileIOException { // read in each line of input file String line = this.fileReader.readLine(); while (line != null) { - // split line into morse code characters using delimiter + // split line into morse code character strings using delimiter String[] lineCharacters = line.split(this.inputFileDelimiter); morseCodeCharacters = this.concatArraysAndNewline(morseCodeCharacters, lineCharacters); line = this.fileReader.readLine(); @@ -86,14 +86,4 @@ private String[] concatArraysAndNewline(String[] array1, String[] array2) { return resultArray; } - - /** - * Removes first element of the given string array and returns it. - */ - // private String[] removeFirstArrayElement(String[] sArray) { - // int aLength = sArray.length - 1; - // String[] resultArray = new String[aLength]; - // System.arraycopy(sArray, 1, resultArray, 0, aLength); - // return resultArray; - // } } \ No newline at end of file diff --git a/morseCodeTranslator/MorseCodeOutput.java b/morseCodeTranslator/MorseCodeOutput.java index ae03b5d..477de00 100644 --- a/morseCodeTranslator/MorseCodeOutput.java +++ b/morseCodeTranslator/MorseCodeOutput.java @@ -18,6 +18,12 @@ class MorseCodeOutput { * Public Functions */ + /** + * MorseCodeOutput constructor. + * @param outputFileName name of file to output to + * @throws OutputFileIOException if an IOException occurs while + * trying to create the output file + */ public MorseCodeOutput(String outputFileName) throws OutputFileIOException { // initialize the file writer try { @@ -29,12 +35,13 @@ public MorseCodeOutput(String outputFileName) throws OutputFileIOException { } /** - * Outputs the given character to file and prints to console. + * Outputs a given character to file. * @param outputChar character to output + * @throws OutputFileIOException if an IOException occurs + * while writing to the output file */ public void outputCharacter(char outputChar) throws OutputFileIOException { String outCharacter = Character.toString(outputChar); - System.out.print(outCharacter); try { this.fileWriter.write(outCharacter); } @@ -45,6 +52,8 @@ public void outputCharacter(char outputChar) throws OutputFileIOException { /** * Call this function to finish writing to the output file. + * @throws OutputFileIOException if an IOException occurs + * when trying to close the output file */ public void finishWriting() throws OutputFileIOException { // close the fileWriter diff --git a/morseCodeTranslator/MorseCodeTranslator.java b/morseCodeTranslator/MorseCodeTranslator.java index 587e5d3..2a5d0d4 100644 --- a/morseCodeTranslator/MorseCodeTranslator.java +++ b/morseCodeTranslator/MorseCodeTranslator.java @@ -6,6 +6,11 @@ import java.io.IOException; import java.util.Arrays; +// This is the only class that is exposed to the user of this package + +/** + * Translates morse code. + */ public class MorseCodeTranslator { /* @@ -13,6 +18,9 @@ public class MorseCodeTranslator { */ private static MorseCodeDictionary morseCodeDictionary; + // This class is meant to expose a static function, so the + // user only needs one instance of this class. + // Therefore the constructor private. private MorseCodeTranslator() { } @@ -25,7 +33,7 @@ private MorseCodeTranslator() { * Translates file containing morse code and writes the results to * an output file. * @param inputFileName name of input file containing morse code - * @param outputFileName intended name of output file for translation + * @param outputFileName intended name of output file * @throws MorseCodeTranslatorException if any exception related to * file input, file output, or morse code to character translation * occurs. Specific exception inherited from MorseCodeTranslatorException @@ -33,18 +41,18 @@ private MorseCodeTranslator() { */ public static void translateFile(String inputFileName, String outputFileName) throws MorseCodeTranslatorException { - MorseCodeInput morseCodeInput; - MorseCodeOutput morseCodeOutput; + MorseCodeInput morseCodeInput = null; + MorseCodeOutput morseCodeOutput = null; String[] morseCodeCharacters; try { // initialize input and output, get array of morse code characters morseCodeInput = new MorseCodeInput(inputFileName); - morseCodeOutput = new MorseCodeOutput(outputFileName); morseCodeCharacters = morseCodeInput.getMorseCodeCharacters(); + morseCodeOutput = new MorseCodeOutput(outputFileName); // translate morse code character strings into characters and output them - for (int i=0; i < morseCodeCharacters.length; i++) { + for (int i = 0; i < morseCodeCharacters.length; i++) { String msCharacter = morseCodeCharacters[i]; // empty string indicates a space @@ -53,6 +61,12 @@ public static void translateFile(String inputFileName, String outputFileName) th } // string "\n" indicates a newline else if (msCharacter.length() == 1 && (char)msCharacter.charAt(0) == '\n') { + + if (i == morseCodeCharacters.length - 1) { + // suppress trailing newline at end of file + continue; + } + morseCodeOutput.outputCharacter('\n'); } else { @@ -63,6 +77,7 @@ else if (msCharacter.length() == 1 && (char)msCharacter.charAt(0) == '\n') { morseCodeOutput.finishWriting(); } catch (MorseCodeTranslatorException e) { + // throw any caught exceptions to the user throw e; } } diff --git a/morseCodeTranslator/MorseCodeTranslatorException.java b/morseCodeTranslator/MorseCodeTranslatorException.java index 811855f..7b2144c 100644 --- a/morseCodeTranslator/MorseCodeTranslatorException.java +++ b/morseCodeTranslator/MorseCodeTranslatorException.java @@ -1,5 +1,11 @@ package morseCodeTranslator; +// Using custom exceptions here to give user of MorseCodeTranslator useful +// exceptions that indicate any specific issues that occur during translation. + +/** + * Exception representing any exception specific to the MorseCodeTranslator + */ public abstract class MorseCodeTranslatorException extends Exception { public MorseCodeTranslatorException() { diff --git a/morseCodeTranslator/OutputFileIOException.java b/morseCodeTranslator/OutputFileIOException.java index a3b8658..c46b9f7 100644 --- a/morseCodeTranslator/OutputFileIOException.java +++ b/morseCodeTranslator/OutputFileIOException.java @@ -1,5 +1,9 @@ package morseCodeTranslator; +/** + * Exception raised when an IOException occurs while writing to the + * translation output file. + */ public class OutputFileIOException extends MorseCodeTranslatorException { public OutputFileIOException() { From 22f8aff89a155034ade428c5928a1e4095415db4 Mon Sep 17 00:00:00 2001 From: Ben Birkett Date: Wed, 27 Mar 2019 15:53:58 -0400 Subject: [PATCH 4/4] Changed demo name --- MorseCodeReaderDemo.java => MorseCodeTranslatorDemo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename MorseCodeReaderDemo.java => MorseCodeTranslatorDemo.java (98%) diff --git a/MorseCodeReaderDemo.java b/MorseCodeTranslatorDemo.java similarity index 98% rename from MorseCodeReaderDemo.java rename to MorseCodeTranslatorDemo.java index b0a9baf..35469ba 100644 --- a/MorseCodeReaderDemo.java +++ b/MorseCodeTranslatorDemo.java @@ -8,7 +8,7 @@ * Demo to demonstrate use of MorseCodeTranslator package. * Compile and run this file to see the translator in action. */ -public class MorseCodeReaderDemo { +public class MorseCodeTranslatorDemo { public static void main(String[] args) { File outputDir; // output file directory