CIT 591 Playfair Cipher Clarifications
Fall 2008, David Matuszek
The purpose of the StringReader is, given a String, to return the characters
from that String two at a time. It also ensures that the two characters returned
are not the same, by inserting an 'x' or a 'q' as necessary.
For example, suppose you construct the StringReader with
StringReader reader = new StringReader("helloworld");
then the subsequent calls to reader should return the following two-element character arrays:
{'h', 'e'}{'l', 'x'}{'l', 'o'}{'w', 'o'}{'r', 'l'}{'d', 'x'}null // To indicate that no characters remainThe constructor for CodeBlock has the task of creating a 5x5 array from
the keyword or key phrase that is passed in. To do this, it first has to
"clean up" the keyword or key phrase by lowercasing it, replacing 'j' with 'i',
removing all nonletters, and removing all duplicate characters. There are
methods in Sun's String class and your own StringUtilities class
that it can use to do this.
CodeBlock contains a getCodeArray() method. The only purpose of this method
is so that you can JUnit test the constructor, to see whether it constructs
the correct 5x5 array from the keyword or key phrase.
Since the CodeBlock object contains the 5x5 array, it
has encipher and decipher methods for character
pairs. These methods take a pair of characters (in a two-element
char[] array), encipher or decipher that pair of characters,
and return the enciphered/deciphered pair in another two-element char[] array).
The job of enciphering or deciphering a complete message is done in the PlayfairCipher
class.
This is the "main" class--the one containing a main method.
The main method (along with any helper methods it may call)
asks the user for a keyword or keyphrase, and constructs a PlayfairCipher object
with that keyword or key phrase. The main method then repeatedly lets the
user choose to enter a message to be enciphered, enter a message to be deciphered,
change the keyword (for future enciphering/deciphering), or quit. The actual
enciphering/deciphering is done by calling methods in PlayfairCipher.
The main method (or a helper method) also calls printInBlocksOfFive(String) in
the StringUtilities class to print results.
The constructor for PlayfairCipher creates a CodeBlock using that keyword or key phrase; that's about all the constructor is expected to do. Of course, it has to keep that CodeBlock object (in an instance variable) for future use.
The encipher(String) and decipher(String) methods of PlayfairCipher encipher
and decipher, respectively, complete messages. They do this two characters
at a time, by calling the StringReader method getNextPairOfLetters() and
the CodeBlock methods encipher(char[]) and decipher(char[]).
This class provides just methods for use by the other classes. Since all
the methods in this class are static, there is no reason to construct any
objects of type StringUtilities.
You should write JUnit tests for:
CodeBlock (because it does significant
work). You should not write JUnit tests for
CodeBlock.
These will be adequately tested by trying to use the objects that are
constructed.main and run methods in CipherExpert.
These are concerned with input/output, which is more difficult to test.getCodeArray method in CodeBlock. The
only point of this method is to let you test whether the CodeBlock
constructor is working; so if getCodeArray doesn't work, you'll find
that out when you use it to test the CodeBlock constructor.printInBlocksOfFive method in StringUtilities, because
it just does output, and output is difficult to test.