Lab Written in Java Your task is to write a command-line program that helps to d
ID: 3599987 • Letter: L
Question
Lab Written in Java
Your task is to write a command-line program that helps to decrypt a message that has been encrypted using a Caesar cipher1. Using this method, a string may contain letters, numbers, and other ASCII characters, but only the letters (upper- and lower-case) are encrypted – a constant number, the shift, is added to the ASCII value of each letter and when letters are shifted beyond ‘z’ or ‘Z’ they are wrapped around (e.g. “Crazy?” becomes “Etcba?” when shifted by 2).
When your program is correctly run, it will be provided two strings: the encrypted message and a string that is in the deciphered text. If fewer/more arguments are provided, your program is to output an error:
$ java edu.wit.cs.comp1050.PA4a
Please supply correct inputs: <encrypted string> <substring>
$ java edu.wit.cs.comp1050.PA4a a
Please supply correct inputs: <encrypted string> <substring>
$ java edu.wit.cs.comp1050.PA4a a b c
Please supply correct inputs: <encrypted string> <substring>
If the correct arguments are supplied, you are to output any shifts (00-25) that contain the supplied substring:
$ java edu.wit.cs.comp1050.PA4a 'Jvtwbaly zjplujl pz mbu!' is 09: Secfkjuh isyudsu yi vkd!
19: Computer science is fun!
$ java edu.wit.cs.comp1050.PA4a 'Jvtwbaly zjplujl pz mbu!' 'fun!' 19: Computer science is fun!
If no shifts contain the substring, provide an error:
$ java edu.wit.cs.comp1050.PA4a 'Jvtwbaly zjplujl pz mbu!' '?' No valid shifts found.
To build this program in an object-oriented fashion, you must first implement a Shifter class. This class is constructed with the encrypted string, and then has methods to both shift by an arbitrary amount and find substrings across all shifts.
To implement this class in an efficient manner, you should use a StringBuilder to shift the encrypted string. You might also find it useful to use an ArrayList to accumulate an unknown number of valid shifts. Look to the JavaDoc’s of the String class for methods to search a string for a substring.
_____________________________________________________________________________________________________________________________________
---------------- Main Method ---------------
//TODO: document this class
public class PA4a {
/**
* Error if incorrect command-line arguments are supplied
*/
public static final String ERR_USAGE = "Please supply correct inputs: <encrypted string> <substring>";
/**
* Error if shift could not be found
*/
public static final String ERR_NONE = "No valid shifts found.";
/**
* Outputs all shifts of the encrypted string
* that contain the supplied substring
*
* @param args command-line arguments: <encrypted string> <substring>
*/
public static void main(String[] args) {
// replace with your code
}
---------------- Shifter Method ---------------
//TODO: document this class
public class Shifter {
/**
* Number of letters in the English alphabet
*/
public static final int NUM_LETTERS = ('z' - 'a') + 1;
/**
* Initializes the shifter
*
* @param s encrypted string
*/
public Shifter(String s) {
// replace with your code
}
/**
* Returns the result of shifting
* by a supplied amount
*
* @param n number of places to shift
* @return shifted string
*/
public String shift(int n) {
return null; // replace with your code
}
/**
* Finds all shifts that contain
* the supplied substring
*
* @param sub substring to find
* @return array of shifts (0-25) that contain the substring (in order)
*/
public int[] findShift(String sub) {
return null; // replace with your code
}
}
Explanation / Answer
ANSWER::
import java.util.Scanner;
public class CaesarCipher
{
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
public static String encrypt(String plainText, int shiftKey)
{
plainText = plainText.toLowerCase();
String cipherText = "";
for (int i = 0; i < plainText.length(); i++)
{
int charPosition = ALPHABET.indexOf(plainText.charAt(i));
int keyVal = (shiftKey + charPosition) % 26;
char replaceVal = ALPHABET.charAt(keyVal);
cipherText += replaceVal;
}
return cipherText;
}
public static String decrypt(String cipherText, int shiftKey)
{
cipherText = cipherText.toLowerCase();
String plainText = "";
for (int i = 0; i < cipherText.length(); i++)
{
int charPosition = ALPHABET.indexOf(cipherText.charAt(i));
int keyVal = (charPosition - shiftKey) % 26;
if (keyVal < 0)
{
keyVal = ALPHABET.length() + keyVal;
}
char replaceVal = ALPHABET.charAt(keyVal);
plainText += replaceVal;
}
return plainText;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String for Encryption: ");
String message = new String();
message = sc.next();
System.out.println(encrypt(message, 3));
System.out.println(decrypt(encrypt(message, 3), 3));
sc.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.