Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
AssessmentP3
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
samuel.boulton
AssessmentP3
Commits
083082af
Commit
083082af
authored
Jan 02, 2019
by
samuelboulton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finsihed
parent
a063fb92
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
16 deletions
+33
-16
AssessmentPartThree.java
src/AssessmentPartThree.java
+33
-16
No files found.
src/AssessmentPartThree.java
View file @
083082af
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
/* 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
/* 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
*/
char
encChar
=
theChar
;
// Declaring upper and lower-case letters in a String
String
lowerCase
=
"abcdefghijklmnopqrstuvwxyz"
;
String
upperCase
=
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
;
if
(
Character
.
isLetter
(
theChar
))
...
...
@@ -24,15 +27,19 @@ public class AssessmentPartThree {
// Seeing if the character given is lower-case
if
(
Character
.
isLowerCase
(
theChar
))
{
// give a the character a position number from the lower-case String
int
position
=
lowerCase
.
indexOf
(
theChar
);
// Adding the offSet to the position number
int
newPosLower
=
position
+
theOffset
;
// making sure that the alphabet is a continuous cycle.
if
(
newPosLower
>
25
)
{
// if the offSet moves it past 'z' then take 26 off newPosLower.
newPosLower
-=
26
;
}
else
if
(
newPosLower
<
0
)
{
// if the offSet goes back past 'a' then add 26 to newPosLower.
newPosLower
+=
26
;
}
encChar
=
lowerCase
.
charAt
(
newPosLower
);
...
...
@@ -45,12 +52,15 @@ public class AssessmentPartThree {
{
int
position
=
upperCase
.
indexOf
(
theChar
);
int
newPosUpper
=
position
+
theOffset
;
// making sure that the alphabet is a continuous cycle.
if
(
newPosUpper
>
25
)
{
// if the offSet moves it past 'Z' then take 26 off newPosLower.
newPosUpper
-=
26
;
}
else
if
(
newPosUpper
<
0
)
{
// if the offSet goes back past 'A' then add 26 to newPosLower.
newPosUpper
+=
26
;
}
encChar
=
upperCase
.
charAt
(
newPosUpper
);
...
...
@@ -58,6 +68,7 @@ public class AssessmentPartThree {
return
encChar
;
}
}
// if letter isn't a character then return original value
else
{
return
encChar
;
...
...
@@ -69,13 +80,19 @@ public class AssessmentPartThree {
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
/* 06 - encryptedMessage
Complete this method so that it uses encryptedCharacter to
return the encrypted version of theMessage using theOffset
*/
String
encMessage
=
""
;
// turning theMessage into characters
char
[]
chars
=
theMessage
.
toCharArray
();
// for loop to go through each char
for
(
int
i
=
0
;
i
<
chars
.
length
;
i
++)
{
// using the method above to calculate each character separately
//then adding them to the String 'encMessage'
encMessage
+=
enryptedCharacter
(
chars
[
i
],
theOffset
);
}
return
encMessage
;
...
...
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