Commit 083082af authored by samuelboulton's avatar samuelboulton

Finsihed

parent a063fb92
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
/* 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
*/
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
/* 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
*/
char encChar = theChar;
// Declaring upper and lower-case letters in a String
String lowerCase = "abcdefghijklmnopqrstuvwxyz";
String upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if(Character.isLetter(theChar))
......@@ -24,15 +27,19 @@ public class AssessmentPartThree {
// Seeing if the character given is lower-case
if(Character.isLowerCase(theChar))
{
// give a the character a position number from the lower-case String
int position = lowerCase.indexOf(theChar);
// Adding the offSet to the position number
int newPosLower = position + theOffset;
// making sure that the alphabet is a continuous cycle.
if(newPosLower > 25)
{
// if the offSet moves it past 'z' then take 26 off newPosLower.
newPosLower -= 26;
}
else if(newPosLower < 0)
{
// if the offSet goes back past 'a' then add 26 to newPosLower.
newPosLower += 26;
}
encChar=lowerCase.charAt(newPosLower);
......@@ -45,12 +52,15 @@ public class AssessmentPartThree {
{
int position = upperCase.indexOf(theChar);
int newPosUpper = position + theOffset;
// making sure that the alphabet is a continuous cycle.
if(newPosUpper > 25)
{
// if the offSet moves it past 'Z' then take 26 off newPosLower.
newPosUpper -= 26;
}
else if(newPosUpper < 0)
{
// if the offSet goes back past 'A' then add 26 to newPosLower.
newPosUpper += 26;
}
encChar=upperCase.charAt(newPosUpper);
......@@ -58,6 +68,7 @@ public class AssessmentPartThree {
return encChar;
}
}
// if letter isn't a character then return original value
else
{
return encChar;
......@@ -69,13 +80,19 @@ public class AssessmentPartThree {
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
/* 06 - encryptedMessage
Complete this method so that it uses encryptedCharacter to
return the encrypted version of theMessage using theOffset
*/
String encMessage = "";
// turning theMessage into characters
char[] chars = theMessage.toCharArray();
// for loop to go through each char
for(int i = 0; i<chars.length; i++)
{
// using the method above to calculate each character separately
//then adding them to the String 'encMessage'
encMessage += enryptedCharacter(chars[i], theOffset);
}
return encMessage;
......
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