Write a program with 3 classes in Java for encryption and decryption using the String class methods such as “length”, “charAt” and loops.
class ImitationGame (with main method)
class Cipher (takes in one String and store the encrypted text as a field variable)
class Decipher (takes in one String and store the encrpted String) as a field variable
Complete the program and upload the code to github. Write a blog post describing and explaining how you completed the program and all resources (e.g. stackoverflow) you used. Avoid copying directly from other sample code.
Sources that I used throughout this project:
How I completed the program:
The first thing that I did was create the program that didn’t utilize the substring function. Here, a string is taken and encrypted into a “secret” (it can easily be figured out) code. All it does it shift the letters in the string by one letter in the alphabet. How it works is through a single method that I either called decipherCode() or convertCharacter(). A for loop is used to go through each char in the given string. The char are of course being read by the charAt() function. With each char, I had to then figure out how I could shift the letter by one in the alphabet. Initially, I thought arrays could be my solution. But that became very complicated and I didn’t really feel it would be efficient to teach myself a whole other topic. Instead, I turned to ASCII. Each char would be converted into an int through casting it, one would be added to value, and re-converted back into the next char in the ASCII chart. Finally, the method would print the result.
The second source code pretty much does the same thing, but the reason why I created it was so I could create a purpose of using substring in my code. This one became a lot more complicated than the first program I typed up. In this one, users are able to select what parts of the string they would like to encrypt or decrypt rather than automatically acting on the whole string. This is done by taking in two numbers (indexes). At first, I considered just making users have to put in those two numbers in the source code as a parameter to some kind of method. But I decided to go a little bit further and allow them to input it through scanner. I created two extra methods, where each of them have the purpose of enabling people to input a number (one to select when to start encrypting/decrypting the string and one to select when to end encrypting/decrypting the string). Then I used the substring function, where the two numbers inputed by the user become the parameters of the method. The way the actual method of encrypting and decrypting the message is very similar to the original program I created. The major difference lies in an extra if/else statement and change in the way I wrote my for loop.