CM-111 Spring 2018 Assignment 6: The Cesar Cipher Due 3/8/2018 Background Julilu
ID: 3724596 • Letter: C
Question
CM-111 Spring 2018 Assignment 6: The Cesar Cipher Due 3/8/2018 Background Julilus Caesar is credited with using this simple substitution encoding scheme to protect military messages. Caesar used a key of three to substitute the letters of the original message with the characters three places away in the alphabet. Here is an example A Caesar cipher using a key of 3 and a right shift The original message: Encoded message ATTACK THE ENEMY DWWDFN WKH HQHPB : Since spaces can reveal information to help decipher the code, spaces would be removed Final encoded message: DWWDFNWKHHQHPB With a key of 3 and right shift, a letter in the original message is replaced by the letter three places to the right. To decipher an encoded message, you find the coded letters in the alphabet, and replace them with letters 3 letters to the left. We can visualize this by juxtaposing the letters of the alphabet above an alphabet shifted three characters. Find the letter to encode in the upper list and replace it with the character in the lower list. To decode, find coded letters in the lower list and replace with top letter. ABCDEFGHUKLMNOPQRSTUVWxYZ DEFGHUKLMNOPORSTUVWXYZABC Assignment Write a program that will encode and decode messag your class caesarcipher es using the Caesar cipher with a right shift. Name The program will: 1. Prompt for the key; a number between 1 and 25. 2. Prompt for E for encrypt or D for Decrypt. 3. Prompt for the message to encode/decode. 4. Encode or decode the message. Remove all spaces before encoding. 5. Print the encoded or decoded message. 6. Prompt for q to quit or Enter to encode/decode another message. 7. The program should keep looping to encode/decode messages until Q is entered.Explanation / Answer
Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.
// CaesarCipher.java
import java.util.Scanner;
public class CaesarCipher {
static Scanner scanner;
public static void main(String[] args) {
/**
* Initializing Scanner object to get user input
*/
scanner = new Scanner(System.in);
String choice = "";
/**
* Loops until user wishes to quit
*/
while (!choice.equalsIgnoreCase("q")) {
// getting key value
int key = getKey();
// getting the operation to be performed (E or D)
String EorD = encodeOrDecode();
String text;
if (EorD.equalsIgnoreCase("E")) {
// getting the text to be encoded
text = getInput();
// removing spaces
text = removeSpaces(text);
// encoding and displaying the text
System.out.println("Encoded text: " + encode(text, key));
} else {
// getting the text to be decoded
text = getInput();
// removing spaces
text = removeSpaces(text);
// decoding and displaying the text
System.out.println("Decoded text: " + decode(text, key));
}
// asking if the user wishes to continue or not
System.out.println("Type q to quit, or press Enter to continue...");
choice = scanner.nextLine();
}
}
/**
* method to prompt the user to enter a valid key value between 1-25 and
* returns it
*/
static int getKey() {
System.out.println("Enter a key value (1-25)");
int key = Integer.parseInt(scanner.nextLine());
if (key < 1 || key > 25) {
/**
* Invalid input, displaying error message and calling the method
* again, recursively
*/
System.out.println("Invalid key, try again..");
return getKey();
} else {
return key;
}
}
/**
* method to prompt the user to choose either E for encoding or D for
* decoding and returns it
*/
static String encodeOrDecode() {
System.out.println("Enter E for encode, D for decode");
String choice = scanner.nextLine();
if (choice.equalsIgnoreCase("E") || choice.equalsIgnoreCase("D")) {
return choice;
} else {
System.out.println("Invalid choice, try again..");
return encodeOrDecode();
}
}
/**
* method to fetch the text to be encoded/decoded from the user
*/
static String getInput() {
System.out.println("Enter the text:");
return scanner.nextLine();
}
/**
* method to remove the spaces from a given text and returns it
*/
static String removeSpaces(String text) {
return text.replaceAll(" ", "");
}
/**
* method to encode a text
*
* @param text
* - text to be encoded
* @param key
* - key value (1-25)
* @return encoded text
*/
static String encode(String text, int key) {
String encodedText = "";
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (Character.isUpperCase(c)) {
// shifting the character 'key' times right
c = (char) (c + key);
/***
* Checking if the character went out of range
*/
if (c > 'Z') {
/**
* shifting the character to fit within range , i.e if a
* character go past Z, it will be started again from the
* front ('A') and moved the remaining spaces to the right
*/
c = (char) (c - 'Z' + 'A' - 1);
}
// appending the character to the output text
encodedText += c;
} else if (Character.isLowerCase(c)) {
// performing same operations, but here it is lower case
c = (char) (c + key);
if (c > 'z') {
c = (char) (c - 'z' + 'a' - 1);
}
encodedText += c;
}
}
return encodedText;
}
/**
* method to decode a text
*
* @param text
* - text to be encoded
* @param key
* - key value (1-25)
* @return decoded text
*/
static String decode(String text, int key) {
String decodedText = "";
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (Character.isUpperCase(c)) {
/**
* Shifting the character 'key' times to the left
*/
c = (char) (c - key);
/**
* checking if the character go beyond 'A', if yes, wrapping up
* to fit within A-Z, by starting from the end, and shifting the
* remaining places to the left
*/
if (c < 'A') {
c = (char) (c + 'Z' - 'A' + 1);
}
decodedText += c;
} else if (Character.isLowerCase(c)) {
c = (char) (c - key);
if (c < 'a') {
c = (char) (c + 'z' - 'a' + 1);
}
decodedText += c;
}
}
return decodedText;
}
}
/*OUTPUT*/
Enter a key value (1-25)
4
Enter E for encode, D for decode
E
Enter the text:
THIS DATA IS GOING TO BE ENCODED
Encoded text: XLMWHEXEMWKSMRKXSFIIRGSHIH
Type q to quit, or press Enter to continue...
Enter a key value (1-25)
4
Enter E for encode, D for decode
d
Enter the text:
XLMWHEXEMWKSMRKXSFIIRGSHIH
Decoded text: THISDATAISGOINGTOBEENCODED
Type q to quit, or press Enter to continue...
Q