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

12:30 03/01/19

parent 1cbc0815
...@@ -4,16 +4,11 @@ import java.io.*; ...@@ -4,16 +4,11 @@ import java.io.*;
public class AssessmentPartFour public class AssessmentPartFour
{ {
List<String> morseCode = new ArrayList<String>(); List<String> morseCode = new ArrayList<String>();
//COMMENT THIS A LOT BETTER
public int loadMorseFromFile(String filename) public int loadMorseFromFile(String filename)
{ {
...@@ -43,57 +38,51 @@ public class AssessmentPartFour ...@@ -43,57 +38,51 @@ public class AssessmentPartFour
//MOVE THE .GET INTO IT'S OWN VARIABLE.
public String translateMorse() public String translateMorse()
{ {
for(int i = 0; i < morseCode.size(); i++) String strTranslated = "";
{
System.out.println(morseCode.get(i) + " " + (morseCode.get(i)).length());
}
String strTranslated = "strTranslated ";
String[][] strMorseArray = String[][] strMorseArray =
{{" ", "E", "T"}, // have 1 char //morse-letter lookup table, ordered by morse code length
{"/", ".", "-"}, /* 0 */ {{" ", "E", "T"}, // have 1 char
{"A", "I", "N", "M"}, // have 2 char /* 1 */ {"/", ".", "-"},
{".-", "..", "-.", "--"}, /* 2 */ {"A", "I", "N", "M"}, // have 2 char
{"D", "G", "K", "O", "R", "S", "U", "W"}, // have 3 char /* 3 */ {".-", "..", "-.", "--"},
{"-..", "--.", "-.-", "---", ".-.", "...", "..-", ".--"}, /* 4 */ {"D", "G", "K", "O", "R", "S", "U", "W"}, // have 3 char
{"B", "C", "F", "H", "J", "L", "P", "Q", "V", "X", "Y", "Z"}, // have 4 char /* 5 */ {"-..", "--.", "-.-", "---", ".-.", "...", "..-", ".--"},
{"-...", "-.-.", "..-.", "....", ".---", ".-..", ".--.", "--.-", "..._", "-..-", "-.--", "--.."}, /* 6 */ {"B", "C", "F", "H", "J", "L", "P", "Q", "V", "X", "Y", "Z"}, // have 4 char
{"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}, // have 5 char /* 7 */ {"-...", "-.-.", "..-.", "....", ".---", ".-..", ".--.", "--.-", "...-", "-..-", "-.--", "--.."},
{".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----"}}; /* 8 */ {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}, // have 5 char
/* 9 */ {".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----"}};
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)
{ {
String strMorseLetter;
int x = (((morseCode.get(i)).length()) * 2) - 1; int x = (((morseCode.get(i)).length()) * 2) - 1;
//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
{ {
System.out.println("something");
System.out.println(strMorseArray[x-1][y]);
if(strMorseArray[x][y].contains(morseCode.get(i))) if(strMorseArray[x][y].contains(morseCode.get(i)))
//contains runs fractionally faster than equals.
//only checks against morse code which is the same length, so ".." containing "." is not an issue.
{ {
strTranslated.concat(strMorseArray[x-1][y]); strTranslated = strTranslated.concat(strMorseArray[x-1][y]);
strMorseArray[x-1][y].concat(strTranslated); //x minus one = corresponding translation of morse code within the array
//adding recently matched letter to translated string
break; break;
} }
} }
} }
//NEED TO SORT OUT LOOPS. WHY HAVE I DIVIDED THE MORSE ARRAY LIKE THIS AGAIN? WHY AREN'T MY STRINGS CONCATNATING? strTranslated = strTranslated.toLowerCase();
//converting to lowercase to comply with unit test
System.out.println(strTranslated); System.out.println("strTranslated = " + strTranslated);
return ""; 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