Question: I have been stuck on this program for hours with no clue how to start and approach this. I would appreicate any help! Thanks. Gaius
I have been stuck on this program for hours with no clue how to start and approach this. I would appreicate any help! Thanks.
Gaius Julius Caesar encoded his battle messages so that the opponent could not read them should they intercept them. The cipher required that both Caesar and the recipient knew the key to the cipher.
You are to write a program that reads in a message from a text file, encodes the message with a Caesar cipher, and then outputs the message to another file. Your program should also be able to read in an encoded message and decode it.
Define What this program must do.
Outline How this program will accomplish the task. Your solution should be modularized.
Specifications as outlined in class discussions
Input
-The program should read, in order, from standard input (a.k.a. the console window): - The filename of the text file to open - The cipher key for encryption/decryption - Note: No one asked about the length of the cipher key. Therefore it remains a mystery... - A digit: 1 to encrypt or 2 to decrypt
If the file does not exist, or the user somehow provides bad input data, the program should not crash. Instead, it should print a message to show the user how to use the program:
Usage: Cipher fileToCipher cipherKey 1forEncode/2forDecode
Encryption and Decryption
To encrypt a message, each letter in the message is shifted right by a number corresponding to the distance of the cipher key letter from the first letter in the alphabet. The cipher key is reused over and over until the message is encrypted. Punctuation and spaces are not encrypted but still appear in the encoded message. Uppercase and lowercase must be preserved. Letters "wrap" around from 'z' to 'a'.
Message: The ships sail at dawn. Cipher key: swiftly Output: Ldm lsgho xttj wb wluf
---------------------------------------------------------------------
| Message letter | Cipher letter | Shift Right | Result |
|---|---|---|---|
| T | s | 18 | L |
| h | w | 22 | d |
| e | i | 8 | m |
| f | none | ||
| s | t | 19 | l |
| h | l | 11 | s |
| i | y | 25 | g |
| p | s | 18 | h |
| s | w | 22 | o |
| i | none | ||
| s | f | 5 | x |
| a | t | 19 | t |
| i | l | 11 | t |
| l | y | 25 | j |
Output
Output is written to a text file which is named same as the input file plus .coded if being encrypted or .decoded if being decrypted .
Helper Methods
These shift methods contain the formula for shifting by some k value. You do not need to use them if you would rather figure out your own. If you do come up with a snazzier method than these, you could earn a few extra credit points. Here "up" means add to the current value; "down" is shift back the other way.
// Use: distance is synonymous with the position in the alphabet of a letter from 'a': a=0, b=1, c=2, ... // Call: shiftUpByK(theCharacterInTheMessage, alphabet.indexOf(theCharacterInTheCipherKey) public static final int NUM_LETTERS = 26; // shifting up for the encoding process public char shiftUpByK(char c, int distance) { if ('a' <= c && c <= 'z') return (char) ('a' + (c - 'a' + distance) % NUM_LETTERS); if ('A' <= c && c <= 'Z') return (char) ('A' + (c - 'A' + distance) % NUM_LETTERS); return c; // don't encrypt if not an alphabetic character } // shifting down for the decoding process public char shiftDownByK(char c, int distance) { return shiftUpByK(c, NUM_LETTERS - distance); } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
