Commit 19664b29 authored by spray's avatar spray

changed to correct assesmment part

parent e56f0d16
import java.util.ArrayList;
public class AssessmentPartThree { public class AssessmentPartThree {
...@@ -10,23 +11,83 @@ public class AssessmentPartThree { ...@@ -10,23 +11,83 @@ public class AssessmentPartThree {
public char enryptedCharacter(char theChar, int theOffset) public char enryptedCharacter(char theChar, int theOffset)
{ {
// 05 - encryptedCharacter
// Complete this method so that it returns the encrypted character for ArrayList<Character> alphabet = new ArrayList<Character>(); //Create arraylist to store alphabet
// theChar when and offset of theOffset is used
// So if theChar='m' and theOffset is 3 the method will return 'p' alphabet.add('a'); //add a to end of list
// Lower case characters remain lower case, upper case remain upper case alphabet.add('b'); //add b to end of list
// Any other characters are returned unchanged alphabet.add('c'); //repeat for all of alphabet
alphabet.add('d');
return 'a'; alphabet.add('e');
alphabet.add('f');
alphabet.add('g');
alphabet.add('h');
alphabet.add('i');
alphabet.add('j');
alphabet.add('k');
alphabet.add('l');
alphabet.add('m');
alphabet.add('n');
alphabet.add('o');
alphabet.add('p');
alphabet.add('q');
alphabet.add('r');
alphabet.add('s');
alphabet.add('t');
alphabet.add('u');
alphabet.add('v');
alphabet.add('w');
alphabet.add('x');
alphabet.add('y');
alphabet.add('z');
Boolean upper = false; //create a boolean that will be used later
if(Character.isUpperCase(theChar) == true) { //if the letter needing encryption is upper case then..
upper = true; //set upper boolean to true
}
if(theChar == ' ') { //if the character is a space
return ' '; //return a space (to keep words separate)
}
if(Character.isDigit(theChar)) { //if the character is a number
return theChar; //return it as a number (so it doesn't encrypt numbers)
}
theChar = Character.toLowerCase(theChar); //convert all chars to lowercase before they get encrypted
int position = alphabet.indexOf(theChar); //store the array position of the char
int encrypted = position + theOffset; //the encrypted version of the letter is the current pos + the shift/offset
if(encrypted < 0) { //if new position is lower than 0 (off the array)
encrypted = encrypted + 26; //add 26 to it so it loops
}
if(encrypted > 26) { //if new position is higher than 26 (off the array)
encrypted = encrypted - 26; //take away 26 so it loops
}
char end = alphabet.get(encrypted); //use the index to return the character in that position
if(upper) { //if upper was true...
end = Character.toUpperCase(end); //put the encrypted character back to uppercase
}
return end; //return the encrypted letter.
} }
public String encryptedString(String theMessage, int theOffset) 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";
char[] letters = theMessage.toCharArray(); //Convert theMessage to a character Array
String encrypted = ""; //Create an empty string
for(int i = 0; i < letters.length; i++) { //for 0 to length of the char array -1
encrypted += enryptedCharacter(letters[i], theOffset); //apply encryptedCharacter function to each letter by the new offset
}
return encrypted; //
} }
} }
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