CS 211 Project 3: Cipherous Symmetry full solution with JUnit Testing, Encryption and Decryption
Class Alphabet
Alphabet represents the set of characters that will be allowed in some message. For different messages, we can choose to use different Alphabet objects.
The primary function of Alphabets is to provide the translation of characters from symbols to integers and back via its indexOf(c) and get(i) methods. Several ciphers will require translating a character like 'C' into a number. The Alphabet provides such a functionality via its a.indexOf('C') method which will produce the integer associated with C if the letter is in the alphabet. Similarly, converting a number like 8 to an equivalent character is done via a.get(8) which returns a character from the alphabet.
Adhere to the abstraction barrier set up by Alphabet and use its methods to do character/number translations elsewhere in the project. This is a required part of the manual inspection criteria.
Fields
private String symbolspublic static final Alphabet DEFAULT = .... Due to the chance to mistype this, we have provided it in the project pack in Alphabet.java.
Methods
public Alphabet(String symbols). The constructor initializes symbols.public int indexOf(char c). Returns the index of char parameter c in string symbols. For example, the index of 'b' in "abc" is 1. Throws NotInAlphabetException if char parameter c is not in symbols.public char get(int i). Returns the char at position i of symbols. For example, the char at position 1 of "abc" is 'b'. Throws NotInAlphabetException if int parameter i is not a position in symbols. The NotInAlphabetException may have any character associated with as the offender parameter for its constructor.public int length(). Returns the length of string symbols.public String getSymbols(). Returns symbols.public String toString(). String representation. Example: if symbols is "ABC", returns "Alphabet(ABC)"public boolean equals(Object other). Return true if other is another Alphabet and has the same characters and ordering as this alphabet. The below examples below should compile and produce the indicated output.Object a1 = new Alphabet("ABCabc");Object a2 = new Alphabet("ABCabc");Object a3 = new Alphabet("123");Object s1 = new String("123");Object o1 = new Object();
System.out.println( a1.equals(a2) ); // trueSystem.out.println( a1.equals(a3) ); // falseSystem.out.println( a1.equals(s1) ); // falseSystem.out.println( a1.equals(o1) ); // false
Class NotInAlphabetException
Class NotInAlphabetException inherits from class RuntimeException.Fields
public final String msg;public final char offender;public final Alphabet a;Methods
public NotInAlphabetException(String msg, char offender, Alphabet a). The constructor initializes the data members.In the event that a NotInAlphabetException is needed for an unknown character, use this constructor with any character can be used for the offender parameter. This situation arises during some operations of the Alphabet class.public NotInAlphabetException(char offender, Alphabet a) The constructor initializes the data members. The value of the message is created by calling String.format() to create a string whose format is illustrated by the following example:"Not in the alphabet: 'a' not found in ABC."In the event that a NotInAlphabetException is needed for an unknown character, do not use this constructor. Use the other constructor
public String toString(). Returns msg.
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 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, aNotInAlphabetException 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 the alphabet.public Alphabet getAlphabet(). Returns 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 NotInAlphabetException if any character is found that isn't in the alphabet.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 NotInAlphabetException if any character is found that isn't in the 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 NotInAlphabetException 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 NotInAlphabetException if any character is found that isn't in the alphabet.
Buy now