Commit 77b88575 authored by Neil's avatar Neil

21:31 03/12/18

parent 39bb1959
...@@ -8,7 +8,29 @@ public class AssessmentPartThree { ...@@ -8,7 +8,29 @@ public class AssessmentPartThree {
// - so a becomes b, b becomes c, etc // - so a becomes b, b becomes c, etc
// If a value of -2 is chosen a becomes y, b becomes z and so on // If a value of -2 is chosen a becomes y, b becomes z and so on
public char enryptedCharacter(char theChar, int theOffset) public static void main(String[] args)
{
String input = "z";
int offset = 3;
System.out.print(input + " ");
System.out.println(encryptedString(input, offset));
System.out.println(-3 % 26);
char a3 = (char) ((char) 'z' - 2);
System.out.println(a3);
}
/*
"hello,5,mjqqt",
"Java Coding,-3,Gxsx Zlafkd",
"dandelion,8,livlmtqwv",
"ktixevzout,-6,encryption"
*/
public static char enryptedCharacter(char theChar, int theOffset) //REMEMBER TO REMOVE STATIC
{ {
// 05 - encryptedCharacter // 05 - encryptedCharacter
// Complete this method so that it returns the encrypted character for // Complete this method so that it returns the encrypted character for
...@@ -18,46 +40,50 @@ public class AssessmentPartThree { ...@@ -18,46 +40,50 @@ public class AssessmentPartThree {
// Any other characters are returned unchanged // Any other characters are returned unchanged
//Uppercase characters //Uppercase characters
if((theChar >= 65) && (theChar <= 90)) //65 to 90 = A to B if((theChar >= 65) && (theChar <= 90)) //65 to 90 = A to B
{ {
theChar = (char) (((theChar - 65 + theOffset) % 26) + 65); theChar = (char) (((theChar - 65 + theOffset) % 26) + 65);
} }
//Lowercase characters //Lowercase characters
else if((theChar >= 97) && (theChar <= 122)) // 97 to 122 = a to b else if((theChar >= 97) && (theChar <= 122)) // 97 to 122 = a to b
{ {
theChar = (char) (((theChar - 97 + theOffset) % 26) + 97); theChar = (char) (((theChar - 97 + theOffset) % 26) + 97);
} }
/* /*
theChar = (char) (((theChar - 97 + theOffset) % 26) + 97); formulae theChar = (char) (((theChar - 97 + theOffset) % 26) + 97); formulae
(theChar - 97 - ASCII a/A to give value between 1 and 26 (theChar - 97 - ASCII a/A to give value between 1 and 26
+ theOffset) + the offset value + theOffset) + the offset value
( % 26) % modulo ensures value is always between 1 and 26 ( % 26) % modulo ensures value is always between 1 and 26
( + 97) + ASCII a/A to return result to ASCII char ( + 97) + ASCII a/A to return result to ASCII char
(char) result needs to be casted to char (char) result needs to be casted to char
*/ */
return theChar; //COPY THIS OVER FROM LAB COMPUTER TO PRESERVE FORMATTING
return theChar;
} }
//THE PROBLEM IS WITH MINUS NUMBERS!!!! NEED TO HAVE A WAY TO DEAL WITH IT ROLLING BACKWARDS!!!
public String encryptedString(String theMessage, int theOffset) public static String encryptedString(String theMessage, int theOffset) //REMEMBER TO REMOVE STATIC
{ {
// 06 - encryptedMessage // 06 - encryptedMessage
// 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));
//converting result array into string, and returning this //converting result array into string, and returning this
} }
} }
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