Commit 75369544 authored by neil.whitehead's avatar neil.whitehead

FINAL

parent aafe3ea0
/**
* <b>Assessment</b> part three<br>
* <b>Created</b> 08/11/18<br>
* <b>Modified</b> 04/01/18<br>
* @author <a href = "mailto:neil.whitehead@yorksj.ac.uk"> Neil Whitehead</a>, <a href = "mailto:a.guest@yorksj.ac.uk">Andrew Guest</a>
*/
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.io.*; import java.io.*;
...@@ -5,47 +12,57 @@ import java.io.*; ...@@ -5,47 +12,57 @@ import java.io.*;
public class AssessmentPartFour public class AssessmentPartFour
{ {
List<String> morseCode = new ArrayList<String>(); List<String> morseCode = new ArrayList<String>();
//global variable
/**
//COMMENT THIS A LOT BETTER * Populates list with input from text file <br>
* Assumes that no validation checks are required.
* @param filename string
* @param morseCode string
* @return morseCode.size int
*/
public int loadMorseFromFile(String filename) public int loadMorseFromFile(String filename)
{ {
morseCode = new ArrayList<String>(); morseCode = new ArrayList<String>();
try try (FileReader file = new FileReader (filename); BufferedReader buffer = new BufferedReader (file))
//try/catch blocks will ensure that the program can recover from errors
//creating new file class so program can interact with file
//creating new buffer, which is faster to read from than repeatedly accessing the file directly
{ {
FileReader file = new FileReader (filename); String strLine = "";
BufferedReader buffer = new BufferedReader ( file );
String line = "";
while ((line = buffer.readLine()) !=null) while ((strLine = buffer.readLine()) !=null)
//whilst there is something to add. . .
{ {
morseCode.add(line); morseCode.add(strLine);
//. . . add to list
} }
//buffer.close(); not needed as try will automatically close //buffer.close(); not needed as try-with-resources will automatically close
} }
catch (IOException e) catch (IOException e)
//only used when error occurs.
{ {
System.out.println("error"); System.out.println("Exception: " + e.getMessage());
//e.getMessage retrieves a string with an error message to further explain issue
} }
return morseCode.size(); return morseCode.size();
} }
/**
//MOVE THE .GET INTO IT'S OWN VARIABLE. * Translates morse into standard English <br>
* Assumes that no validation checks are required.
* @param morseCode string array list (global variable)
* @return strTranslated string
*/
public String translateMorse() public String translateMorse()
{ {
String strTranslated = ""; String strTranslated = "";
String[][] strMorseArray = String[][] strMorseArray =
//morse-letter lookup table, ordered by morse code length //morse-letter lookup table, segregated by morse code length
//nb length corresponds with frequency within English language, so perhaps quicker than alphabetical
/* 0 */ {{" ", "E", "T"}, // have 1 char /* 0 */ {{" ", "E", "T"}, // have 1 char
/* 1 */ {"/", ".", "-"}, /* 1 */ {"/", ".", "-"},
/* 2 */ {"A", "I", "N", "M"}, // have 2 char /* 2 */ {"A", "I", "N", "M"}, // have 2 char
...@@ -60,15 +77,15 @@ public class AssessmentPartFour ...@@ -60,15 +77,15 @@ public class AssessmentPartFour
for(int i = 0; i < morseCode.size(); i++) for(int i = 0; i < morseCode.size(); i++)
//loops through each morse word within list (which has been taken from file) //loops through each morse word within list (which has been taken from file)
{ {
String strMorseLetter; String strUntranslated = morseCode.get(i);
int x = (((morseCode.get(i)).length()) * 2) - 1; int x = (((strUntranslated).length()) * 2) - 1;
//length of the morse input, times 2, minus 1 is the x co-ordinate of morse code within the array. //length of the morse input, times 2, minus 1 is the x co-ordinate of morse code within the array.
for(int y = 0; y < strMorseArray[x].length; y++) for(int y = 0; y < strMorseArray[x].length; y++)
//loop through to identify y co-ordinate //loop through to identify y co-ordinate
{ {
if(strMorseArray[x][y].contains(morseCode.get(i))) if(strMorseArray[x][y].contains(strUntranslated))
//contains runs fractionally faster than equals. //contains runs fractionally faster than equals.
//only checks against morse code which is the same length, so ".." containing "." is not an issue. //only checks against morse code which is the same length, so ".." containing "." is not an issue.
{ {
...@@ -81,7 +98,6 @@ public class AssessmentPartFour ...@@ -81,7 +98,6 @@ public class AssessmentPartFour
} }
strTranslated = strTranslated.toLowerCase(); strTranslated = strTranslated.toLowerCase();
//converting to lowercase to comply with unit test //converting to lowercase to comply with unit test
System.out.println("strTranslated = " + strTranslated);
return strTranslated; return strTranslated;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment