Question: #part A) must all be done with loops and strings only All the letters can be shifted by 3 simply by doing the following code
#part A) must all be done with loops and strings only
All the letters can be shifted by 3 simply by doing the following code snippet: String spyMessage = password ; String scrambledMessage = ; char letter, newLetter; int newLetterNum; Loop Pseudo Code: 0. For the sake of simplicity, please convert the spyMessage to all lower case letters. 1. Get the letter in spyMessage at index i until i < spyMessage.length. letter = spyMessage.charAt(i); //need to keep adding 1 to i, until the end of the message 2. Check to see if the letter is either x, y, or z, and if so, hard code its scrambled new value:
If (letter == x ) { newLetter = a; } else if (letter == y) { newLetter = b; } else if (letter == z) { newLetter = c; }
3. For all the other letters, convert the letter to an int, add 3 to it, then convert it back to a char: newLetterNum = ( (int) letter ) + 3; newLetter = (char) newLetterNum;
*The reason this works is because each letter has a positive integer assigned to it in this chart: http://www.asciitable.com/ 4. Regardless of how the newLetter was created, concatenate it to the scrambledMessage: scrambledMessage += newLetter; 5. Continue Looping until all the letters of the spyMessage are processed. 6. Display the original spyMessage and the scrambledMessage
7. Once the encryption algorithm works, think about how you would reverse it, to get the scrambled message back to the spyMessage.
What 3 letters would need special consideration, and have to be hardcoded to the decrypted value? What would the algorithm for the rest of the letters look like? See the ASCII chart here: http://www.asciitable.com/
So far I have.......
public String toString() { return "Original Word: " + origWord + " Encrypted Word:" + encryptWord; } public void encryptOrig() { //Add code to take the decryptWord and encrypt it, storing //the encrypted word in encryptWord String spyMessage = "password"; String scrambledMessage = ""; char letter, newLetter; int newLetterNum; letter = spyMessage.charAt('i'); newLetterNum = ((int) letter) +3 ; newLetter = (char) newLetterNum; scrambledMessage += newLetter; System.out.println(toString()); } public void decryptEncrypt() { //Add code to take the encryptWord and decrypt it, storing //the original word in orgiWord String spyMessage = "password"; String scrambledMessage = ""; char letter, newLetter; int newLetterNum; letter = spyMessage.charAt('i'); newLetterNum = ((int) letter) -3 ; newLetter = (char) newLetterNum; scrambledMessage += newLetter; System.out.println(toString()); } }
#part B)
instead of the algorithm provided above, for shifting letters, try this one:
public static char encrypt(char letter, int offset)
{
if (offset < 0) { // if the offset is negative, add 26 to it so that it will work with the modulus
offset = 26 + offset;
}
letter -= 97; // subtract 97 from the letter to ensure that it will be from 0-26
letter = (char) ((letter + offset) % 26); // offset the character and compensate for offsets > 25.
letter += 97; // adds 97 back to the letter so that it returns back to its original position, but offset
return letter; } To call the method:
encrypt(spyMessage.charAt(i), 3); // for a 3-character offset to the right
encrypt(spyMessage.charAt(i), -3); // for a 3-character offset to the left
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
