Question: I need so much help I'm confused with this assignment I thought I was done but I still keep getting errors when I run it

I need so much help I'm confused with this assignment I thought I was done but I still keep getting errors when I run it the program must be made with loops and strings only and with a toString method. When I try to input words to be encrypted or decrypted I keep getting errors

The purpose of this challenge is to get you to see how useful your String manipulation skills can be, especially if you were James Bond trying to crack a message being sent by a spy from Russia

Encryption is the process of encoding messages to keep them secret, and decryption is the process of reversing encryption and putting the encrypted message back to the original text. Did you know that the Caesar Cipher is one of the first techniques ever used to perform encryption? It shifts the alphabet by a certain number of letters, so the original message appears encrypted.

You are James Bond, Java programmer, and counter-spy. Your mission, should you choose to accept it, is to encrypt the password that the spy types using the Caesar Cipher, shifting all the letters over by 3 characters. The following chart depicts the pattern:

a ===> d b ===> e c ===> f etc.,. Notice that at the end of the alphabet, the last 3 letters wrap around to the beginning of the alphabet:

x ===> a y === > b z === > c 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/

The Challenge

Loop to display a menu that will ask the user to choose 1 of 3 options:

1. Encryption Enter a password to see what it looks like scrambled. 2. Decryption Enter an encrypted word to see what it looks like unscrambled. 3. End Spy Mission When the user enters option 1, ask the user for the word to encrypt. Only use letters a z, then, display the encrypted message to the user. Note: passwords dont have spaces. When the user enters option 2, ask the user for the encrypted word to decrypt. Again, only use letters a z. Then, display the decrypted word to the user.

When the user enters option 3, advise the user to that this program will self-destruct in seconds, and show 5 4 3- 2- 1- Boom!

For extra credit, after the user selects the option 1 or 2, and after the user enters a word to encrypt or decrypt, ask the user to guess what the encrypted or decrypted message would be. Compare the users guess to the encrypted or decrypted message, and tell the user if he/she guessed correctly or not. If the user guessed correctly, display a message stating: Successfully encryptedmission accomplished! If the user guessed incorrectly, display a message stating Unsuccessfully encrypted...Danger, danger!

On a seperated method, 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

SO FAR I HAVE.......

public static void main(String[] args) { //Add a do-while loop that keeps looping while the user //enters either 1 or 2. int menuOption = displayMenu(); do{ switch(menuOption) { case 1: encryptPassword(); break; case 2: decryptPassword(); break; case 3: selfDestruct(); break; } } while(menuOption != 3); } public static int displayMenu() { Scanner key = new Scanner(System.in);

//Display the menu to show: // 1. Encrypt a Password? // 2. Decrypt a Password? // 3. Stop Spying...END int choice = 0; while(true) { System.out.println("1. Encrypt a Password"); System.out.println("2. Decrypt a Password"); System.out.print("3. Stop Spying...END: "); //Get input from user and store in a local variable //loop to validate value entered as 1, 2, or 3 //and if not, keep looping and asking user to enter valid value //return value entered choice = key.nextInt(); if(choice < 1 || choice > 3) { System.out.println("Invalid choice, Try again!!"); } else { break; } } return choice; } public static void encryptPassword() { Scanner key = new Scanner(System.in); //1. Ask user for for to encrypt System.out.print("Enter word to encrypt: "); String word = key.next(); //2. Instantiate the PasswordEncryption object, passing // it the word and a boolean value of true, meaning encrypt PasswordEncryption pwdEnc = new PasswordEncryption(word, true); pwdEnc.encryptOrig(); //3. Display the encrypted word to the user. //4. Extra Credit: Before displaying encrypted word, // ask user to guess the encrypted word. If user guesses // correctly, state "Successfully encryptedmission accomplished" // If user did not guess correctly, state "Unsuccessfully encrypted...Danger, danger! System.out.print("Try guessing the incrypted word: "); String userguess = key.next(); String encryptedWord = pwdEnc.getEncryptedWord(); if(userguess.equals(encryptedWord)) { System.out.println("Successfully encryptedmission accomplished"); } else { System.out.println("Unsuccessfully encrypted...Danger, danger!"); System.out.println("Correct word is: " + encryptedWord); } } public static void decryptPassword() { Scanner key = new Scanner(System.in); //1. Ask user for encrypted word to decrypt System.out.print("Enter word to decrypt: "); String word = key.next(); //2. Instantiate the PasswordEncryption object, passing // it the word and a boolean value of false, meaning decrypt PasswordEncryption pwdEnc = new PasswordEncryption(word, false); pwdEnc.decryptEncrypt(); //3. Display the decrypted word to the user. //4. Extra Credit: Before displaying decrypted word, // ask user to guess the decrypted word. If user guesses // correctly, state "Successfully decryptedmission accomplished" // If user did not guess correctly, state "Unsuccessfully decrypted...Danger, danger! System.out.print("Try guessing the decrypted word: "); String userguess = key.next(); String decruptedWord = pwdEnc.getOrigWord(); if(userguess.equals(decruptedWord)) { System.out.println("Successfully decryptedmission accomplished"); } else { System.out.println("Unsuccessfully decrypted...Danger, danger!"); System.out.println("Correct word is: " + decruptedWord); } } public static void selfDestruct() { //Advise the user that this program will self-destruct in 5 seconds //5 - 4 - 3 - 2 - 1 - 0 Boom! System.out.println(" This program will deconstruct in 5 seconds: "); for(int i=5; i > 0; i--) { System.out.print(i+" - "); } System.out.println("0 Boom!");

} }

public class PasswordEncryption { private String origWord; private String encryptWord; String spyMessage = "password" ; String scrambledMessage = ""; char letter, newLetter; int newLetterNum; String decryptedMessage = "";

public PasswordEncryption(String word, boolean encrypt) { if (encrypt) { this.origWord = word; this.encryptWord = ""; } else { this.encryptWord = word; this.origWord = ""; } }

public String getOrigWord() { return origWord; }

public void setOrigWord(String origWord) { this.origWord = origWord; }

public String getEncryptedWord() { return encryptWord; }

public void setEncryptedWord(String encryptWord) { this.encryptWord = encryptWord; } 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 for(int i=0; i

} public void decryptEncrypt() { //Add code to take the encryptWord and decrypt it, storing //the original word in orgiWord for(int i=0; i

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!