7: Creating a "Secret Code"
Fall 2006, David Matuszek
will provide a class
SecretCode which contains a
method. When the
main method is executed, it displays a window
similar to the one at the right. Clicking the buttons will call methods in
CodeMachinewhich has methods in it to create a new "secret code" and to encode and decode secret messages.
Supplied code: SecretCode.java and a starter version of CodeMachine.java.
Write a class
CodeMachine with (at least) the following methods:
public String createNewCode()
a -> m
b -> y
, etc. No letter should substitute for itself; in other words, you can't have
c -> h
(for example). The method should save the newly created secret code in an instance variable or variables of the
k -> k
CodeMachine, and it should return a string containing the alphabet (in normal order), a newline (
'\n'), and the alphabet (in scrambled order)--for example,
public String encode(String input)
public String decode(String input)
SecretCode class expects to find the above methods, with the
exact signatures specified, in your
CodeMachine class; so these
methods are required. The "helper" methods suggested below
are only suggested, not required.
Test-Driven Development. Provide Javadoc comments and JUnit tests for all your
To create a new "secret code," I suggest you create a
of 26 locations, containing the lowercase letters of the alphabet. Every
time you want a new secret code, just shuffle (randomize) this array.
To shuffle the array, I recommend writing a method
. You are already familiar with using
so this shouldn't be too hard.
Here are some facts that might come in handy:
charliterals are written with single quotes,
Stringliterals with double quotes.
chars are numbers, so you can do comparisons and arithmetic:
ch1 <= 'z'
forloop with a
charindex, for example,
for (char ch = 'a'; ch <= 'z'; ch++)
chars, you get an
int; cast it back to a
char. For example,
(char)(ch + 1)
'a' + 25 = 'z'
To translate text, make a
StringBuilder object from your input
String. It's much easier to replace one character with another
StringBuilder than in a
String. Then, just go
through character by character, doing the translation, and replacing each letter
with its encoded (or decoded) counterpart. When you're done translating, use
toString() method to get a
String back from the
It's easier to look up characters in a
String than in a
Hence, you may wish to convert your
char to a
Remember that the
Character class has methods for converting letters
to uppercase or to lowercase. It also has tests for whether a character is a
letter, and if so, what case it is. If you can translate a single letter, and
get the case right, you can get the case right for the entire text. Use the
Java API, don't depend entirely on my lectures or your book.
I would suggest writing the following "helper" methods that you
can call from
These methods will encode or decode, depending on which order you give it the two alphabets. Or, you can use
char translate(char ch, String fromAlphabet, String toAlphabet)
- Translates a single character. If
chis lowercase, this method looks it up in the
fromAlphabet, and returns the character in the corresponding location in the
chis uppercase, it does the approriate conversions to return the uppercase translation. If
chisn't a letter, it is just returned unchanged.
void translate(StringBuilder text, String fromAlphabet, String toAlphabet)
- Using the previous method, translates all the characters in text.
charparameters for the two alphabets, but that's probably a bit more work.
By the way--it isn't hard to test methods that return
void, if they make changes in one of the supplied parameters. For example, the
shuffle method described above changes the
char array given as a parameter.
If you encode a string, and then decode it (without getting a new secret code), you should get back a string that is equal to the original string.
This is an individual project. You can talk with your classmates and help one another, but write and turn in your own code. Don't share code with anyone.
Remember to put your name in an
@author tag in the Javadoc for
Estimate how long this will take you. Write down your estimate. Keep track
of how long it actually takes you. Put both numbers in the main Javadoc for
Thursday, October 26, before midnight. Via Blackboard.