| Implementation
Hints for the Secret
Codes Assignment Fall 2007, David Matuszek |
The one-parameter constructor for the Encoder class takes a 26-letter string,
for example "jaicyukovqhetrzpgsxnmwblfd", and stores it away in some form for
later use.
The no-parameter
constructor for the Encoder class
is supposed to create a random secret code. An easy way to do this is to
take a String containing the lowercase alphabet (that is, the string "abcdefghijklmnopqrstuvwxyz")
, convert it to an array of characters, shuffle the array, and convert the
array back into a String. With a little thought, you should be able to figure
out how you can pass the resultant string to the one-parameter constructor;
if not, you will have to repeat some code in both constructors.
String class, in the Java API.static void shuffle(int[] array) {
for (int i = array.length; i > 1; i--) {
int j = random.nextInt(i - 1);
swap(array, i - 1, j);
}
} |
The Encoder class doesn't need the Lexicon class for anything. With the Encoder class, you should be able to encode a message, decode it again, and get exactly
what you started with.
The purpose of the Lexicon class is to provide tools that can
be used by the
Decoder class. Even if you don't use all the tools, the assignment
requires you to write them all.
The "high level view" of the Decoder class is this:
The ultimate goal is to take some text, encoded with some unknown code, and
decode it as best as you can.
One thing your main method should allow the user to do is to
read in a plain text file, encode it with some random encoding, then (without
knowledge of the encoding) decode it the best it can. Your main method
might let the user do some other things, like use particular codes or enter
things to code and decode--whatever you find useful. When the TAs and I run
your program, however, we don't want to have to guess how to do things with
it, so be sure to have nice, clear prompts.
There is no simple way to decode something, and I do not expect great solutions.
Please get the Encoder and Lexicon classes
fully debugged first; then do what
you can with the Decoder class. If your decoder works well enough
that someone can more-or-less read the result, that's great; if not, don't
worry about it.
Remember: Strings are written with double quotes ("abc")
but characters
are written with single quotes ('a').
Also, remember that a char is a number, so you can do
arithmetic with it. For example, if the variable ch holds one
of the lowercase letters 'a' through 'z', then is the corresponding number in the range 0 to 25. (However,
it's bad form to use the numeric value directly, even if you know it: In, the 97 is a magic number.)
When you do arithmetic with a character, the result is an int,
not a char. If you want a character back, you have to cast it.
For example,
It's easy enough to make an array that has the same size as some other array:
int[] newArray = new int[oldArray.length];
You cannot make an array without saying how big to make it. What you can do is to make a list, put things into the list, then make an array from the list.
import java.util.ArrayList;ArrayList<String> list = new ArrayList<String>();list.add("hello");
list.add("goodbye");String[] words = list.toArray(new String[0]); //
Magic--don't try to understand!