Commit 61762793 authored by william.hill1's avatar william.hill1

Update AssessmentPartThree.java

parent e56f0d16
public class AssessmentPartThree {
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 // The simplest form of encryption is the rotation cipher (also known as
// letter that that number of places away from it. // Caeser's Cipher)
// So if an offset value of 1 is chosen, each letter is replaced by the one after it // An offset value is chosen to encrypt a message. Each letter is replaced by
// - so a becomes b, b becomes c, etc // the
// If a value of -2 is chosen a becomes y, b becomes z and so on // 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
public char enryptedCharacter(char theChar, int theOffset) // after it
{ // - so a becomes b, b becomes c, etc
// 05 - encryptedCharacter // If a value of -2 is chosen a becomes y, b becomes z and so on
// Complete this method so that it returns the encrypted character for
// theChar when and offset of theOffset is used public char enryptedCharacter(char theChar, int theOffset) {
// So if theChar='m' and theOffset is 3 the method will return 'p' // 05 - encryptedCharacter
// Lower case characters remain lower case, upper case remain upper case // Complete this method so that it returns the encrypted character for
// Any other characters are returned unchanged // theChar when and offset of theOffset is used
// So if theChar='m' and theOffset is 3 the method will return 'p'
return 'a'; // Lower case characters remain lower case, upper case remain upper case
} // Any other characters are returned unchanged
char Encrypted = 0;
public String encryptedString(String theMessage, int theOffset)
{ if (Character.isDigit(theChar)) { // checks to see if a character of theChar is a number
// 06 - encryptedMessage return theChar; // if it isn't fully made of letters it returns the original value of theChar
// Complete this method so that it uses encryptedCharacter to } else { // if it was fully letters it goes to this
// return the encrypted version of theMessage using theOffset char Value = (char) (theChar + theOffset); // adds theChar value and the offset and sets this as a variable
if (Value > 'z') // if the value of "Value" is more then the ASCII value of 'z'(122)
return "Encryptred message"; Encrypted += (char) (Value) - (26 - theOffset); // if the value is above the max it -26 to get it back
} // to 'a'
// and goes up from there.
} else // if test isn't bigger then 'z'
Encrypted += (char) (Value); // it will just add the offset since the values are in range
}
return Encrypted;
}
public String encryptedString(String theMessage, int theOffset) {
StringBuilder Encrypted = new StringBuilder(); // builds the string which will be returned at the end
for (char i : theMessage.toCharArray()) { //sets the value of i to the values in theMessage, in char form
if (Character.isLetter(i)) { //chescks to see if the character is a letter
if (Character.isUpperCase(i)) { //checks to see if the character is an uppercase letter
Encrypted.append((char) ('A' + (i - 'A' + theOffset % 26 + 26) % 26 )); // if it is an upercase letter it will change the character like this
} else { // if its not uppercase
Encrypted.append((char) ('a' + (i - 'a' + theOffset % 26 + 26) % 26 )); //if it is an lowercase letter it will change the character like this
}
} else { //if the character isnt a letter it will leave it unchanged
Encrypted.append(i);
}
}
return Encrypted.toString(); //returns the Array in a stringform
}
}
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