Results 1 to 5 of 5
  1. #1
    bubblegum is offline Member
    Join Date
    Sep 2012
    Posts
    19
    Rep Power
    0

    Default 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.
    Java 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:
    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; 
    }
    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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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.

  3. #3
    bubblegum is offline Member
    Join Date
    Sep 2012
    Posts
    19
    Rep Power
    0

    Default Re: Creating a class that encrypts sentences

    Quote Originally Posted by Fubarable View Post
    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?

    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?

  4. #4
    Amok is offline Member
    Join Date
    Sep 2012
    Location
    Australia
    Posts
    12
    Rep Power
    0

    Default Re: Creating a class that encrypts sentences

    in public String encrypt(String sntnc) {
    ajd = sntnc; <============ what is ajd? what is it doing?

  5. #5
    bubblegum is offline Member
    Join Date
    Sep 2012
    Posts
    19
    Rep Power
    0

    Default Re: Creating a class that encrypts sentences

    It is connecting the method's parameter to the static variable.

Similar Threads

  1. how to extract sentences from a txt book?
    By Kosala in forum Advanced Java
    Replies: 4
    Last Post: 01-15-2012, 07:07 PM
  2. Replies: 9
    Last Post: 07-29-2011, 02:18 PM
  3. How to read a particular parf of an sentences ???
    By qwerty53 in forum New To Java
    Replies: 3
    Last Post: 07-29-2011, 10:00 AM
  4. Creating random sentences
    By bluekswing in forum New To Java
    Replies: 4
    Last Post: 06-27-2007, 05:45 PM
  5. vars and if sentences in XSL-FO
    By Alan in forum XML
    Replies: 1
    Last Post: 05-31-2007, 02:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •