Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need so much help I\'m confused with this assignment I thought I was done but

ID: 3851405 • Letter: I

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 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 don’t 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 user’s 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 encrypted…mission 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 encrypted…mission 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 encrypted…mission 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 decrypted…mission 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 decrypted…mission 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<spyMessage.length(); i++)
{
letter = spyMessage.charAt(i);
if (letter == 'x' )
{
newLetter = 'a';
}
else if (letter == 'y')
{
newLetter = 'b';
}
else if (letter == 'z')
{
newLetter = 'c';
}
  
else
{
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

for(int i=0; i<spyMessage.length(); i++)
{
letter = spyMessage.charAt(i);
if (letter == 'x' )
{
newLetter = 'a';
}
else if (letter == 'y')
{
newLetter = 'b';
}
else if (letter == 'z')
{
newLetter = 'c';
}
  
else
{
newLetterNum = ((int) letter) -3 ;
newLetter = (char) newLetterNum;
}
decryptedMessage += newLetter;
System.out.println(toString());
}
}
}

Explanation / Answer

========== UPDATE START ==============

Note: Please rename MissionSpy to whatever what your original class name.

import java.util.Scanner;

public class MissionSpy

{

   public static void main(String[] args)

   {

   //Add a do-while loop that keeps looping while the user

   //enters either 1 or 2.

  

   int menuOption ;

  

   do{

       menuOption = displayMenu();

   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 encrypted…mission accomplished"

   // If user did not guess correctly, state "Unsuccessfully encrypted...Danger, danger!”

   System.out.print("Try guessing the incrypted word: ");

   String userguess = key.next();

  

   if(userguess.equals(pwdEnc.getEncryptedWord())) {

   System.out.println("Successfully encrypted…mission accomplished");

   }

   else

   {

   System.out.println("Unsuccessfully encrypted...Danger, danger!");

   System.out.println(pwdEnc.toString());

   }

  

   }

  

   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 decrypted…mission accomplished"

   // If user did not guess correctly, state "Unsuccessfully decrypted...Danger, danger!”

   System.out.print("Try guessing the decrypted word: ");

   String userguess = key.next();

  

   if(userguess.equals(pwdEnc.getOrigWord()))

   {

   System.out.println("Successfully decrypted…mission accomplished");

   }

   else

   {

   System.out.println("Unsuccessfully decrypted...Danger, danger!");

   System.out.println(pwdEnc.toString());

   }

  

   }

  

   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

       String spyMessage = origWord;

       for (int i = 0; i < spyMessage.length(); i++) {

           letter = spyMessage.charAt(i);

           if (letter == 'x') {

               newLetter = 'a';

           } else if (letter == 'y') {

               newLetter = 'b';

           } else if (letter == 'z') {

               newLetter = 'c';

           }

           else {

               newLetterNum = ((int) letter) + 3;

               newLetter = (char) newLetterNum;

           }

           scrambledMessage += newLetter;

       }

       encryptWord = scrambledMessage;

   }

   public void decryptEncrypt() {

       // Add code to take the encryptWord and decrypt it, storing

       // the original word in orgiWord

       spyMessage = encryptWord;

       for (int i = 0; i < spyMessage.length(); i++) {

           letter = spyMessage.charAt(i);

           if (letter == 'x') {

               newLetter = 'a';

           } else if (letter == 'y') {

               newLetter = 'b';

           } else if (letter == 'z') {

               newLetter = 'c';

           }

           else {

               newLetterNum = ((int) letter) - 3;

               newLetter = (char) newLetterNum;

           }

           decryptedMessage += newLetter;

          

       }

       origWord = decryptedMessage;

   }

}

========== UPDATE END ==============

Here is the needed code for the question. I have eliminated the PasswordEncryption class. We don't need to store any member variables. A cleaner design is to just have a utility class with helper methods to encrypt and decrypt. I have reused your code for encrypt in the utility class though. For decrypt we need to reverse the logic , i.e. replace a by x , b by y and c by z and then for other letters subtract 3.

So for the 1st part of the assignment, 2 classes are required - MissionSpy and EncryptDecryptUtil

I named your class with main as MissionSpy since I did not find its name in the question. That part of code was missing.

For 2nd part , an alternate algorithm is for encrypt/decrypt is given at hte end of the question using offset values. I have written a Demo class on how to use that function to do encryption and decryption. Please take a look at Demo2.java for that.

Ouputs for both parts are shown....

Please post a comment in case of any doubts / issues, I shall respond to it. If you are happy with the answer, request you to give it a thumbs up ! Thank you very much.

MissionSpy.java

import java.util.Scanner;

public class MissionSpy

{

public static void main(String[] args)

{

//Add a do-while loop that keeps looping while the user

//enters either 1 or 2.

   int menuOption;

   do{

       menuOption = displayMenu();

  

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.println("3. Stop Spying...END: ");

System.out.print(" Enter your choice: ");

//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. Use the utility class to encrypt the word

      String encryptedWord = EncryptDecryptUtil.encrypt(word);

      //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 encrypted…mission accomplished"

      // If user did not guess correctly, state "Unsuccessfully encrypted...Danger, danger!”

      System.out.print("Try guessing the incrypted word: ");

      String userguess = key.next();

     

      if(userguess.equals(encryptedWord)) {

         System.out.println("Successfully encrypted…mission 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. Use the utility to decrypt the word

String decryptedWord = EncryptDecryptUtil.decrypt(word);

//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 decrypted…mission accomplished"

// If user did not guess correctly, state "Unsuccessfully decrypted...Danger, danger!”

System.out.print("Try guessing the decrypted word: ");

String userguess = key.next();

if(userguess.equals(decryptedWord))

{

   System.out.println("Successfully decrypted…mission accomplished");

}

else

{

      System.out.println("Unsuccessfully decrypted...Danger, danger!");

      System.out.println("Correct word is: " + decryptedWord);

}

  

}

  

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!");

}

  

}

EncryptDecryptUtil.java

public class EncryptDecryptUtil {

  

   public static String encrypt(String spyMessage)

   {

       char letter, newLetter;

       int newLetterNum;

       String scrambledMessage = "";

       for(int i=0; i<spyMessage.length(); i++)

      {

   letter = spyMessage.charAt(i);

   if (letter == 'x' )

   {

       newLetter = 'a';

   }

   else if (letter == 'y')

   {

       newLetter = 'b';

   }

   else if (letter == 'z')

   {

       newLetter = 'c';

   }

  

   else

   {

       newLetterNum = ((int) letter) +3 ;

       newLetter = (char) newLetterNum;

   }

   scrambledMessage += newLetter;

   }

      

       return scrambledMessage;

   }

  

   public static String decrypt(String spyMessage)

   {

       char letter, newLetter;

       int newLetterNum;

       String scrambledMessage = "";

       for(int i=0; i<spyMessage.length(); i++)

      {

   letter = spyMessage.charAt(i);

   if (letter == 'a' )

   {

       newLetter = 'x';

   }

   else if (letter == 'b')

   {

       newLetter = 'y';

   }

   else if (letter == 'c')

   {

       newLetter = 'z';

   }

  

   else

   {

       newLetterNum = ((int) letter) - 3 ;

       newLetter = (char) newLetterNum;

   }

   scrambledMessage += newLetter;

   }

      

       return scrambledMessage;

   }   

}

output

1. Encrypt a Password

2. Decrypt a Password

3. Stop Spying...END:

Enter your choice: 1

Enter word to encrypt: apple

Try guessing the incrypted word: ddewd

Unsuccessfully encrypted...Danger, danger!

Correct word is: dssoh

1. Encrypt a Password

2. Decrypt a Password

3. Stop Spying...END:

Enter your choice: 2

Enter word to decrypt: dssoh

Try guessing the decrypted word: mango

Unsuccessfully decrypted...Danger, danger!

Correct word is: apple

1. Encrypt a Password

2. Decrypt a Password

3. Stop Spying...END:

Enter your choice: 3

This program will deconstruct in 5 seconds:

5 - 4 - 3 - 2 - 1 - 0 Boom!

Demo2.java

public class Demo2 {

       //apart from the main algorithm, try this encrypt / decrypt using offset

       //for encrypting a message , loop through the charactesr and call this function with

       //offset of +3 and to decrypt a message, loop through and call this method with offset

       //of -3

       private 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;

       }

      

       public static void main(String[] args)

       {

           String word = "apple";

           String encrypted = "";

           for(int i = 0; i < word.length(); i++)

           {

               encrypted += encrypt(word.charAt(i), 3);

           }

          

           System.out.println(word + " is encrypted to " + encrypted);

          

           String decrypted = "";

           for(int i = 0; i < encrypted.length(); i++)

           {

               decrypted += encrypt(encrypted.charAt(i), -3);

           }

          

           System.out.println(encrypted + " is decrypted to " + decrypted);

          

       }

}

output

apple is encrypted to dssoh

dssoh is decrypted to apple