CS202 Assignment 3-- Caesar’s Challenge (group assignment)
Due: 11:59pm 06/26/2023(Monday)
Introduction
As a renowned detective, Sherlock Holmes receives a lot of mail. Some of his mail included
puzzles from his rivals, attempting to be the first to confuse Sherlock. Unfortunately, many of
these correspondents are not very creative. In particular, he frequently gets “secret” messages
encoded with Caesar ciphers. Although these ciphers are trivial for Sherlock to crack, he has
much better things to do, and has therefore passed this task on to Dr. Watson. Help relieve Dr.
Watson of this boring job by writing a program to automatically decipher these messages.
Caesar cipher
A Caesar cipher takes as input a message string and a shift value n, and shifts each letter in the
message forward through the alphabet by n letters. For example, let's say n = 1. Then, shifting
the letter ‘A’ forward by 1 would return ‘B’, and shifting ‘B’ forward by 2 would return ‘D’.
The alphabet should wrap around - shifting ‘Z’ forward by 1 would return ‘A.’ For example,
shifting the word “HAL” forward by 1 would return “IBM”, and shifting “Abcdefghijklm,
nopqrstuvwxyz!” forward by 5 will return “Fghijklmnopqr, stuvwxyzabcde!”. (Notice that the
case of the letters is preserved, and non-letter symbols are unchanged.)
It might be easier to understand what "shifting forward" means if you look
at https://computerscienced.co.uk/site/caesar-cipher-wheel/caesar-cipher/
Decoding a message encoded with a shift of n is equivalent to encoding a message with a shift
of 26 − n. Of course, these rivals haven’t given Sherlock the shift value - your program has to
figure it out itself. One way to do this is by shifting the message by all 26 possible shifts, and
returning the one that seems most like English. To do this, you might find helpful this 26-
element array, holding the frequencies of each letter in English:
double[] englishFrequencies = {0.08167, 0.01492, 0.02782,
0.04253, 0.12702, 0.02228, 0.02015, 0.06094, 0.06966,
0.00153, 0.00772, 0.04025, 0.02406, 0.06749, 0.07507,
0.01929, 0.00095, 0.05987, 0.06327, 0.09056, 0.02758,
0.00978, 0.02360, 0.00150, 0.01974, 0.00074};
That is, 8.167% of all letters in English text are A, 1.492% are B, ... and 0.074% are Z.
Specification
Implement the class CaesarDecipher, which consists of the method
public static String decipher(String ciphertext);
Example
Input
decipher("Me m dqzaizqp pqfqofuhq, Etqdxaow Taxyqe dqoquhqe m bxqftadm ar rmz
ymux.");
Output
As a renowned detective, Sherlock Holmes receives a plethora of fan mail.
Hints:
Use a brute-force strategy to try every possible key value (1 ~ 25) and calculate the
frequency error that it generates, for example, the frequency error can be the summation of the
absolute frequency difference between the actual frequency of a letter in the deciphered text and
the given frequency of that letter in English text. For example, for a key candidate of 6, letter ‘W’
would be deciphered to letter ‘C’, and the absolute frequency difference would be:
| occurrence of letter ‘C’ in the deciphered text – 0.02782 |
After calculating the frequency errors for all candidate key values, choose the one with the smallest
frequency error, and use that key to decipher the ciphertext.
Submission requirements:
1. Submit your .java file to canvas. Use extra methods when necessary. Do not put everything
in the decipher().
2. A screenshot of your running result. (png, jpg, pdf, or docx)
3. Each group can have at most two students. If you work with another student in a group,
submit a README.docx or README.txt containing both students’ names and a
description of each student’s work in this assignment. Both students need to submit his/her
assignment to canvas. I might randomly pick students to ask you give me a demo and
explain how your code works.
4. Make sure your code is free of compile errors. A project with compile errors in it will earn
you a very low grade.
5. Follow the Java style guide (Files/ASSIGNMENTS/JavaStyleGuide.pdf) and comment
your code whenever necessary.
6. Refer to (Files/AQuickGuide2Eclipse.pdf) for how to import an existing project into
Eclipse, how to open a workspace, etc.
Academic Honesty:
Your code must be your own. Any format of sharing code is not allowed and is considered
plagiarism.