Java Problem: I need help with a Hierachy class (class Symmetric Cipher). This p
ID: 3729079 • Letter: J
Question
Java Problem: I need help with a Hierachy class (class Symmetric Cipher). This program is about Encryption. Please read and help me. Thank you so much. Tester cases are provided at the bottom.
---------------------------------Here is the task that I need help------------------------
class Cipher
Class Cipher is an abstract class and all of its methods are abstract. This parent class represents any object that can encrypt and decrypt strings.
Methods
public abstract String encrypt(String s);
public abstract String decrypt(String s);
class SymmetricCipher
Class SymmetricCipher is an abstract class that inherits from class Cipher. Not all of its methods are abstract.
Fields
protected Alphabet alphabet: The alphabet that this cipher works on. Characters that are to be encrypted/decrypted that do not exist in the alphabet will cause problems. In such cases, a MissingCharAlphabetException should be raised.
Methods
public SymmetricCipher(Alphabet alphabet). The constructor initializes the data member.
public int wrapInt(int i). Given an index value that may be outside the range of valid indexes into the alphabet, wrap it back around to a valid index.
public int rotate(int index, int shift). Given an index into the alphabet, rotate it around shift times to the resulting valid index into alphabet. When the index is out of bounds in either direction, we wrap around until it's back in range. No matter what index and shift value is given, we should be able to return an answer (assuming the alphabet has a non-zero length! Which it always should).
Hint: doesn't this sound a bit like modular arithmetic?
public Alphabet getAlphabet(). Returns alphabet.
protected abstract char encrypt1(char c). Child classes must provide an implementation of this; it provides a way to encrypt a single character. Child class implementations will throw MissingCharAlphabetException if any character is found that isn't in the alphabet.
protected abstract char decrypt1(char c). Child classes must provide an implementation of this; it provides a way to decrypt a single character. Child class implementations will throw MissingCharAlphabetException if any character is found that isn't in the alphabet.
public String encrypt(String s). Implement this method based upon the encrypt1 definition (below), which encrypts a single character (think of the Caesar Cipher for an understanding). Throws MissingCharAlphabetException if any character is found that isn't in the alphabet.
Manual Inspection Criteria (5%): encrypt/decrypt methods correctly invoke encrypt1/decrypt1 as part of their solution.
public String decrypt(String s). Implement this method based upon the decrypt1 definition, which decrypts a single character (think of the Caesar Cipher for an understanding). Throws MissingCharAlphabetException if any character is found that isn't in the alphabet.
Manual Inspection Criteria (same as above): encrypt/decrypt methods correctly invoke encrypt1/decrypt1 as part of their solution.
-------------------------------------Here is the tester cases----------------------
Tester cases for Class SymmetricCipher: https://paste.ee/p/aVCsy
-----------------------------------Addtional Info-----------------------------
Since this program is about Hierarchy. The task that I need help with required the first 3 classes of the program.
Here is the link of java codes of the first 3 tasks that I have finished: https://paste.ee/p/33qTc
Here is the link to the first 3 tasks of the program (class Alphabet and AlphabetException)
Explanation / Answer
SymmetricCipherTests.java
import org.junit.*;
import org.junit.Test;
import static org.junit.Assert.*;
import java.util.*;
public class SymmetricCipherTests {
public static void main(String args[]){
org.junit.runner.JUnitCore.main("SymmetricCipherTests");
}
static Alphabet uppers = new Alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ .");
static Alphabet lowers = new Alphabet("abcdefghijklmnopqrstuvwxyz");
static Alphabet lowerSpace = new Alphabet("abcdefghijklmnopqrstuvwxyz ");
class SwapRWCipher extends SymmetricCipher{
public SwapRWCipher(Alphabet a){
super(a);
}
public SwapRWCipher(){
super(Alphabet.DEFAULT);
}
public char encrypt1(char c){
switch(c){
case 'R': return 'W';
case 'r': return 'w';
case 'W': return 'R';
case 'w': return 'r';
default: return c;
}
}
public char decrypt1(char c){
switch(c){
case 'R': return 'W';
case 'r': return 'w';
case 'W': return 'R';
case 'w': return 'r';
default: return c;
}
}
}
SwapRWCipher swap = new SwapRWCipher(Alphabet.DEFAULT);
@Test (timeout=2000) public void test_Symmetric_1(){ assertEquals( 5, swap.rotate( 4, 1)); }
@Test (timeout=2000) public void test_Symmetric_2(){ assertEquals( 5, swap.rotate( 4, 1)); }
@Test (timeout=2000) public void test_Symmetric_3(){ assertEquals( 3, swap.rotate( 4, -1)); }
@Test (timeout=2000) public void test_Symmetric_4(){ assertEquals(10, swap.rotate(10, 93)); } // DEFAULT has 93 characters.
@Test (timeout=2000) public void test_Symmetric_5(){ assertEquals(91, swap.rotate( 3, -5)); }
@Test (timeout=2000) public void test_Symmetric_6(){ assertEquals( 9, swap.rotate( 2,100)); }
@Test (timeout=2000) public void test_Symmetric_7(){ assertEquals( Alphabet.DEFAULT, swap.getAlphabet()); }
@Test (timeout=2000) public void test_Symmetric_8(){ assertEquals( uppers, new SwapRWCipher(uppers).getAlphabet()); }
@Test (timeout=2000) public void test_Symmetric_9() { assertEquals( 1, swap.wrapInt( 1)); }
@Test (timeout=2000) public void test_Symmetric_10(){ assertEquals( 0, swap.wrapInt( 93)); }
@Test (timeout=2000) public void test_Symmetric_11(){ assertEquals(92, swap.wrapInt( -1)); }
@Test (timeout=2000) public void test_Symmetric_12(){ assertEquals(88, swap.wrapInt( -5)); }
@Test (timeout=2000) public void test_Symmetric_13(){ assertEquals(70, swap.wrapInt(1000)); }
@Test (timeout=2000) public void test_Symmetric_14(){ assertEquals("no fancy alphas", swap.encrypt("no fancy alphas"));}
@Test (timeout=2000) public void test_Symmetric_15(){ assertEquals("no fancy alphas", swap.decrypt("no fancy alphas"));}
@Test (timeout=2000) public void test_Symmetric_16(){ assertEquals("hello rowld", swap.encrypt("hello world"));}
@Test (timeout=2000) public void test_Symmetric_17(){ assertEquals("rwRW", swap.encrypt("wrWR"));}
@Test (timeout=2000) public void test_Symmetric_18(){ assertEquals("hello rowld", swap.decrypt("hello world"));}
@Test (timeout=2000) public void test_Symmetric_19(){ assertEquals("rwRW", swap.decrypt("wrWR"));}
}
Alphabet.java
public class Alphabet {
// Fields
public static final Alphabet DEFAULT = new Alphabet(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 1234567890!@#$%^&*()_+-=[]{}\|;:'",./?<>");
private String symbols;
private String msg;
// Methods
// Standard constructor
public Alphabet (String symbols) {
this.symbols = symbols;
}
// Find out where c is in our alphabet
public int indexOf (char c) {
// Get the index
int index = symbols.indexOf(c);
// Check if we have a valid index, if not throw an exception
if (index == -1)
throw new NotInAlphabetException(c, this);
else
// If we got here, we have a valid index to return
return index;
}
// Get whatever char is at index i
public char get (int i) {
// Check to see if we can even index into our alphabet
if (i > length()) {
msg = String.format("Asked to get symbol @%s, but length of Alphabet is %s.", i, length());
// Our exception
throw new NotInAlphabetException(msg, '!', this);
}
else
// Return the character
return symbols.charAt(i);
}
// Get the length
public int length () {
return symbols.length();
}
// Return our alphabet
public String getSymbols () {
return this.symbols;
}
// Tell the user what our alphabet looks like
public String toString () {
return String.format("Alphabet(%s)", (symbols));
}
// Allow us to compare to other alphabets
public boolean equals (Object other) {
return (((Alphabet) this).getSymbols().equals(((Alphabet) other).getSymbols()));
}
}
NotInAlphabetException.java
public class NotInAlphabetException extends RuntimeException {
// Fields
public final String msg;
public final char offender;
public final Alphabet a;
// Methods
public NotInAlphabetException (String msg, char offender, Alphabet a) {
this.msg = msg;
this.offender = offender;
this.a = a;
}
public NotInAlphabetException (char offender, Alphabet a) {
this.offender = offender;
this.a = a;
// Set a standard message because one wasn't provided by the user
this.msg = String.format("Not in alphabet: '%s' not found in %s.", offender, a);
}
public String toString() {
return this.msg;
}
}
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.