Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
ChristopherFosterAssessmentPart4
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
christopher.foster
ChristopherFosterAssessmentPart4
Commits
c4aff524
Commit
c4aff524
authored
Jan 05, 2019
by
chris
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Uploaded
parents
Pipeline
#596
failed with stages
Changes
2
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
140 additions
and
0 deletions
+140
-0
AssessmentPartFour.java
AssessmentPartFour.java
+89
-0
AssessmentPartFourTest.java
AssessmentPartFourTest.java
+51
-0
No files found.
AssessmentPartFour.java
0 → 100644
View file @
c4aff524
import
java.util.ArrayList
;
import
java.io.FileNotFoundException
;
import
java.util.List
;
import
java.io.File
;
import
java.util.Scanner
;
public
class
AssessmentPartFour
{
List
<
String
>
morseCode
=
new
ArrayList
<
String
>();
public
int
loadMorseFromFile
(
String
filename
)
//throws IOException
{
//this deletes all data from the array list effectively making it a blank slate.
morseCode
.
clear
();
//this sets up a variable to represent the text file
File
morse
=
new
File
(
filename
);
//this creates an instance of a scanner so that the file can be read from.
Scanner
readLine
;
//This creates a try catch method that prevents the program from breaking if the file cannot be found.
try
{
//this creates a new instance of the scanner that allows it to read from the file
readLine
=
new
Scanner
(
morse
);
//this creates a while loop that will continue to iterate until there is no longer anything to read from the file
while
(
readLine
.
hasNextLine
())
{
//this adds the line that the program has just read into the array list
morseCode
.
add
(
readLine
.
nextLine
());
}
//This section of code runs if the file cannot be found and causes an error
}
catch
(
FileNotFoundException
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
}
//This returns the number of lines within the array list by calculating the size
return
morseCode
.
size
();
}
public
String
translateMorse
()
{
//this creates an instance of a string builder called message
StringBuilder
message
=
new
StringBuilder
();
//this is a for loop that will continuously loop until the array morseCode is empty
//this has also created a string that is equal to each string in the array morse
for
(
String
sentence:
morseCode
)
{
//this creates a string called decoded and uses the method translator to translate the string sentence
String
decoded
=
translator
(
sentence
);
//this uses the stringbuilder to add the decoded string to the message
message
.
append
(
decoded
);
}
//this returns the fully decoded message
return
message
.
toString
();
}
//this is an additional method that I created that translates the morse for me
public
String
translator
(
String
morseSentence
)
{
//this is an array that contains the english alphabet including a space
String
[]
englishAlphabet
=
{
"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"
,
" "
};
//this is an array that contains the morse alphabet
String
[]
morseAlphabet
=
{
".-"
,
"-..."
,
"-.-."
,
"-.."
,
"."
,
"..-."
,
"--."
,
"...."
,
".."
,
".---"
,
"-.-"
,
".-.."
,
"--"
,
"-."
,
"---"
,
".--."
,
"--.-"
,
".-."
,
"..."
,
"-"
,
"..-"
,
"...-"
,
".--"
,
"-..-"
,
"-.--"
,
"--.."
,
"/"
};
//this is a for loop that continues to loop until it reaches the end of the array morseAlphabet
for
(
int
i
=
0
;
i
<
morseAlphabet
.
length
;
++
i
){
//this checks whether the word in the array is equal to the letter in the morse alphabet
if
(
morseSentence
.
equals
(
morseAlphabet
[
i
]))
{
//if they are equal then this returns the translated word to the method translateMorse
return
englishAlphabet
[
i
];
}
}
//nothing is returned if the program is unable to decipher what is in the file.
return
null
;
}
}
AssessmentPartFourTest.java
0 → 100644
View file @
c4aff524
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
import
org.junit.jupiter.api.BeforeAll
;
import
org.junit.jupiter.api.DisplayName
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.params.ParameterizedTest
;
import
org.junit.jupiter.params.provider.CsvSource
;
class
AssessmentPartFourTest
{
public
static
AssessmentPartFour
test
;
@BeforeAll
static
void
setUpBeforeClass
()
throws
Exception
{
test
=
new
AssessmentPartFour
();
}
@ParameterizedTest
@DisplayName
(
"Testing loadMorseFromFile"
)
@CsvSource
({
"test1.txt,9"
,
"test2.txt,18"
,
"test3.txt,15"
,
"test4.txt,13"
})
void
testEnryptedCharacter
(
String
filename
,
int
count
)
{
assertEquals
(
count
,
test
.
loadMorseFromFile
(
filename
));
}
@ParameterizedTest
@DisplayName
(
"Testing translateMorse"
)
@CsvSource
({
"test1.txt,9,java code"
,
"test2.txt,18,programming is fun"
,
"test3.txt,15,fingers crossed"
,
"test4.txt,13,yippee did it"
})
void
testEncryptedString
(
String
filename
,
int
count
,
String
message
)
{
int
cc
=
test
.
loadMorseFromFile
(
filename
);
if
(
cc
==
count
)
{
assertEquals
(
message
,
test
.
translateMorse
());
}
else
{
fail
(
"File failed to load"
);
}
}
}
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