Commit f00b680b authored by Danie's avatar Danie

Final commit.

parent 402af014
...@@ -14,21 +14,22 @@ public class AssessmentPartThree { ...@@ -14,21 +14,22 @@ public class AssessmentPartThree {
public char enryptedCharacter(char theChar, int theOffset) { public char enryptedCharacter(char theChar, int theOffset) {
char returnChar = theChar; char returnChar = theChar;
int x = 0; int x = 0;
String alphabet = "abcdefghijklmnopqrstuvwxyz"; String alphabet = "abcdefghijklmnopqrstuvwxyz"; //String containing all the letters of the alphabet.
if (Character.isUpperCase(theChar)) { if (Character.isUpperCase(theChar)) {
alphabet = alphabet.toUpperCase(); alphabet = alphabet.toUpperCase(); //If the character is a capital letter then the alphabet string
//becomes upper case to match.
} }
for (int i = 0; i < alphabet.length(); i++) { for (int i = 0; i < alphabet.length(); i++) { //Increments through every letter of the alphabet.
char character = alphabet.charAt(i); char character = alphabet.charAt(i);
if (character == theChar) { if (character == theChar) { //if that letter of the alphabet is the same as the character then..
x = i + theOffset; x = i + theOffset; //that character is then moved by the offset.
if (x > 26) { if (x > 26) {
x = (x - 26); x = (x - 26); // if the offset goes outside of the alphabet then it loops back round the the start.
} }
if (x < 0) { if (x < 0) {
x = (x + 26); x = (x + 26); // if the offset goes backwards lower than A then it loops to the end of the alphabet.
} }
returnChar = alphabet.charAt(x); returnChar = alphabet.charAt(x); //Returns the encrypted character.
} }
} }
...@@ -45,13 +46,14 @@ public class AssessmentPartThree { ...@@ -45,13 +46,14 @@ public class AssessmentPartThree {
public String encryptedString(String theMessage, int theOffset) { public String encryptedString(String theMessage, int theOffset) {
String alphabet = "abcdefghijklmnopqrstuvwxyz"; String alphabet = "abcdefghijklmnopqrstuvwxyz";
String codedString = ""; String codedString = ""; //Sets up an empty string to be filled later.
char[] theMessageChars = theMessage.toCharArray(); char[] theMessageChars = theMessage.toCharArray(); //Converts theMessage to a character array.
int x = -1; int x = -1;
for (int i = 0; i < theMessage.length(); i++) { for (int i = 0; i < theMessage.length(); i++) { //Increments through theMessage character by character.
char character = theMessage.charAt(i); char character = theMessage.charAt(i);
codedString += enryptedCharacter(character, theOffset); codedString += enryptedCharacter(character, theOffset); //recalls the previous method to encrypt each character
//and then adds those characters into the empty string and returns the encrypted message.
} }
return codedString; return codedString;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment