Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
ChristopherFosterAssessmentPart3
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
ChristopherFosterAssessmentPart3
Commits
29d792e3
Commit
29d792e3
authored
Jan 03, 2019
by
chris
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Uploaded to GIT
parents
Pipeline
#575
failed with stages
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
125 additions
and
0 deletions
+125
-0
AssessmentPartThree.java
AssessmentPartThree.java
+81
-0
AssessmentPartThreeTest.java
AssessmentPartThreeTest.java
+44
-0
No files found.
AssessmentPartThree.java
0 → 100644
View file @
29d792e3
public
class
AssessmentPartThree
{
// The simplest form of encryption is the rotation cipher (also known as Caeser's Cipher)
// An offset value is chosen to encrypt a message. Each letter is replaced by the
// letter that that number of places away from it.
// So if an offset value of 1 is chosen, each letter is replaced by the one after it
// - so a becomes b, b becomes c, etc
// If a value of -2 is chosen a becomes y, b becomes z and so on
public
char
enryptedCharacter
(
char
theChar
,
int
theOffset
)
{
// 05 - encryptedCharacter
// Complete this method so that it returns the encrypted character for
// theChar when and offset of theOffset is used
// So if theChar='m' and theOffset is 3 the method will return 'p'
// Lower case characters remain lower case, upper case remain upper case
// Any other characters are returned unchanged
//this line of code creates an instance of a stringbuilder called sb(stringbuilder for short)
StringBuilder
sb
=
new
StringBuilder
();
//this line of code creates a string that is equal to the value of the character
String
character
=
String
.
valueOf
(
theChar
);
//this line of code converts the string to an array of characters
for
(
char
i:
character
.
toCharArray
())
{
//this line of code checks whether the character is a letter, if it is then it runs the code
if
(
Character
.
isLetter
(
i
))
{
//this checks whether the letter is uppercase
if
(
Character
.
isUpperCase
(
i
))
{
//this line of code works out the new character and then adds it to the stringbuilder
sb
.
append
((
char
)
(
'A'
+
(
i
-
'A'
+
theOffset
)
%
26
));
//this section of code is executed if the letter is lowercase
}
else
{
//this line of code works out the new character and then adds it to the stringbuilder
sb
.
append
((
char
)
(
'a'
+
(
i
-
'a'
+
theOffset
)
%
26
));
}
}
//this section of code is executed if the character is not a letter
else
{
//this adds the character to the string builder, without making any changes
sb
.
append
(
i
);
}
}
//this creates a char type variable to represent the new encrypted character
char
encryptedChar
=
sb
.
charAt
(
0
);
//this returns the encrypted character
return
encryptedChar
;
}
public
String
encryptedString
(
String
theMessage
,
int
theOffset
)
{
// 06 - encryptedMessage
// Complete this method so that it uses encryptedCharacter to
// return the encrypted version of theMessage using theOffset
//creates an instance of a stringbuilder called sb
StringBuilder
sb
=
new
StringBuilder
();
//creates a for loop that will iterate for the number of characters in the string theMessage
for
(
int
i
=
0
;
i
<
theMessage
.
length
();
i
++)
{
//this divides the offset by 26 and then adds 26 after
theOffset
=
theOffset
%
26
+
26
;
//this creates a character that is equal to the current character in the string
char
character
=
theMessage
.
charAt
(
i
);
//this creates a variable to represent the encrypted character, using the method encryptedCharacter to work out what the encryption should be
char
encodedChar
=
this
.
enryptedCharacter
(
character
,
theOffset
);
//this adds the encoded character to the string
sb
.
append
(
encodedChar
);
}
//this returns the encrypted string
return
sb
.
toString
();
}
}
AssessmentPartThreeTest.java
0 → 100644
View file @
29d792e3
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
AssessmentPartThreeTest
{
public
static
AssessmentPartThree
test
;
@BeforeAll
static
void
setUpBeforeClass
()
throws
Exception
{
test
=
new
AssessmentPartThree
();
}
@ParameterizedTest
@DisplayName
(
"Testing encryptedCharacter"
)
@CsvSource
({
"a,5,f"
,
"q,-3,n"
,
"G,6,M"
,
"5,-3,5"
,
"T,-7,M"
})
void
testEnryptedCharacter
(
char
theChar
,
int
theOffset
,
char
encChar
)
{
assertEquals
(
encChar
,
test
.
enryptedCharacter
(
theChar
,
theOffset
));
}
@ParameterizedTest
@DisplayName
(
"Testing encryptedString"
)
@CsvSource
({
"hello,5,mjqqt"
,
"Java Coding,-3,Gxsx Zlafkd"
,
"dandelion,8,livlmtqwv"
,
"ktixevzout,-6,encryption"
})
void
testEncryptedString
(
String
theMessage
,
int
theOffset
,
String
encMessage
)
{
assertEquals
(
encMessage
,
test
.
encryptedString
(
theMessage
,
theOffset
));
}
}
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