Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
Assignment Part 4
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
bran.harvey-chadwi
Assignment Part 4
Commits
37b32f75
Commit
37b32f75
authored
Dec 11, 2018
by
Bran
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
completed
parent
c899adb3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
17 deletions
+71
-17
AssessmentPartFour.java
src/AssessmentPartFour.java
+65
-15
AssessmentPartFourTest.java
src/AssessmentPartFourTest.java
+6
-2
No files found.
src/AssessmentPartFour.java
View file @
37b32f75
import
java.util.ArrayList
;
import
java.util.List
;
import
java.io.*
;
import
java.util.Scanner
;
public
class
AssessmentPartFour
{
public
class
AssessmentPartFour
{
List
<
String
>
morseCode
=
new
ArrayList
<
String
>();
List
<
String
>
morseCode
=
new
ArrayList
<
String
>();
//creates an arraylist called morseCode that can handle strings
public
int
loadMorseFromFile
(
String
filename
)
public
int
loadMorseFromFile
(
String
filename
)
{
File
file
=
new
File
(
filename
);
// creates a instance of a file
morseCode
.
clear
();
// clears the contents of the array "morseCode
return
0
;
Scanner
sc
=
null
;
// creates a scanner called sc and assigns it a value of null
try
//creates a try block and attempts to run the code inside it
{
sc
=
new
Scanner
(
file
);
//assigns the value of file to the scanner sc
}
catch
(
FileNotFoundException
e
)
// if an exception is thrown from assigning the value of "file" to the scanner sc,
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
// if an exception is met, prints the stack trace of the instance ready for debugging
}
String
lineFromFile
;
// creates a string called lineFromFile
while
(
sc
.
hasNextLine
())
//creates a while loop as long as sc has another line waiting
{
lineFromFile
=
sc
.
nextLine
();
// makes lineFromFile equal to the
morseCode
.
add
(
lineFromFile
);
//adds the value assigned to lineFromFile to the array morseCode
}
return
morseCode
.
size
();
//returns the size of the array string morseCode
}
public
String
translateMorse
()
//////////////////////////////////////////////
public
String
translateMorse
(
String
filename
)
{
return
""
;
}
}
File
file
=
new
File
(
filename
);
//creates an instance of a file
morseCode
.
clear
();
// clears the contents of morseCode
//creates an array string containing the alphabet in morse code
String
[]
morse
=
{
".-"
,
"-..."
,
"-.-."
,
"-.."
,
"."
,
"..-."
,
"--."
,
"...."
,
".."
,
".---"
,
"-.-"
,
".-.."
,
"--"
,
"-."
,
"---"
,
".--."
,
"--.-"
,
".-."
,
"..."
,
"-"
,
"..-"
,
"...-"
,
".--"
,
"-..-"
,
"-.--"
,
"--.."
,
"/"
};
String
[]
alphabet
=
{
"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"
,
" "
};
//creates an array string containing the alphabet in lower case
Scanner
sc
=
null
;
//Assigns a value of null to the scanner "sc"
try
//creates a try block and attempts to run the code inside it
{
sc
=
new
Scanner
(
file
);
// creates a new scanner and assigns the value of "file" to it
}
catch
(
FileNotFoundException
e
)
// if the scanner cannot assign the value of "file" , run the following
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
// if an exception is met, prints the stack trace of the instance ready for debugging
}
String
lineFromFile
;
//creates a string called lineFromFile
while
(
sc
.
hasNextLine
())
//creates a while loop aslong as as sc has another line waiitng
{
lineFromFile
=
sc
.
nextLine
();
//stores the next line in the scanner sc
morseCode
.
add
(
lineFromFile
);
// assigns the value of lineFromfile to morsecode
}
String
message
=
""
;
//creates a string called message and assigns it nothing
for
(
int
i
=
0
;
i
<
morseCode
.
size
();
i
++)
//creates a for loop where i = 0 and runs for the size of the array morseCode
{
for
(
int
x
=
0
;
x
<
alphabet
.
length
;
x
++)
//creates a nested for loop where x = 0 and runs for the the length of alphabet.length (so 26 times)
{
if
(
morseCode
.
get
(
i
).
equals
(
morse
[
x
]))
// if the i'th element of morsecode is equal to the x 'th value of the array morse
{
morseCode
.
set
(
i
,
alphabet
[
x
]
);
// sets the value of i and the x'th value of alphabet ine the array morseCode
message
=
message
+
alphabet
[
x
];
// makes message equal to message + the x'th value of alphabet
}
}
}
return
message
;
//returns the value of message
}
}
\ No newline at end of file
src/AssessmentPartFourTest.java
View file @
37b32f75
...
...
@@ -27,6 +27,7 @@ class AssessmentPartFourTest {
})
void
testEnryptedCharacter
(
String
filename
,
int
count
)
{
assertEquals
(
count
,
test
.
loadMorseFromFile
(
filename
));
test
.
morseCode
.
clear
();
}
@ParameterizedTest
...
...
@@ -38,14 +39,17 @@ class AssessmentPartFourTest {
"test4.txt,13,yippee did it"
})
void
testEncryptedString
(
String
filename
,
int
count
,
String
message
)
{
test
.
morseCode
.
clear
();
int
cc
=
test
.
loadMorseFromFile
(
filename
);
if
(
cc
==
count
)
{
assertEquals
(
message
,
test
.
translateMorse
());
assertEquals
(
message
,
test
.
translateMorse
(
filename
));
}
else
{
fail
(
"File failed to load"
);
}
test
.
morseCode
.
clear
();
}
}
}
\ No newline at end of file
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