Commit f85758b3 authored by neil.whitehead's avatar neil.whitehead

FINAL

parent ff9e8560
/**
* <b>Assessment</b> part three<br>
* <b>Created</b> 08/11/18<br>
* <b>Modified</b> 04/01/18<br>
* @author <a href = "mailto:neil.whitehead@yorksj.ac.uk"> Neil Whitehead</a>, <a href = "mailto:a.guest@yorksj.ac.uk">Andrew Guest</a>
*/
public class AssessmentPartThree
{
// 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
// letter that that number of places away from it.
// So if an offset value of 1 is chosen, each letter is replaced by the one after it
// - so a becomes b, b becomes c, etc
// If a value of -2 is chosen a becomes y, b becomes z and so on
/**
* Descripts characters using Caesar cipher <br>
* Assumes that no validation checks are required.
* @param theChar char
* @param theOffset int
* @return theChar char
*/
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
{
//Dealing with negative offsets
theOffset = (theOffset < 0) ? 26 + theOffset : theOffset;
//ensures that the offset will "wrap around" by finding corresponding positive value
//ensures that the offset will properly "wrap around" by finding complementary positive value
//Uppercase characters
if((theChar >= 65) && (theChar <= 90)) //65 to 90 = A to B
......@@ -40,21 +38,24 @@ public class AssessmentPartThree
( + 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
}
/**
* Decripts strings using Caesar cipher <br>
* Assumes that no validation checks are required.
* @param theMessage string
* @param theOffset int
* @return chaResultArr string
*/
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
char chaMessageArr[] = theMessage.toCharArray();
char[] chaResultArr = new char[chaMessageArr.length];
//initialising Arrays
//initialising Arrays
for(int i = 0; i < chaMessageArr.length; i++)
//loop to run through each character in encrypted message
......@@ -62,7 +63,6 @@ public class AssessmentPartThree
chaResultArr[i] = enryptedCharacter(chaMessageArr[i], theOffset);
//passing each character to decryption method and saving result in array
}
return (new String(chaResultArr));
//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