Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ignore compiled Java class files
*.class
65 changes: 65 additions & 0 deletions MorseCodeTranslatorDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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 MorseCodeTranslatorDemo {
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(inputFileName, outputFileName);
}
catch (MorseCodeTranslatorException e) {
System.out.println("\nSuccessfully caught exception:");
System.out.println(e);
}
}
}
2 changes: 2 additions & 0 deletions input_files/allWhiteSpace.morsecode
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
||||||||||
||||||
Empty file added input_files/emptyFile.morsecode
Empty file.
1 change: 1 addition & 0 deletions input_files/invalidInput.morsecode
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
..-.||..||.-?..||.
1 change: 1 addition & 0 deletions input_files/invalidInput2.morsecode
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--||---|-..
1 change: 1 addition & 0 deletions input_files/invalidInput3.morsecode
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-..||....||-.-.-||.
7 changes: 7 additions & 0 deletions input_files/sampleFile1.morsecode
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.-||-...||-.-.||-..||.||..-.||--.||....||..||.---||-.-||.-..||--||-.
---||.--.||--.-||.-.||...||-||..-||...-||.--||-..-||-.--||--..
-----||.----||..---||...--||....-||.....||-....||--...||---..
----.||.-.-.-||--..--||..--..||-...-

-..||---||--.
....||.||.-..||.-..||---||||.--||---||.-.||.-..||-..
1 change: 1 addition & 0 deletions input_files/sampleFile2.morsecode
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--||---||.-.||...||.||||-.-.||---||-..||.
25 changes: 25 additions & 0 deletions morseCodeTranslator/InputFileIOException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package morseCodeTranslator;

/**
* Exception raised when an IOException occurs while reading a morse
* code input file.
*/
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);
}

}
24 changes: 24 additions & 0 deletions morseCodeTranslator/InputFileNotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package morseCodeTranslator;

/**
* Exception raised when the input morse code file is not found.
*/
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);
}

}
25 changes: 25 additions & 0 deletions morseCodeTranslator/InvalidMorseCodeInputException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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() {
super();
}

public InvalidMorseCodeInputException(String message) {
super(message);
}

public InvalidMorseCodeInputException(String message, Throwable cause) {
super(message, cause);
}

public InvalidMorseCodeInputException(Throwable cause) {
super(cause);
}

}
72 changes: 72 additions & 0 deletions morseCodeTranslator/MorseCodeDictionary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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
// static so that we only create one instance of this
private static final Map<String, Character> 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]);
}
}

// 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() {

}

/*
* Public Functions
*/

/**
* 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 (input not in the morse code map)
if (msCharacter == null)
throw new InvalidMorseCodeInputException("Invalid morse code input: " + morseCodeInput);

return msCharacter;
}
}
89 changes: 89 additions & 0 deletions morseCodeTranslator/MorseCodeInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package morseCodeTranslator;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
* Class for reading a morse code input file.
*/
class MorseCodeInput {

/*
* 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");
}
}

/**
* 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[] 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 character strings 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;
}
}
Loading