Commit 07fb80e7 authored by ash.brett's avatar ash.brett

Update AssessmentPartThree.java

parent 229b95af
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
//This section of the code creates a new instance of StringBuilder called encoded
//This section of the code creates a new instance of StringBuilder called encoded StringBuilder encoded = new StringBuilder();
StringBuilder encoded = new StringBuilder(); //This section of the code creates a new string which is equal the value of theChar
//This section of the code creates a new string which is equal the value of theChar String letter=String.valueOf(theChar);
String letter=String.valueOf(theChar);
//This section of the code creates a char called i and converts the string Letter to an array of characters
//This section of the code creates a char called i and converts the string Letter to an array of characters //i is the selected character of the array
//i is the selected character of the array for (char i : letter.toCharArray()) {
for (char i : letter.toCharArray()) { //this states that if the char i is a letter then it will run the following code
//this states that if the char i is a letter then it will run the following code if (Character.isLetter(i)) {
if (Character.isLetter(i)) { //this code checks to see if the character is uppercase and if it is it then proceeds to work out the new letter once this is done it adds it to the StringBuilder Encoded
//this code checks to see if the character is uppercase and if it is it then proceeds to workout the new letter once this is done it adds it to the stringBuilder Encoded if (Character.isUpperCase(i)) {
if (Character.isUpperCase(i)) { encoded.append((char) ('A' + (i - 'A' + theOffset) % 26 ));
encoded.append((char) ('A' + (i - 'A' + theOffset) % 26 )); //This code then runs if the character is a letter but isn't uppercase therefore meaning it must be lower case, it proceeds to work out the new letter and then adds it to the StringBuilder Encoded
//This code then runs if the character is a letter but isnt uppercase therefore meaning it must be lower case, it proceds to work out the new letter and then adds it to the stringBuilder Encoded } else {
} else { encoded.append((char) ('a' + (i - 'a' + theOffset) % 26 ));
encoded.append((char) ('a' + (i - 'a' + theOffset) % 26 )); }
} //If i isn't a letter then it will run this code
//If i isnt a letter then it will run this code } else {
} else { //this adds the char i to the StringBuilder Encoded
//this adds the char i to the stringbuilder Encoded encoded.append(i);
encoded.append(i); }
}
}
} //This section creates a char called Finished and sets it to equal the first value of the StringBuilder encoded
//This section creates a char called Finished and sets it to equal the first value of the stringbuilder encoded char Finished = encoded.charAt(0);
char Finished = encoded.charAt(0); //It then returns the char Finished
//It then returns the char Finished return Finished;
return Finished;
}
}
public String encryptedString(String theMessage, int theOffset)
public String encryptedString(String theMessage, int theOffset) {
{ // 06 - encryptedMessage
// 06 - encryptedMessage // Complete this method so that it uses encryptedCharacter to
// Complete this method so that it uses encryptedCharacter to // return the encrypted version of theMessage using theOffset
// return the encrypted version of theMessage using theOffset
//This section of the code creates a new instance of StringBuilder called encoded
//This section of the code creates a new instance of StringBuilder called encoded StringBuilder encoded = new StringBuilder();
StringBuilder encoded = new StringBuilder(); //The section of the code creates the int i and sets its value to 0. And then says for each time i is less than the length of the String theMessage run the code below and increment i by one
//The section of the code creates the int i and sets it value to 0. and then says for each time i is less than length of the String theMessage run the code below and increment i by one for (int i=0; i < theMessage.length(); i++) {
for (int i=0; i < theMessage.length(); i++) { //this sets the int theOffset to be equal to theOffset divided by 26 plus 26
//this sets the int theOffset to be equal to theOffset divided by 26 plus 26 theOffset = theOffset % 26 + 26;
theOffset = theOffset % 26 + 26; //this creates a char called letter and sets it to be equal to theMessage character at i
//this creates a char called letter and sets it to be equal to theMessage character at i //i is used to work out the char of the message
//i is used to work out the char of the message //for example if the value of i is 2 then it will look for the character at 2 in the message
//for example if the value of i is 2 then it will look for the character at 2 in the message char letter = theMessage.charAt(i);
char letter = theMessage.charAt(i); //This creates a new char called result and sets it to be equal to the result of the method enryptedCharacter
//This creates a new char called result and sets it to be equal to the result of the method enryptedCharacter //This section of code takes in the char letter and theOffset in order to run the method enryptedCharacter using these values
//This section of code takes in the char letter and theOffset in order to run the method enryptedCharacter using these values char Result = this.enryptedCharacter(letter, theOffset);
char Result = this.enryptedCharacter(letter, theOffset); //This section of code takes in the char Result and adds it to the StringBuilder encoded
//This section of code takes in the char Result and adds it to the stringBuilder encoded encoded.append(Result);
encoded.append(Result); }
} //This section of the code returns the value of the StringBuilder encoded in a string
//This section of the code returns the value of the stringBuilder encoded in a string return encoded.toString();
return encoded.toString();
}
} }
}
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