Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Programming-01-Assesment-04
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
ben.coxford
Programming-01-Assesment-04
Commits
cdc6208f
Commit
cdc6208f
authored
Dec 30, 2018
by
Ben
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Part 4 Completed and Annotated.
parent
c899adb3
Pipeline
#557
canceled with stages
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
2 deletions
+50
-2
AssessmentPartFour.java
src/AssessmentPartFour.java
+50
-2
No files found.
src/AssessmentPartFour.java
View file @
cdc6208f
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
//Imports hash maps.
import
java.io.*
;
//Imports all classes from java.io package.
import
java.util.Scanner
;
//Imports scanner to read text file.
import
java.util.List
;
import
java.util.List
;
public
class
AssessmentPartFour
{
public
class
AssessmentPartFour
{
...
@@ -7,15 +10,60 @@ public class AssessmentPartFour {
...
@@ -7,15 +10,60 @@ public class AssessmentPartFour {
public
int
loadMorseFromFile
(
String
filename
)
public
int
loadMorseFromFile
(
String
filename
)
{
{
File
mFile
=
new
File
(
filename
);
//Opens the file.
return
0
;
morseCode
.
clear
();
//Empties or clears the global array morseCode.
Scanner
sc
;
//Defines the scanner.
try
{
//Tries to open the file.
sc
=
new
Scanner
(
mFile
);
//Initialises the scanner with the file.
String
nLine
;
while
(
sc
.
hasNextLine
())
{
//Repeats every time there is another line.
nLine
=
sc
.
nextLine
();
morseCode
.
add
(
nLine
);
//Adds the next line to the morseCode array.
}
sc
.
close
();
//Closes the file.
}
catch
(
FileNotFoundException
e
)
{
//If the file cannot be found the exception is caught.
System
.
out
.
println
(
e
.
getLocalizedMessage
());
//Prints out the localised message for the error.
}
return
morseCode
.
size
();
//Returns the size of the morseCode array.
}
}
public
char
morseToEnglish
(
String
findCode
)
{
//Method to convert morse code to a character.
//Defines and initialises two arrays to store the letters and their corresponding morse code value.
char
[]
letters
=
{
'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'
,
'1'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
'8'
,
'9'
,
'0'
};
String
[]
code
=
{
".-"
,
"-..."
,
"-.-."
,
"-.."
,
"."
,
"..-."
,
"--."
,
"...."
,
".."
,
".---"
,
"-.-"
,
".-.."
,
"--"
,
"-."
,
"---"
,
".--."
,
"--.-"
,
".-."
,
"..."
,
"-"
,
"..-"
,
"...-"
,
".--"
,
"-..-"
,
"-.--"
,
"--.."
,
".----"
,
"..---"
,
"...---"
,
"...--"
,
"....-"
,
"....."
,
"-...."
,
"--..."
,
"---.."
,
"----."
,
"-----"
};
//Defines and initialises a HashMap.
HashMap
<
String
,
Character
>
keys
=
new
HashMap
<
String
,
Character
>();
for
(
int
i
=
0
;
i
<
letters
.
length
;
i
++)
{
//For length of the letters array.
keys
.
put
(
code
[
i
],
letters
[
i
]);
//Add to the hash map each code and their corresponding character value. (Pairs)
}
return
keys
.
get
(
findCode
);
//Gets the character from the HashMap, using the morse code as the key.
}
public
String
translateMorse
()
public
String
translateMorse
()
{
{
return
""
;
String
message
=
""
;
//Initialises the final message.
for
(
int
i
=
0
;
i
<
morseCode
.
size
();
i
++)
{
//Iterates to length of the morse code message.
if
(
morseCode
.
get
(
i
).
equals
(
"/"
))
{
//If the code is equal to a / then the whitespace is added to the message.
message
=
message
+
" "
;
}
else
{
//Calls the method to return the English character and adds it to the message.
char
toAdd
=
morseToEnglish
(
morseCode
.
get
(
i
));
message
=
message
+
Character
.
toString
(
toAdd
);
}
}
return
message
;
//Returns the final message.
}
}
...
...
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