Project 4: User Input, Parameters, Return Values and Caesar Ciphers


Due: Oct 6 at 11 pm

This assignment is designed to give you practice with:
  1. Methods with parameters and return values
  2. Strings
  3. User input
  4. for loops

You are not allowed to use Java features that are not covered in Chapters 1-3 of the textbook. In particular, you may not use conditional statements (if, if else, etc.), while loops or arrays.

Cryptography involves creating and decoding secret messages. Suppose you want to send a message, called the plaintext. You encode the plaintext to create the string you will actually transmit, called the ciphertext. Your goal is to prevent your adversaries from understanding the message if they intercept it.

Your program performs encryption on a message consisting entirely of uppercase letters using a Caesar cipher with a specified key, and decryption of a message (all uppercase letters) using a Caesar cipher with all possible keys (1, 2, ..., 25). A Caesar cipher encrypts a plaintext message using a key from 1 to 25 by replacing each letter in the message by the letter key positions ahead in the alphabet, with wraparound to the beginning of the alphabet as needed. So with key 5, the letter 'B' is replaced with 'G', and the letter 'Y' is replaced with 'D'. Decryption reverses this process; so during decryption with key 5, 'G' is replaced by 'B', for example. (Hint: use the modulus operator).

Your program will use static methods to provide structure and remove redundancy. You must include the following methods. This does not imply that you should not use other methods as well.

  1. public static String encryptCaesar(String plaintext, int key): this method takes a plaintext message consisting entirely of uppercase letters, as well as a key, and returns the corresponding ciphertext.
  2. public static char encryptChar(char ch, int key): this method encrypts the character ch using the specified key, and returns the encrypted character.
  3. public static void printPlain(String cipher): this method takes a ciphertext, and displays the corresponding plaintext, for each possible encryption key from 1 to 25. 

Your program will prompt the user for an encryption key, and a plaintext message to be encoded, and then produce the ciphertext. Then your program will prompt the user for a ciphertext message, and display the corresponding plaintext, for each possible encryption key from 1 to 25.

Hints:
1. You can do arithmetic on characters. For example, the ASCII value of 'A' is 65. The value of the expression 'A' + 5 is 70, and you can convert that value to the char 'E' by using an explicit cast: (char) ('A' + 5)
2. You will need to use the length() method for type String.


Sample Run (with values entered by the user underlined):

Enter encryption key: 4
Plaintext - uppercase letters only: HOOK
Ciphertext: LSSO

Enter ciphertext - uppercase letters only: LSSO

Plaintext for each encryption key from 1 to 25:
key 1: plaintext = KRRN
key 2: plaintext = JQQM
key 3: plaintext = IPPL
key 4: plaintext = HOOK
key 5: plaintext = GNNJ
key 6: plaintext = FMMI
key 7: plaintext = ELLH
key 8: plaintext = DKKG
key 9: plaintext = CJJF
key 10: plaintext = BIIE
key 11: plaintext = AHHD
key 12: plaintext = ZGGC
key 13: plaintext = YFFB
key 14: plaintext = XEEA
key 15: plaintext = WDDZ
key 16: plaintext = VCCY
key 17: plaintext = UBBX
key 18: plaintext = TAAW
key 19: plaintext = SZZV
key 20: plaintext = RYYU
key 21: plaintext = QXXT
key 22: plaintext = PWWS
key 23: plaintext = OVVR
key 24: plaintext = NUUQ
key 25: plaintext = MTTP



Submission and Grading:

Submit your file Caesar.java by the due date.

Part of your project grade comes from its "external correctness." This is based on correct output on various sample inputs (like you see in the sample run above).  Your output should match the format of the sample run exactly.

The rest of your program's score comes from "internal correctness." Internal correctness includes:
1. Use of the style guidelines (see the course webpage).
2. Use of for loops to capture repetition.
3. Use of static methods to reduce redundancy and structure the solution.
4. Appropriate use of comments, white space (e.g., indentation and blank lines), and meaningful identifiers to enhance readability of your code.
5. Correct header for your file.

Checklist - did you remember to: