Creating a class that encrypts sentences
Hey guys again! I have a new problem, but hopefully this one is pretty straightforward. Here's what the textbooks states:
You have just been hired by the CIA as a programmer in the encryption department. Your job is
to write a class called Crypto. One method, encrypt, will accept a String that represents the
sentence to be encrypted. It will return a String that is the sentence with all v’s (big or small)
replaced with “ag’,r”, all m’s (big or small) with “ssad”, all g’s (big or small) with “jeb..w”, and
all b’s (big or small) with “dug>?/”.
The class contains another method, decrypt, that accepts a String that represents the sentence to
be decrypted. In this method the reverse process described above is performed. It returns a String
that is the original sentence before encryption.
Use the following Tester class to insure that your methods work.
Code:
import java.io.*;
import java.util.*;
public class Tester
{
public static void main(String args[])
{
Scanner kbReader = new Scanner(System.in);
System.out.print(“Enter a sentence that is to be encrypted: ”);
String sntnc = kbReader.nextLine( );
System.out.println(“Original sentence = ” + sntnc);
Crypto myCryptObj = new Crypto( );
String encryptdSntnc = myCryptObj.encrypt(sntnc);
System.out.println(“Encrypted sentence = ” + encryptdSntnc);
String decryptdSntnc = myCryptObj.decrypt(encryptdSntnc);
System.out.println(“Decrypted sentence = ” + decryptdSntnc);
}
}
Test with this sentence: “This is a very big morning.” After running your program, your screen
should appear as follows:
Enter a sentence that is to be encrypted: This is a very big morning.
Original sentence = This is a very big morning.
Encrypted sentence = This is a ag',rery dug>?/ijeb..w ssadorninjeb..w.
Decrypted sentence = This is a very big morning.
Now, I've created a class that is supposed to encrypt/decrypt but it is doing something weird. Please give tip in the right direction. My code/output is below:
My code:
Code:
public class Crypto
{
public Crypto()
{
String salsa = ajd;
}
public String encrypt(String sntnc) {
ajd = sntnc;
sntnc = sntnc.replace("v", "ag\',r");
sntnc = sntnc.replace("V", "ag\',r");
sntnc = sntnc.replace("M", "ssad");
sntnc = sntnc.replace("m", "ssad");
sntnc = sntnc.replace("g", "jeb..w");
sntnc = sntnc.replace("G", "jeb..w");
sntnc = sntnc.replace("B", "dug>?\\");
sntnc = sntnc.replace("b", "dug>?\\");
return sntnc;
}
public String decrypt(String sntnc) {
sntnc = sntnc.replace("ag\',r", "v");
sntnc = sntnc.replace("ssad", "m");
sntnc = sntnc.replace("jeb..w", "g");
sntnc = sntnc.replace("dug>?\\", "b");
return sntnc;
}
public String ajd;
}
The output is the following:
Enter a sentence that is to be encrypted: This is a very big morning.
Original sentence = This is a very big morning.
Encrypted sentence = This is a ajedug>?\..w',rery dug>?\ijedug>?\..w ssadorninjedug>?\..w.
Decrypted sentence = This is a ajeb..w',rery bijeb..w morninjeb..w.
Re: Creating a class that encrypts sentences
You will need to reverse the order of your decrypt statements as they have to be in the opposite order as the encryption statements. Now think on this a bit and see if you can tell me why you need to do this.
As an aside: next time, please only post formatted code. Please know that it is hard work to read and understand someone else's code. As we are all volunteers trying to help you for free, we appreciate any and all effort that you expend in not trying to make our work any more difficult than it already is.
Re: Creating a class that encrypts sentences
Quote:
Originally Posted by
Fubarable
As an aside: next time, please only post formatted code. Please know that it is hard work to read and understand someone else's code. As we are all volunteers trying to help you for free, we appreciate any and all effort that you expend in not trying to make our work any more difficult than it already is.
I'm guessing you mean with indents and comments that indicate where the program does something? :(think):
I have another question: What can I do to stop the program from replacing letters in the encrypt statements?
Enter a sentence that is to be encrypted: This is a very big morning.
Original sentence = This is a very big morning.
Encrypted sentence = This is a ag',rery dug>?\ijedug>?\..w ssadorninjedug>?\..w.
Decrypted sentence = This is a very big morning.
In that statement \ijedug>?\..w It should instead be dug>?/ijeb..w
How do I make the program replace the strings but still contain those specific character?
Re: Creating a class that encrypts sentences
in public String encrypt(String sntnc) {
ajd = sntnc; <============ what is ajd? what is it doing?
Re: Creating a class that encrypts sentences
It is connecting the method's parameter to the static variable.