Please help with this assignement The purpose of this challenge is to get you to
ID: 3851968 • Letter: P
Question
Please help with this assignement
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!”
And this is what I have so far
The domain:
package jamesbondtester;
/**
*
* @author cristy
*/
public class PasswordEncryption
{
private String origWord;
private String encryptWord;
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()
{
char aLetter, newLetter;
int newLetterNum;
String spyMessage= "";
String scrambledMessage = "";
spyMessage = spyMessage.toLowerCase();
for (int i = 0; i < spyMessage.length(); ++i)
{
aLetter = spyMessage.charAt(i);
if (aLetter == 'x')
{
newLetter = 'a';
}
else if (aLetter == 'y')
{
newLetter = 'b';
}
else if (aLetter == 'z')
{
newLetter = 'c';
}
else
{
newLetterNum = ((int)aLetter) + 3;
newLetter = (char)newLetterNum;
}
scrambledMessage += newLetter;
}
System.out.println(toString());
//Add code to take the origWord and encrypt it, storing
//the encrypted word in encryptWord
}
public void decryptEncrypt()
{
char aLetter, newLetter;
int newLetterNum;
String spyMessage = "";
String scrambledMessage = "";
spyMessage = spyMessage.toLowerCase();
for (int i = 0; i < spyMessage.length(); ++i)
{
aLetter = spyMessage.charAt(i);
if (aLetter == 'a')
{
newLetter = 'x';
}
else if (aLetter == 'b')
{
newLetter = 'y';
}
else if (aLetter == 'c')
{
newLetter = 'z';
}
else
{
newLetterNum = ((int)aLetter) - 3;
newLetter = (char)newLetterNum;
}
scrambledMessage += newLetter;
}
System.out.println(toString());
//Add code to take the encryptWord and decrypt it, storing
//the original word in orgiWord
}
}
And the driver is:
package jamesbondtester;
import java.util.Scanner;
/**
*
* @author cristy
*/
public class JamesBondTester {
/**
* @param args the command line arguments
*/
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()
{
//Display the menu to show:
// 1. Encrypt a Password?
// 2. Decrypt a Password?
// 3. Stop Spying...
//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
Scanner keyboard = new Scanner(System.in);
int choice = 0;
while (true)
{
System.out.println("1. Encrypt a Password");
System.out.println("2. Decrypt a Password");
System.out.println("3. Stop spyring... END");
choice = keyboard.nextInt();
if (choice < 1 || choice > 3)
{
System.out.println("Invalid choice. Try again!!!");
}
else;
{
break;
}
}
return choice;
}
public static void encryptPassword()
{
//1. Ask user for for to encrypt
//2. Instantiate the PasswordEncryption object, passing
// it the word and a boolean value of true, meaning encrypt
//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!”
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter word to encrypt: ");
String word = keyboard.next();
PasswordEncryption passEnc = new PasswordEncryption(word, true);
passEnc.encryptOrig();
System.out.println("Try guessing the encrypted word: ");
String userGuess = keyboard.next();
if (userGuess.equals(passEnc.getEncryptedWord()))
{
System.out.println("Successfully encrypted... MISSION ACCOMPLISHED!!!");
}
else
{
System.out.println("Unsuccessfully encrypted... Danger, danger!!!");
System.out.println(passEnc.toString());
}
}
public static void decryptPassword()
{ //1. Ask user for encrypted word to decrypt
//2. Instantiate the PasswordEncryption object, passing
// it the word and a boolean value of false, meaning decrypt
//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!”
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter word to decrypt: ");
String word= keyboard.next();
PasswordEncryption passDec = new PasswordEncryption(word, false);
passDec.decryptEncrypt();
System.out.println("Try guessing the decrypted word: ");
String userGuess = keyboard.next();
if (userGuess.equals(passDec.getEncryptedWord()))
{
System.out.println("Successfully decrypted... MISSION ACCOMPLISHED!!!");
}
else
{
System.out.println("Unsuccessfully decrypted... Danger, danger!!!");
System.out.println(passDec.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 self-destruct in 5 seconds: ");
for (int i = 5; i > 0; i--)
{
System.out.println(i + " - ");
}
System.out.println("0 BOOM!");
}
}
And the output I'm getting is:
ant -f C:\Users\carvi\OneDrive\Documents\NetBeansProjects\JamesBondTester -Dnb.internal.action.name=run run
init:
Deleting: C:UserscarviOneDriveDocumentsNetBeansProjectsJamesBondTesteruilduilt-jar.properties
deps-jar:
Updating property file: C:UserscarviOneDriveDocumentsNetBeansProjectsJamesBondTesteruilduilt-jar.properties
compile:
run:
1. Encrypt a Password
2. Decrypt a Password
3. Stop spyring... END
1
Enter word to encrypt:
apple
Original Word: apple
Encrypted Word:
Try guessing the encrypted word:
dosse
Unsuccessfully encrypted... Danger, danger!!!
Original Word: apple
Encrypted Word:
1. Encrypt a Password
2. Decrypt a Password
3. Stop spyring... END
3
This program will self-destruct in 5 seconds:
5 -
4 -
3 -
2 -
1 -
0 BOOM!
BUILD SUCCESSFUL (total time: 15 seconds)
It won't display the encrypted word when I wan to encrypt not does it display when I want to decrypt
Please help me fix whatever mistake I have in my code, thank you.
Explanation / Answer
The fixed code is given below. The issue was that your were not starting with the correct message to be encrypted/decrypted and , also you need to store it back into the appropriate variables . Those variables are the one used by toString(). Only if you have set them currectly the output will be shown. Fixed those.
Here is the modified code with output. Please do rate the answer if it helped. Post a comment in case of any issues, I shall respond.
package jamesbondtester;
/**
*
* @author cristy
*/
public class PasswordEncryption
{
private String origWord;
private String encryptWord;
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 origWord and encrypt it, storing
//the encrypted word in encryptWord
char aLetter, newLetter;
int newLetterNum;
String spyMessage= origWord.toLowerCase(); //take the original word
String scrambledMessage = "";
for (int i = 0; i < spyMessage.length(); ++i)
{
aLetter = spyMessage.charAt(i);
if (aLetter == 'x')
{
newLetter = 'a';
}
else if (aLetter == 'y')
{
newLetter = 'b';
}
else if (aLetter == 'z')
{
newLetter = 'c';
}
else
{
newLetterNum = ((int)aLetter) + 3;
newLetter = (char)newLetterNum;
}
scrambledMessage += newLetter;
}
encryptWord = scrambledMessage; //store the result in encryptWord
System.out.println(toString());
}
public void decryptEncrypt()
{
//Add code to take the encryptWord and decrypt it, storing
//the original word in orgiWord
char aLetter, newLetter;
int newLetterNum;
String spyMessage = encryptWord; //start with the encryptword
String scrambledMessage = "";
spyMessage = spyMessage.toLowerCase();
for (int i = 0; i < spyMessage.length(); ++i)
{
aLetter = spyMessage.charAt(i);
if (aLetter == 'a')
{
newLetter = 'x';
}
else if (aLetter == 'b')
{
newLetter = 'y';
}
else if (aLetter == 'c')
{
newLetter = 'z';
}
else
{
newLetterNum = ((int)aLetter) - 3;
newLetter = (char)newLetterNum;
}
scrambledMessage += newLetter;
}
origWord = scrambledMessage; //store the result in origWord
System.out.println(toString());
}
}
package jamesbondtester;
import java.util.Scanner;
/**
*
* @author cristy
*/
public class JamesBondTester {
/**
* @param args the command line arguments
*/
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()
{
//Display the menu to show:
// 1. Encrypt a Password?
// 2. Decrypt a Password?
// 3. Stop Spying...
//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
Scanner keyboard = new Scanner(System.in);
int choice = 0;
while (true)
{
System.out.println("1. Encrypt a Password");
System.out.println("2. Decrypt a Password");
System.out.println("3. Stop spyring... END");
choice = keyboard.nextInt();
if (choice < 1 || choice > 3)
{
System.out.println("Invalid choice. Try again!!!");
}
else;
{
break;
}
}
return choice;
}
public static void encryptPassword()
{
//1. Ask user for for to encrypt
//2. Instantiate the PasswordEncryption object, passing
// it the word and a boolean value of true, meaning encrypt
//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!”
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter word to encrypt: ");
String word = keyboard.next();
PasswordEncryption passEnc = new PasswordEncryption(word, true);
System.out.println("Try guessing the encrypted word: ");
String userGuess = keyboard.next();
passEnc.encryptOrig();
if (userGuess.equals(passEnc.getEncryptedWord()))
{
System.out.println("Successfully encrypted... MISSION ACCOMPLISHED!!!");
}
else
{
System.out.println("Unsuccessfully encrypted... Danger, danger!!!");
}
}
public static void decryptPassword()
{ //1. Ask user for encrypted word to decrypt
//2. Instantiate the PasswordEncryption object, passing
// it the word and a boolean value of false, meaning decrypt
//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!”
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter word to decrypt: ");
String word= keyboard.next();
PasswordEncryption passDec = new PasswordEncryption(word, false);
System.out.println("Try guessing the decrypted word: ");
String userGuess = keyboard.next();
passDec.decryptEncrypt();
if (userGuess.equals(passDec.getEncryptedWord()))
{
System.out.println("Successfully decrypted... MISSION ACCOMPLISHED!!!");
}
else
{
System.out.println("Unsuccessfully decrypted... Danger, danger!!!");
}
}
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 self-destruct in 5 seconds: ");
for (int i = 5; i > 0; i--)
{
System.out.println(i + " - ");
}
System.out.println("0 BOOM!");
}
}
output
1. Encrypt a Password
2. Decrypt a Password
3. Stop spyring... END
1
Enter word to encrypt:
apple
Try guessing the encrypted word:
sample
Original Word: apple
Encrypted Word: dssoh
Unsuccessfully encrypted... Danger, danger!!!
1. Encrypt a Password
2. Decrypt a Password
3. Stop spyring... END
2
Enter word to decrypt:
dssoh
Try guessing the decrypted word:
mango
Original Word: apple
Encrypted Word: dssoh
Unsuccessfully decrypted... Danger, danger!!!
1. Encrypt a Password
2. Decrypt a Password
3. Stop spyring... END
3
This program will self-destruct in 5 seconds:
5 -
4 -
3 -
2 -
1 -
0 BOOM!