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

Update AssessmentPartThree.java

parent e56f0d16
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
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
return 'a';
}
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
return "Encryptred message";
}
}
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
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
char Encrypted = 0;
if (Character.isDigit(theChar)) { // checks to see if a character of theChar is a number
return theChar; // if it isn't fully made of letters it returns the original value of theChar
} else { // if it was fully letters it goes to this
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)
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