Skip to content

Instantly share code, notes, and snippets.

@geluso
Created July 18, 2018 19:17
Show Gist options
  • Save geluso/7a5522d15950fb5aa25eb68c99c1d13a to your computer and use it in GitHub Desktop.
Save geluso/7a5522d15950fb5aa25eb68c99c1d13a to your computer and use it in GitHub Desktop.
05-cipher-grading-B

Total points: 8/10. -1 point class inheritance doesn't override encode and decode. -1 point no real tests implemented.

Project Structure

Nice project structure especially with separating out the src and test directories. It's true that most Java projects do their structure like that, but I just haven't bothered with it yet as I've been using the auto-generated IntelliJ code. Eventually we'll get to some tooling that will set things like this up correctly automatically.

Class names in Java are always capitalized. Change your casesarCipher and keywordCipher filenames and classnames to CasesarCipher and KeywordCipher respectively to match Java styles. Having lower-case classnames will be a -1 stylistic point in the future.

Reading User Input

Good job checking user input to make sure users entered things properly.

Inheritance

Nice work using the CaesarCipher inside the ROT13 cipher.

Slick work providing only one option for the ROT13 cipher because you noticed that the encode and the decode are the same.

You didn't override the encode and decode methods for the Ciphers, instead you made different names like caesarCipher, Rot13 and keyWordCypher.

The advantage of overriding a method with the same name is you can build code that interacts with anything that extends Cipher, all subclasses.

if (method==1) Ciphers.caesarCipher.caesarCipher(str, key, true);
if (method==2) Ciphers.caesarCipher.caesarCipher(str, key, false);
if (method==3) Ciphers.Rot13.Rot13(str);
if (method==4) Ciphers.keyWordCipher.keyWordCypher(str,strkey,true);
if (method==5) Ciphers.keyWordCipher.keyWordCypher(str,strkey,false);

You could write something like the following. The important part is that you have one object that has a type of Cipher. Then, you instantiate it using one of the subclasses. Then the whole point of inheritance is the subclasses all have methods called encode and decode so you can call those methods safely, then allow the overridden methods actually execute the code defined in the subclass.

(PS, I'm taking liberties changing the structure of your program and assuming you have a method like promptForInt to illustrate my example.)

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);

Drop Curlys

Nice work building up the Caesar cipher. Just letting you know that most Java programmers do not use "drop-curlies" for while loops. That's more of C, C++, C# style.

while (encryptionkey.length() < 27)
{
    encryptionkey += Cipher.ALPHABET.charAt(key);
    key++;
    if (key >25) key-=26;
}

One-Line If Statements

I know you love one-line if statements but be aware where you use them. This line is on the edge of becoming long and readability would benefit from being spread across two lines. (it's longer if you include indentation)

if (!key.contains(keyword.substring(i,i+1))) key += keyword.charAt(i);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment