1

For homework I have been assigned the task of creating a program that encrypts a String based on a method called the Rail Fence Cipher. ( I don't know if this is common knowledge or not, or even if you guys want to see exactly what it is, so i have included the Wikipedia link if you wish too see).

I have been able to encrypt the string successfully in a straight main method. I am now writing the code correctly using classes.

I am not sure if I should just use a normal class and create an object or not. My first inclination is to make the class abstract, and simply manipulate the data, and return the encrypted method. I am not really sure what the correct way to go about this it. I doubt i would have any problem writing the code in an abstract class, or an object class. I am unsure of which is considered correct though.

Can anyone offer any guidance? The biggest reason I see not to use an abstract class would be that this program is simple and does not use any sort of interfaces.

    public final class Scrambler {

private Scrambler() {

}

/*
*Scrambles a string using a three line rail fence cipher technique 
*
 */
public static String scrambleByThreeLines(String _pText) {

    //takes the message string, removes spaces, and makes lowercase
    _pText = _pText.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();

    //Takes the length of the plain text string, and identifies how long it is,
    //then the next highest integer that is divisble by three
    int length = _pText.length() + (4 - (_pText.length() % 4));

    //creates a temporary array to use when populating the actual array that
    //will contain the extra null characters
    char[] tempText = _pText.toCharArray();

    //creates what will be the full array including the null characters
    char[] pText = new char[length];

    //loop to populate full array
    for (int i = 0; i < pText.length; i++) {
        if (i < tempText.length) {
            pText[i] = tempText[i];
        } else {
            pText[i] = 'x';
        }
    }
    /*Scrambling function of the cipher-------------------------------------
    *creates four strings that are concatenated togeather with a space inbetween
    *utalizing the three rail cipher technique as shown on Page 13 of the 
    *Martin Gardner book, "Codes Ciphers, and Secret Writings"
    */
    String one = "";
    String two = "";
    String three = "";
    String four = "";
    int counter = 0;
    for (int i = 0; i < pText.length; i++) {
        if ((i + 1) % 2 == 0) {
            if (counter % 2 == 0) {
                three += pText[i];
            } else {
                two += pText[i];
            }

        } else {
            if (counter % 2 == 0) {
                one += pText[i];
            } else {
                four += pText[i];
            }
            counter++;
        }
    }
    return String.format(one + " " + two + " " + three + " " + four);
}

/*
*Unscrambles a string using a three line rail fence cipher technique 
 */
public static String unscrableByThreeLines(String _cText) {
    //splits the text into the four strings to decode-----------------------
    String[] a = _cText.split(" ", 4);

    char[] one = a[0].toCharArray();
    char[] two = a[1].toCharArray();
    char[] three = a[2].toCharArray();
    char[] four = a[3].toCharArray();
    //----------------------------------------------------------------------
    //removes one char at a time in the correct order to decode to a String
    String pText = "";

    for(int i =0; i < one.length; i++){
        pText += one[i];
        pText += two[i];
        pText += four[i];
        pText += three[i];
    }

     return String.format(pText);
    }
}
R.Hull
  • 126
  • 1
  • 8
  • Is your abstract class supposed to be extended? If not then don't make it abstract. – Pshemo Feb 05 '16 at 00:32
  • No i wouldint be extending it. the only real reason i see for making it abstract is that im only manipulating the data. The comparison i see in my head is like that of the Math class – R.Hull Feb 05 '16 at 00:35
  • 2
    Don't create objects. Make `public static` methods and mark the `constructor` as `private`, then call each one like `Encrypt.encryptString(String string)` – Riley Carney Feb 05 '16 at 00:43
  • 1
    If you want to create utility class then you can make it `final` to avoid extending it, and make its constructor private (to avoid its instantiation). To be fair making only constructor private makes it also not extendable so we could skip `final` keyword but it is good to have it just for clarity reasons (like `java.lang.Math` does it). – Pshemo Feb 05 '16 at 00:43
  • I have edited my post to include the code I have tried to write based off your suggestions. Is this what you were suggesting or have i screwed it up completely? – R.Hull Feb 05 '16 at 01:20

0 Answers0