Java Program Tasks: Help is greatly appreciated. Life saver. Please help. Please
ID: 3730828 • Letter: J
Question
Java Program Tasks: Help is greatly appreciated. Life saver. Please help.
Please help with a single task (class) in this program (class CaesarCipher only) that will be extended from class SymmetricCipher (provided). This program is about Encryption (Hierarchy). All required classes to be used to extend will be provided below (links). Tester cases for this class is also at the bottom. Please read and help me. Thank you so much.
- Here is a structure of this program.(Hierachy)
----------------------------------Java Codes----------------------
Here is the link to the java codes for class Alphabet, AlphabetException, BadIndexAlphabetException andMissingCharAlphabetException that I have finished (Will be used for inhereting for this CaesarCipher class)-----------------> https://paste.ee/p/19qsM
Here is the link to the java codes for class SymmetricCipher (will be used to extend)--------------------->https://paste.ee/p/PIkkA
Note: Use the Alphabet class to determine the which number corresponds to each character in a message during encryption and decryption. This will be done the most in the implementation of encrypt() and decrypt() for which SymmetricCipher is primarily responsible. CaesarCipher will need to implement encrypt1()/decrypt1() that determines the results of shifting single ints.
---------------------------------Here are the tasks that I am seeking for help-------------------------
class CaesarCipher
Fields--------
protected int shift: The shift that this cipher uses to adjust characters during encryption/decryption.
Methods---------
public CaesarCipher(int shift, Alphabet alphabet). The constructor stores the shift and alphabet.
Note: even if shift is out of range of the alphabet, store it exactly as given and let other code deal with it.
public CaesarCipher(int shift). This constructor stores the shift and uses the default Alphabet found in Alphabet.DEFAULT.
Note: even if shift is out of range of the alphabet, store it exactly as given and let other code deal with it.
public char encrypt1(char c). Encrypts and returns a single character based on the shift and the alphabet in use. Will throw MissingCharAlphabetException if any character is found that isn't in the alphabet.
Manual Inspection Criteria (5%): implement encrypt1/decrypt1 methods but rely on the inherited encrypt/decrypt methods fromSymmetricCipher.
public char decrypt1(char c). Decrypts and returns a single character based on the shift and the alphabet in use. Will throw MissingCharAlphabetException if any character is found that isn't in the alphabet.
Manual Inspection Criteria (same as above): implement encrypt1/decrypt1 methods but rely on the inherited encrypt/decrypt methods from SymmetricCipher.
public String encrypt(String s). Shifts each character by the shift amount and returns the newly encoded string. Inherited implementation.
public String decrypt(String s). Shifts each character in the opposite direction by the shift amount, which will recover the original secret message. Inherited implementation.
public String toString(). Return a string representation of the cipher in the following format
where the value associated with shift is taken from the internal field of the class.
-----------------------------Here is the link to the tester cases---------------------
Tester Cases for class Caesar Cipher----------------> https://paste.ee/p/2OdnF
---------------------------Additional Info--------------------------------
Summary of the whole Program (all tasks--clases): https://paste.ee/p/oyr5d
Introduction of this program (encryption): https://paste.ee/p/6iJER
------------------------Thank you so much--------------------------------
I would greatly appreciate your helps
Explanation / Answer
public class CaesarCipher extends SymmetricCipher {
// Fields
protected int shift;
// Methods
public CaesarCipher(int shift, Alphabet alphabet) {
super(alphabet);
this.shift = shift;
}
public CaesarCipher(int shift) {
super(Alphabet.DEFAULT);
this.shift = shift;
}
@Override public String encrypt (String s){
return super.encrypt(s);
}
@Override public String decrypt (String s){
return super.decrypt(s);
}
@Override public char encrypt1(char c) {
int index = alphabet.indexOf(c);
return alphabet.get(rotate(index, wrapInt(shift)));
}
@Override public char decrypt1(char c) {
return alphabet.get(wrapInt(rotate(alphabet.indexOf(c), -shift)));
}
@Override public String toString() {
return String.format("Caesar Cipher (shift=%s)", shift);
}
}
SymmetricCipher.java
public abstract class SymmetricCipher extends Cipher {
// Fields
protected Alphabet alphabet;
// Methods
// Standard constructor
public SymmetricCipher (Alphabet alphabet) {
this.alphabet = alphabet;
}
public int wrapInt (int i) {
if (i >= alphabet.length())
while (i >= alphabet.length())
i -= alphabet.length();
else if (i < 0)
i = alphabet.length() + i;
return i;
}
public int rotate (int index, int shift) {
int test = index + wrapInt(shift);
if (test > alphabet.length())
return test - alphabet.length();
else
return test;
}
public Alphabet getAlphabet () {
return this.alphabet;
}
@Override public String encrypt(String s) {
char[] charArray = s.toCharArray();
for (int i = 0; i < charArray.length; i++) {
charArray[i] = encrypt1(charArray[i]);
}
return new String(charArray);
}
@Override public String decrypt(String s) {
char[] charArray = s.toCharArray();
for (int i = 0; i < charArray.length; i++) {
charArray[i] = decrypt1(charArray[i]);
}
return new String(charArray);
}
protected char encrypt1 (char c){
return 'a';
}
protected char decrypt1 (char c){
return 'a';
}
}
Cipher.java
public abstract class Cipher {
public abstract String encrypt(String s);
public abstract String decrypt(String s);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.