Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
assignment 03
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
nathan.smyth
assignment 03
Commits
497376d1
Commit
497376d1
authored
Jan 07, 2019
by
Invate
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
first
parent
e56f0d16
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
70 additions
and
29 deletions
+70
-29
AssessmentPartThree.java
src/AssessmentPartThree.java
+70
-29
No files found.
src/AssessmentPartThree.java
View file @
497376d1
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
return
'a'
;
}
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
return
"Encryptred message"
;
}
// 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
int
ascii
=
(
int
)
theChar
;
//character turned into ascii value to shift by theOffset easily
char
character
=
'a'
;
//declaring the return variable
if
((
ascii
>
64
&&
ascii
<
92
)
||
(
96
<
ascii
&&
ascii
<
123
))
//if the ascii value is withing the letter ranges then it is shifted
{
character
=
(
char
)
(
ascii
+
theOffset
);
//applies the shift and turns the ascii value back into a char
if
((
ascii
+
theOffset
)
>
122
)
{
character
=
(
char
)((
ascii
+
theOffset
)-
26
);
//if the letter is shifted past z it loops back around to a
}
else
if
((
ascii
+
theOffset
)
>
90
&&
(
ascii
+
theOffset
)<
97
)
{
character
=
(
char
)((
ascii
+
theOffset
)+
26
);
//if the character is shifted below a then it loops around to z
}
else
if
((
ascii
+
theOffset
)
<
65
)
{
character
=
(
char
)((
ascii
+
theOffset
)+
26
);
//if the character is shifted below upper case A then it loops around to Z
}
else
if
((
ascii
+
theOffset
)
<
97
&&
(
ascii
+
theOffset
)>
90
)
{
character
=
(
char
)((
ascii
+
theOffset
)-
26
);
//if the character is shifted past upper case Z then it loops around to A
}
}
else
{
character
=
(
char
)
ascii
;
// if the character is not a letter it remains the same;
}
return
character
;
//returns character after shift
}
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
char
[]
message
=
theMessage
.
toCharArray
();
//creates a char array
for
(
int
i
=
0
;
i
<
theMessage
.
length
();
i
++)
{
message
[
i
]
=
enryptedCharacter
(
message
[
i
],
theOffset
);
//steps through the array and shifts the characters by theOffset
}
String
encryptedMessage
=
""
;
//declaring return string
for
(
int
j
=
0
;
j
<
message
.
length
;
j
++)
{
encryptedMessage
=
encryptedMessage
+
message
[
j
];
//steps through the char array a final time and concatenates the elements into a string
}
return
encryptedMessage
;
}
}
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