Assignment 8: More Codes
Fall 2006, David Matuszek

Purposes of this assignment:

General idea of the assignment:

Using a slightly expanded version of my SecretCode GUI class (, have it handle three kinds of secret codes:


In the previous assignment, you were to write a class CodeMachine containing these three methods:

To start, change the name of this class to SimpleCodeMachine. Use Eclipse's Refactor > Rename command so everything gets changed correctly.

Next, write a public abstract class CodeMachine containing the following methods:

static CodeMachine create(String codeType, String secretKey)
Returns an object of type SimpleCodeMachine, ArrayCodeMachine, or PigLatinMachine, depending on whether the codeType is "simple", "array", or "pig latin", respectively. For the first two of these, the secretKey is used to construct the new secret code; for Pig Latin, it is ignored.

abstract public String encode(String input)
This will be supplied by each subclass.

abstract public String decode(String input)
This will be supplied by each subclass.

You will extend this class with three subclasses, SimpleCodeMachine, ArrayCodeMachine, and PigLatinMachine. My class, SecretCodes, will call the create method to get a code machine of some type, then call its encode and decode methods as required.


This is basically the same as the previous assignment. (In fact, you should be able to reuse most of the code from the previous assignment.) The only new part is that, instead of creating a random secret code, you create one based on the secret key.

Instead of the method createNewCode(), you will have a constructor, SimpleCodeMachine(String secretKey).

Here's what to do. First, prepare the secret key by lowercasing it, throwing out duplicate characters, and throwing out any nonletters. For example, if you start with "Happy Halloween!", downcase it to "happy halloween!", throw out duplicate characters to get "hapy lowen!", and throw out nonletters to get "hapylowen".

Next, put this secret key on the front of the lowercase alphabet and again throw out duplicate characters. That is, start with hapylowenabcdefghijklmnopqrstuvwxyz and get hapylowenbcdfgijkmqrstuvxz. This is your secret alphabet.


This uses a much more complex encoding scheme, but can encode (almost) every character you can enter from the keyboard.

You will need a constructor, ArrayCodeMachine(String secretKey). Your "alphabet" will be all the printable ASCII characters, plus space and newline (but not tab). Here it is, in a form you can copy and paste into a Java program:

" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO" +

Put the secret key on the front and throw out duplicate characters. For example, if the secret key is "Friday, October 27, 2006", we will get:

"Friday, October 27, 2006" +
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO" +

which becomes:

"Friday, Octobe2706" +
"!\"#$%&'()*+-./134589:;<=>?@ABCDEGHIJKLMN" +

Now put these characters, in this order, into an 8 by 12 array:

F r i d a y , O c t o
b e 2 7 0 6 ! " # $ % &
' ( ) * + - . / 1 3 4 5
8 9 : ; < = > ? @ A B C
R S T U V W X Y Z [ \ ]
^ _ ` f g h j k l m n p
q s u v w x z { | } ~ \n

Here's how to use this for encoding:

Rule Examples
If two characters are in different rows and different columns, then replace each character with a character in the same row but in the column of the other character. sh --> v_
At --> Bc
Random --> VFftcp
If two characters are in the same row, replace each character with the character to its right (end-around, if necessary). at --> yo
do --> aF
damp --> ayn^
If two characters are in the same column, replace each character with the character below it (end-around, if necessary). re --> e(
Vars --> g0er
If two characters are in the same row and same column (that is, they are the same character), replace each by the character to its right (end-around, if necessary). rr --> ii
occurrence --> Fti}ii%_r$

If you have an odd number of characters, the last character is a problem. Here's what you should do:


Your constructor for the PigLatinMachine class will not require a parameter. In fact, you can probably just use the default (Java-supplied) constructor.

The rules for translating into Pig Latin are:

Example: "When the cat's away, the mice will play." becomes "Enwhay ethay at'scay awayhay, ethay icemay illway ayplay."

It is not possible to "decode" Pig Latin with complete accuracy--for example, both stop and tops get encoded as opstay--but you should be able to decode a number of words correctly.


Before you leave the lab, you should have a pretty clear initial design for this project. That is, you and your partner should have decided what methods you will have (complete with signatures and return values), and what class each will be in. You will probably need to modify the design as you get into the project, but it is important to have an initial design as a starting point.

Other requirements:

Javadoc is required for all your classes and methods (except JUnit tests). I recommend, but do not require, comments (of some sort) for any JUnit methods whose purpose or meaning isn't totally obvious.

The required methods are the only ones that should be public. You will probably have quite a few additional methods. If you want to test a particular method, give it package (default) access. If you don't want to test it directly, make it private.

After you divide up the work with your partner, estimate how long your part will take, and write it down somewhere. Keep approximate track of how long it actually does take, and put both numbers (for each of you) in the Javadoc for your (new) CodeMachine class.

Due date:

Thursday, November 9, before midnight.