Skip to content

Instantly share code, notes, and snippets.

@geluso
Created July 18, 2018 19:44
Show Gist options
  • Save geluso/0338240a9aee06168c16ad2f660db12b to your computer and use it in GitHub Desktop.
Save geluso/0338240a9aee06168c16ad2f660db12b to your computer and use it in GitHub Desktop.

Total points: 8/10

Overall Structure

-1 point: missing Main.java file described in lab writeup. -1 point: zero tests

There should be a seperation between the objects you create and a program that uses those objects. Each of the cipher files should just have code that encodes and decodes text. The Main.java should be there to accomplish the task of being a program that collects user input, creates cipher objects and manipulates them.

Receiving User Input

Nice work configuring your program to receive both numbers and words to select between encode / decode.

Nice work using the validSelection boolean and error printing according to that.

Class Inheritance

Excellent job using inheritance to override the encode and decode methods for each cipher! This is excellent! Great job using the protected method replaceCharacters as described in the lab writeup.

import java.util.Scanner;

public class ROT13Cipher extends Cipher {
    char[] codeAlphabet = "nopqrstuvwxyzabcdefghijklm".toCharArray();


    public String encode(String payload){
        String codedAlphebet = new String(codeAlphabet);
        return replaceCharacters(payload,ALPHABET,codeAlphabet.toString());
    }

    public String decode(String payload){
        String codedAlphebet = new String(codeAlphabet);
        return replaceCharacters(payload,codedAlphebet,ALPHABET);
    }

}

Eliminating Redundancy

You can eliminate redundancy in your switch statement by taking further advantage of the inheritance. You could do something like this:

This code splits up choosing a cipher, and choosing whether to encode/decode. Since all the ciphers extend from the base Cipher class you know they have the encode and decode methods.

Yes, this code is an approximation to show you an example of eliminating this type of redundancy. You would have to add further prompts for input to allow people to enter their keyword and such.

int method = promptForInt("1: caeser\n2: ROT13\n3: Keyword");

Cipher cipher = null;
if (method==1)  {
  cipher = new CaesarCipher();
} else if (method==2) {
  cipher = new ROT13Cipher();
} else if (method==3) {
  cipher = new KeywordCipher();
}

String result = null;

int choice = promptForInt("1: encode\n2: decode");
if (choice == 1) {
  result = cipher.encode();  
} else {
  result = cipher.decode();  
}

System.out.println(result);

A Variation on replaceCharacters

Your replaceCharacters method does things manually. Good job finding out how to go through each letter of the source string and searching through the target letters to find the corresponding letter to replace it with.

There is a method you can use to find the location of a character or string that will save you from writing these for loops in the future: indexOf

protected String replaceCharacters(String payload, String source, String target) {
    char[] replacedString = new char[payload.length()];
    for(int i = 0; i < payload.length(); i++){
        for(int j = 0; j< source.length(); j++)
            if(payload.charAt(i) == source.charAt(j))
            {
                replacedString[i] = target.charAt(j);
            }
    }
    String returnerString = new String(replacedString);
    return returnerString;
}
protected String replaceCharacters(String payload, String source, String target) {
    String result = "";
    for (Character cc : payload.toCharArray()) {
        int index = source.indexOf(cc);
        if (index >= 0) {
            result += target.charAt(index);
        } else {
            result += cc;
        }
    }
    return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment