简体   繁体   中英

Decrypt encrypted text in java

i have an assignment. i have done all the parts of the assignment. but i am strucked in below part of my assignment. can you help me

Assignment guidelines

III.Write one constructor method for the EncryptedNode class. This constructor should take a String as an argument. Unlike other constructors you have written, this constructor should function conditionally. If the message is one character long, simply assign that character to the letter instance variable. If the message is two characters long, assign the first character to the letter, and the other character as a string to the right EncryptedNode. In any other case, find the middle value of the input String, assign the character at the first index to letter, characters

from 1 up to the middle character should go to the right EncryptedNode, and the remaining unclaimed

characters should go the left EncryptedNode. Note: you will be instantiating new EncryptedNode objects from this constructor, thus creating a recursive construction.

IV. Write the following methods:

a. The decrypt method takes no arguments and returns a String. This method should re-build the original unencrypted String by adding the letter, left and right elements of each EncryptedNode recursively. This process will be left to you to solve.

my sample code is

class EncryptedNode {

public EncryptedNode left, right ;
public char letter ;


// EncryptedNode Class constructor method.
public EncryptedNode (String message) {

    // get String length
    int message_length = message.length() ;

    if (message_length == 1) {
        this.letter = message.charAt(0) ;
    }else if (message_length == 2) {
        this.letter = message.charAt(0) ;
        this.right = new EncryptedNode (message.substring(1));
    } else {
        this.letter = message.charAt(0) ;

        // get the middle index of the message string.
        int middle_index = message.substring(1).length() /2 ;

        // get left and right strings
        String rightStr = message.substring(1,middle_index+1);
        String leftStr = message.substring(middle_index+1);

        this.left = new EncryptedNode (leftStr);
        this.right = new EncryptedNode (rightStr);
    }

    System.out.println (this.letter);
    // System.out.println (this.right);

}


public static void main (String [] args) {

    EncryptedNode en = new EncryptedNode("ABCDEF") ;

    en.decrypt();
}

public String decrypt () {
    if (this.left == null && this.right==null) {
        return this.letter;
    }else if (this.left == null && this.right != null) {
        return this.letter + this.right; 
    }else if (this.left !=null && this.right != null) {
        return this.left + this.letter + this.right;
    }
}

}

I'm not going to write the code for you, since this is a classroom assignment, but I can explain how you should go about solving it.

You want to work backward through the encryption method in order to decrypt it. Right now, you're just returning "st" no matter what the encrypted string is.

I misread the code before. This is a recursive method, so you have to start your way from the bottom and work your way up. You're going to have to decrypt the left side and then decrypt the right side and add them together.

In the decrypt method, you have to recursively call this.left.decrypt(); and this.right.decrypt(); until you get to the base case. The base case is when the left and the right are equal to null. At that point you just return the letter. This is the bottom of the recursion.

So to write your decrypt method, you want to return this.left.decrypt() + this.letter + this.right.decrypt(); unless this.left or this.right are null.

If this.right is null, you'll return this.letter;

If this.left is null, you'll return this.letter + this.right.decrypt();

Sorry for the initial confusion, I thought this was an easier assignment than it really was.

Hopefully I helped this time!

Solution:

public String decrypt () {

    if (this.right == null) {
       return this.letter;
    }
    if (this.left == null) {
        return this.letter + this.right.decrypt();
    }
    return this.left.decrypt() + this.letter + this.right.decrypt();
}

You are very close to a valid solution. Keep in mind that according to your specs you store the text as letter + right + left and not ( left + letter + right ).

I am posting a solution, take a close look to understand how it works. If you are allowed to use StringBuilder (or the thread-safe StringBuffer) use those when you need to change the value of a String (I am putting the StringBuilder concatenation version in comments).

public static void main (String [] args) {

    EncryptedNode en = new EncryptedNode("ABCDEF") ;

    System.out.println(en.decrypt());
}

public String decrypt () {

    //if you are not allowed to use StringBuilder
    String s = String.valueOf(this.letter);
    if (this.right != null) {
        s = s + this.right.decrypt();
    }
    if (this.left != null) {
        s = s + this.left.decrypt();
    }
    return s;

    /*
    StringBuilder sb = new StringBuilder();
    sb.append(this.letter);
    if (this.right != null)
        sb.append(this.right.decrypt());
    if (this.left != null)
        sb.append(this.left.decrypt());
    return sb.toString();
    */
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM