Commit fae474be authored by benn.robinson's avatar benn.robinson

Update AssessmentPartThree.java

parent 7a54ec0d
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[] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; // creates an array which has each letter of the alphabet
int letterPlace = 0; //creates an int and the value is 0
theChar = Character.toLowerCase(theChar); //makes theChar equal to the lowercase value of theChar
boolean upperCase = Character.isUpperCase(theChar); // makes it equal to the uppercase value of theChar
if (Character.isLetter(theChar))
{
if (theOffset < 0)
{
theOffset = theOffset + 26;
}
if (theOffset > alphabet.length) // if the value of theOfset is greater than the length of the string alphabet
{
theOffset = theOffset - 26;
}
else
{
theChar = alphabet[theOffset];
}
}
else
{
return theChar;
}
for (int i = 0 ; i < alphabet.length; i++) // creates a 'for' loop that runs as long as 'i' is less than the length of the array string alphabet
{
if (theChar == alphabet[i])
{
theOffset = theOffset + i;
}
}
if (upperCase)
{
theChar = Character.toUpperCase(theChar); //makes thechar equal to the uppercase value of thechar
}
return theChar;
}
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
String output = "";
for (char letter : theMessage.toCharArray()) //creates a 'for' loop that iterates the char "letter" through theMessage.tochararray
{
output = output + letter;
letter = enryptedCharacter( letter, theOffset); // makes letter equal to the amount which has been obtained while running theMessgae through the encrypted character method above
}
return output;
}
}
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