Commit df9f6a1f authored by Ben's avatar Ben

Part Three Completed and Commented

parent 51cb1380
Pipeline #555 failed with stages
......@@ -17,20 +17,24 @@ public class AssessmentPartThree {
// Lower case characters remain lower case, upper case remain upper case
// Any other characters are returned unchanged
//Checks whether the character is a digit or a whitespace. If so then the digit/whitespace is returned as there is no encrypted character for these.
if (Character.isDigit(theChar) || Character.isWhitespace(theChar)) {
return theChar;
}
//Initialises a character array with all the alphabetical characters.
char[] letters = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
if (Character.toUpperCase(theChar) == theChar) {
if (Character.toUpperCase(theChar) == theChar) { //Tests if the current character is upper case.
//If so then the letters array is converted to uppercase characters.
for (int i=0; i<letters.length;i++) {
letters[i] = Character.toUpperCase(letters[i]);
}
}
//Searches the letters array, finding the index of the current character in the alphabet.
//If no character is found, the index is left as -1; indicating that the character cannot be encrypted using this algorithm.
int index = -1;
for (int i=0; i<letters.length; i++) {
if (theChar == letters[i]) {
......@@ -38,25 +42,29 @@ public class AssessmentPartThree {
}
}
//Checks if the character can be ciphered.
if (index != -1) {
/*If the index of the ciphered character is less or equal to the length of the letters array
and it is greater or equal to zero, then the index of the ciphered character is simply the sum of the index and theOffset.
*/
if (((index+theOffset) <= letters.length) && (((index + theOffset) >= 0))) {
index = index + theOffset;
}
else if ((index + theOffset) < 0) {
index = letters.length + (index + theOffset);
else if ((index + theOffset) < 0) { //Otherwise if the index of ciphered character is less than zero, the index of the ciphered character is
index = letters.length + index + theOffset; //the sum of the length of the alphabet array, the index and theOffset.
}
else {
index = (index + theOffset) - letters.length;
else { //Otherwise if the calculated index of the ciphered character is greater than length of the alphabet array, the index of the ciphered character is
index = (index + theOffset) - letters.length; //subtracting the length of the alphabet from the sum of the index and theOffset.
}
}
if (index == -1) {
return theChar;
if (index == -1) { //If the ciphered character cannot be calculated.
return theChar; //Return the original character.
}
else {
return letters[index];
else { //If the ciphered character can be calculated.
return letters[index]; //Return the character at the new index.
}
}
......@@ -65,12 +73,12 @@ public class AssessmentPartThree {
// 06 - encryptedMessage
// Complete this method so that it uses encryptedCharacter to
// return the encrypted version of theMessage using theOffset
String message = "";
String message = ""; //Initialises variable for the cipher text.
for (int i=0; i<theMessage.length();i++) {
char current = theMessage.charAt(i);
char newChar = enryptedCharacter(current, theOffset);
message = message + newChar;
for (int i=0; i<theMessage.length();i++) { //Iterates through the plaintext string.
char current = theMessage.charAt(i); //Gets the character at the current index.
char newChar = enryptedCharacter(current, theOffset); //Calls the encryptedCharacter function to calculate and return the encrypted character.
message = message + newChar; //Adds the new encrypted character to the cipher text string.
}
return message;
......
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