Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Part04
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Assessment
Part04
Commits
75369544
Commit
75369544
authored
Jan 04, 2019
by
neil.whitehead
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
FINAL
parent
aafe3ea0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
31 deletions
+47
-31
AssessmentPartFour.java
src/AssessmentPartFour.java
+47
-31
No files found.
src/AssessmentPartFour.java
View file @
75369544
/**
* <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
/**
* Populates list with input from text file <br>
//COMMENT THIS A LOT BETTER
* 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
(
FileReader
file
=
new
FileReader
(
filename
);
BufferedReader
buffer
=
new
BufferedReader
(
file
))
//try/catch blocks will ensure that the program can recover from errors
try
//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
);
//buffer.close(); not needed as try will automatically close
//. . . add to list
}
}
catch
(
IOException
e
)
//buffer.close(); not needed as try-with-resources will automatically close
{
}
System
.
out
.
println
(
"error"
);
catch
(
IOException
e
)
}
//only used when error occurs.
{
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
str
MorseLetter
;
String
str
Untranslated
=
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
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment