My ImitationGame Explanation and Analysis

Whew, I finally managed to complete the arduous task of creating an encryption/decryption system. After many misconceptions and bugs, I managed to pull through, gaining new understandings of concepts like static and have cleared up many grey areas that I had before starting this project (instances, scope, structure of code). Starting off I was assigned to create 3 classes, Cipher, Decipher, and ImitationGame with the main method. Therefore, starting with my encryption cipher method would be more logical, and would make it easier to decrypt once I know how to encrypt a word. I started with class Cipher by declaring my 3 field variables, one is the encrypted text (result), and the others are the two strings that I will refer to throughout my classes and code, as they are the strings that include characters that can be encrypted, and characters that can be decrypted. Then, I started to make the encryption method. I decided to use an indexing technique that will iterate through each character in the inputted string, and compare it to each character in the alphabet. The first for loop’s role is to make sure that each letter in the inputted string is iterated through, by making the loop only stop when every character in input has been checked, and the second for loop is the checker, it compares the targeted character in input with every character in alphabet, until it finds a match, and when it does, take that position from the alphabet in the decrypted alphabet and add that character from alphabets position to the new encrypted string (result). Next, I printed the encrypted text result so the user can see their encrypted test, and returned it. Finally, it was necessary to set getters, so that I can refer to the field variables in Cipher later by making an instance of Cipher and using the method (e.g. Cipher.getResult()). Although it is also possible to make the field variables static so that other classes can access them by making an instance of Cipher, however, I was suggested to use get Methods.

Moving onto the Decipher class, I didn’t have as much trouble with this class, as I did with Cipher, for I just needed to swap a few things around, like on line 12, instead of adding characters from decryption, since it is the reverse, I needed to add the characters from alphabet to the result(decrypted text). Also, I created an instance of Cipher, and used its public Get methods in order to obtain private information from Cipher, required for Decipher to understand the different strings it has to iterate through. Then, I just printed out the result for the user to see their decrypted text, and returned it.

Finally writing the main method (inside the ImitationGame class) was a tedious process, and I had to be careful with many things like using break and making sure the loop would continue no matter what the user typed, while also giving the user an option to quit. I managed to accomplish these things, as I included an else statement in my first prompt to the user, and also if the user decides to encrypt, it will prompt ask the user if he/she would like to decrypt the immediate word, or return back to the main menu, by typing anything, even though I specified (Y or N) for simplicity sakes (Line 22) any phrase other than “Y” will return back to the top of the loop. Additionally, I solved the break issue of printing statements later on in the code, by simply making the loop reliant on a variable being true or false, and if there is a scenario in the main method where the code should stop, I can just assign the variable tru = false like I did on Line 39.

Overall this assignment took me to another level in Java, and I feel was really beneficial towards my proficiency level in Java. It really helped me gain a much better understanding of how OOP works, in such ways as understanding hierarchy in Java through coding (Classes – Objects – Methods – Variables), and realizing why I needed to create an instance of a class (object), to run a method of that class.

Leave a Reply