Commit 497376d1 authored by Invate's avatar Invate

first

parent e56f0d16
public class AssessmentPartThree { public class AssessmentPartThree {
// The simplest form of encryption is the rotation cipher (also known as Caeser's Cipher) // 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 // An offset value is chosen to encrypt a message. Each letter is replaced by the
// letter that that number of places away from it. // 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 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 // - 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 char enryptedCharacter(char theChar, int theOffset)
{ {
// 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
// theChar when and offset of theOffset is used // theChar when and offset of theOffset is used
// So if theChar='m' and theOffset is 3 the method will return 'p' // So if theChar='m' and theOffset is 3 the method will return 'p'
// Lower case characters remain lower case, upper case remain upper case // Lower case characters remain lower case, upper case remain upper case
// Any other characters are returned unchanged // Any other characters are returned unchanged
return 'a'; int ascii = (int)theChar;//character turned into ascii value to shift by theOffset easily
}
char character = 'a';//declaring the return variable
public String encryptedString(String theMessage, int theOffset)
{ if((ascii>64 && ascii<92) || (96< ascii && ascii < 123))//if the ascii value is withing the letter ranges then it is shifted
// 06 - encryptedMessage {
// Complete this method so that it uses encryptedCharacter to
// return the encrypted version of theMessage using theOffset character = (char) (ascii+theOffset); //applies the shift and turns the ascii value back into a char
return "Encryptred message"; if((ascii+theOffset) > 122)
} {
character = (char)((ascii+theOffset)-26); //if the letter is shifted past z it loops back around to a
}
else if((ascii+theOffset) >90 && (ascii+theOffset)<97)
{
character = (char)((ascii+theOffset)+26); //if the character is shifted below a then it loops around to z
}
else if((ascii+theOffset) <65)
{
character = (char)((ascii+theOffset)+26);//if the character is shifted below upper case A then it loops around to Z
}
else if((ascii+theOffset) <97 && (ascii+theOffset)>90)
{
character = (char)((ascii+theOffset)-26);//if the character is shifted past upper case Z then it loops around to A
}
}
else
{
character = (char)ascii;// if the character is not a letter it remains the same;
}
return character;//returns character after shift
}
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[] message = theMessage.toCharArray();//creates a char array
for(int i =0;i<theMessage.length();i++)
{
message[i] = enryptedCharacter(message[i],theOffset);//steps through the array and shifts the characters by theOffset
}
String encryptedMessage = "";//declaring return string
for(int j=0;j<message.length;j++)
{
encryptedMessage = encryptedMessage + message[j];//steps through the char array a final time and concatenates the elements into a string
}
return encryptedMessage;
}
} }
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