Commit 2fff3ac5 authored by neil.whitehead's avatar neil.whitehead

12:19 10/12/18

parent 39bb1959
public class AssessmentPartThree { public class AssessmentPartThree
{
// The simplest form of encryption is the rotation cipher (also known as Caeser's Cipher) // 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 // An offset value is chosen to encrypt a message. Each letter is replaced by the
...@@ -17,25 +18,31 @@ public class AssessmentPartThree { ...@@ -17,25 +18,31 @@ public class AssessmentPartThree {
// Lower case characters remain lower case, upper case remain upper case // Lower case characters remain lower case, upper case remain upper case
// Any other characters are returned unchanged // Any other characters are returned unchanged
//Uppercase characters //Dealing with negative offsets
if((theChar >= 65) && (theChar <= 90)) //65 to 90 = A to B theOffset = (theOffset < 0) ? 26 + theOffset : theOffset;
{ //ensures that the offset will "wrap around"
theChar = (char) (((theChar - 65 + theOffset) % 26) + 65);
} //Uppercase characters
//Lowercase characters if((theChar >= 65) && (theChar <= 90)) //65 to 90 = A to B
else if((theChar >= 97) && (theChar <= 122)) // 97 to 122 = a to b {
{ theChar = (char) (((theChar - 65 + theOffset) % 26) + 65);
theChar = (char) (((theChar - 97 + theOffset) % 26) + 97); }
} //Lowercase characters
/* else if((theChar >= 97) && (theChar <= 122)) // 97 to 122 = a to b
theChar = (char) (((theChar - 97 + theOffset) % 26) + 97); formulae {
(theChar - 97 - ASCII a/A to give value between 1 and 26 theChar = (char) (((theChar - 97 + theOffset) % 26) + 97);
+ theOffset) + the offset value }
( % 26) % modulo ensures value is always between 1 and 26 /*
( + 97) + ASCII a/A to return result to ASCII char theChar = (char) (((theChar - 97 + theOffset) % 26) + 97); formulae
(char) result needs to be casted to char (theChar - 97 - ASCII a/A to give value between 1 and 26
*/ + theOffset) + the offset value
return theChar; ( % 26) % modulo ensures value is always between 1 and 26
( + 97) + ASCII a/A to return result to ASCII char
(char) result needs to be casted to char
*/
return theChar;
//any non alphabetical chars will fall through and return original char
} }
...@@ -45,15 +52,15 @@ public class AssessmentPartThree { ...@@ -45,15 +52,15 @@ public class AssessmentPartThree {
// Complete this method so that it uses encryptedCharacter to // Complete this method so that it uses encryptedCharacter to
// return the encrypted version of theMessage using theOffset // return the encrypted version of theMessage using theOffset
char chaMessageArr[] = theMessage.toCharArray(); char chaMessageArr[] = theMessage.toCharArray();
char[] chaResultArr = new char[chaMessageArr.length]; char[] chaResultArr = new char[chaMessageArr.length];
//initialising Arrays //initialising Arrays
for(int i = 0; i < chaMessageArr.length; i++) for(int i = 0; i < chaMessageArr.length; i++)
//loop to run through each character in encrypted message //loop to run through each character in encrypted message
{ {
chaResultArr[i] = enryptedCharacter(chaMessageArr[i], theOffset); chaResultArr[i] = enryptedCharacter(chaMessageArr[i], theOffset);
//passing each character to decryption method and saving result in array //passing each character to decryption method and saving result in array
} }
return (new String(chaResultArr)); return (new String(chaResultArr));
......
...@@ -23,7 +23,10 @@ class AssessmentPartThreeTest { ...@@ -23,7 +23,10 @@ class AssessmentPartThreeTest {
"q,-3,n", "q,-3,n",
"G,6,M", "G,6,M",
"5,-3,5", "5,-3,5",
"T,-7,M" "T,-7,M",
"a,31,f",
"T,-33,M",
"!, 2, !"
}) })
void testEnryptedCharacter(char theChar, int theOffset, char encChar) { void testEnryptedCharacter(char theChar, int theOffset, char encChar) {
assertEquals(encChar,test.enryptedCharacter(theChar, theOffset)); assertEquals(encChar,test.enryptedCharacter(theChar, theOffset));
......
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