Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Programming 01 Task 5-6
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.spray
Programming 01 Task 5-6
Commits
19664b29
Commit
19664b29
authored
Jan 07, 2019
by
spray
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
changed to correct assesmment part
parent
e56f0d16
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
73 additions
and
12 deletions
+73
-12
AssessmentPartThree.java
src/AssessmentPartThree.java
+73
-12
No files found.
src/AssessmentPartThree.java
View file @
19664b29
import
java.util.ArrayList
;
public
class
AssessmentPartThree
{
...
...
@@ -10,23 +11,83 @@ public class AssessmentPartThree {
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'
;
ArrayList
<
Character
>
alphabet
=
new
ArrayList
<
Character
>();
//Create arraylist to store alphabet
alphabet
.
add
(
'a'
);
//add a to end of list
alphabet
.
add
(
'b'
);
//add b to end of list
alphabet
.
add
(
'c'
);
//repeat for all of alphabet
alphabet
.
add
(
'd'
);
alphabet
.
add
(
'e'
);
alphabet
.
add
(
'f'
);
alphabet
.
add
(
'g'
);
alphabet
.
add
(
'h'
);
alphabet
.
add
(
'i'
);
alphabet
.
add
(
'j'
);
alphabet
.
add
(
'k'
);
alphabet
.
add
(
'l'
);
alphabet
.
add
(
'm'
);
alphabet
.
add
(
'n'
);
alphabet
.
add
(
'o'
);
alphabet
.
add
(
'p'
);
alphabet
.
add
(
'q'
);
alphabet
.
add
(
'r'
);
alphabet
.
add
(
's'
);
alphabet
.
add
(
't'
);
alphabet
.
add
(
'u'
);
alphabet
.
add
(
'v'
);
alphabet
.
add
(
'w'
);
alphabet
.
add
(
'x'
);
alphabet
.
add
(
'y'
);
alphabet
.
add
(
'z'
);
Boolean
upper
=
false
;
//create a boolean that will be used later
if
(
Character
.
isUpperCase
(
theChar
)
==
true
)
{
//if the letter needing encryption is upper case then..
upper
=
true
;
//set upper boolean to true
}
if
(
theChar
==
' '
)
{
//if the character is a space
return
' '
;
//return a space (to keep words separate)
}
if
(
Character
.
isDigit
(
theChar
))
{
//if the character is a number
return
theChar
;
//return it as a number (so it doesn't encrypt numbers)
}
theChar
=
Character
.
toLowerCase
(
theChar
);
//convert all chars to lowercase before they get encrypted
int
position
=
alphabet
.
indexOf
(
theChar
);
//store the array position of the char
int
encrypted
=
position
+
theOffset
;
//the encrypted version of the letter is the current pos + the shift/offset
if
(
encrypted
<
0
)
{
//if new position is lower than 0 (off the array)
encrypted
=
encrypted
+
26
;
//add 26 to it so it loops
}
if
(
encrypted
>
26
)
{
//if new position is higher than 26 (off the array)
encrypted
=
encrypted
-
26
;
//take away 26 so it loops
}
char
end
=
alphabet
.
get
(
encrypted
);
//use the index to return the character in that position
if
(
upper
)
{
//if upper was true...
end
=
Character
.
toUpperCase
(
end
);
//put the encrypted character back to uppercase
}
return
end
;
//return the encrypted letter.
}
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"
;
char
[]
letters
=
theMessage
.
toCharArray
();
//Convert theMessage to a character Array
String
encrypted
=
""
;
//Create an empty string
for
(
int
i
=
0
;
i
<
letters
.
length
;
i
++)
{
//for 0 to length of the char array -1
encrypted
+=
enryptedCharacter
(
letters
[
i
],
theOffset
);
//apply encryptedCharacter function to each letter by the new offset
}
return
encrypted
;
//
}
}
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