Results 1 to 5 of 5
- 09-26-2012, 03:57 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 19
- Rep Power
- 0
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.
Test with this sentence: “This is a very big morning.” After running your program, your screenJava 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); } }
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:
The output is the following:Java 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; }
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.
- 09-29-2012, 10:26 PM #3
Member
- Join Date
- Sep 2012
- Posts
- 19
- Rep Power
- 0
Re: Creating a class that encrypts sentences
I'm guessing you mean with indents and comments that indicate where the program does something?
.gif)
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?
- 09-30-2012, 02:16 PM #4
Member
- Join Date
- Sep 2012
- Location
- Australia
- Posts
- 12
- Rep Power
- 0
Re: Creating a class that encrypts sentences
in public String encrypt(String sntnc) {
ajd = sntnc; <============ what is ajd? what is it doing?
- 10-01-2012, 05:14 PM #5
Member
- Join Date
- Sep 2012
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
how to extract sentences from a txt book?
By Kosala in forum Advanced JavaReplies: 4Last Post: 01-15-2012, 07:07 PM -
Creating and implementing class for creating a calendar object
By kumalh in forum New To JavaReplies: 9Last Post: 07-29-2011, 02:18 PM -
How to read a particular parf of an sentences ???
By qwerty53 in forum New To JavaReplies: 3Last Post: 07-29-2011, 10:00 AM -
Creating random sentences
By bluekswing in forum New To JavaReplies: 4Last Post: 06-27-2007, 05:45 PM -
vars and if sentences in XSL-FO
By Alan in forum XMLReplies: 1Last Post: 05-31-2007, 02:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks