CS 124, Spring 2017
Lab 7: Vigenère Cipher

Once again this week, you will be working with subroutines. In this case, you will work on an implementation of the Vigenère Cipher, the system for encoding and decoding text messages that was discussed and demonstrated in class. This is an exercise in writing subroutines and in using an API that provides some utility functions that can be used in solving the problem.

You will, presumably be working in Eclipse. You will probably want to create a new Java Project in Eclipse for this lab. You will need copies of the files CodeUtil.java and TextIO.java. You can copy-and-paste them from /classes/cs124 into the Eclipse window, putting them in the src folder in your project.

This lab is due by 3:00 PM next Friday, February 9. You should submit the file named Cipher.java using the Submit124 web site as usual.

The Assignment

You should write a program that does encoding and decoding of messages using the Vigenère cipher. The program must be named Cipher. Create a new class in your Eclipse project with that name. Your project should already contain the files CodeUtil.java and TextIO.java. Note that you should not modify CodeUtil.java; your program must work with the original version.

For the main() routine in the program, you should use the following code, which you should copy-and-paste from this web page into the file Cipher.java:

public static void main(String[] args) {
    
    String plainText;   // The un-encrypted message.
    String cipherText;  // The encrypted message.
    String key;         // The key for the Vigenere cipher.
    boolean encodeFlag; // Whether to do encoding or decoding.
    
    key = readVignereKey();  // returns a string of letters only!
    encodeFlag = askUserEncodeOrDecode();

    if (encodeFlag) {
        plainText = readInput();
        plainText = CodeUtil.lettersOnly(plainText);
        cipherText = vigenereEncode(plainText,key);
        writeOutput(cipherText);
    }
    else {
        cipherText = readInput();
        cipherText = CodeUtil.lettersOnly(cipherText);
        plainText = vigenereDecode(cipherText,key);
        writeOutput(plainText);
    }

} // end main();

You should not modify main(). To finish the assignment, you should write all the methods that are required by this main() routine. All of the methods are static. In addition to writing the method itself, you should add a JavaDoc comment for each method. You should also add a JavaDoc comment for class Cipher. Here are the requirements for the methods:

You must use functions from CodeUtil.java when appropriate. Read that file to see what functions are available.

Here is an example ciphertext that was encoded using the key carpe diem. This is an encoding of the preamble to the U.S. constitution: "We the people of the United States, in order...". Your program should be able to decode it:

YEKWISMSBNEFUXKMYZKTVSWWIXQUIEDVGMVFQFFGQDUSDGPVGJHKXGPIFCIV
BENNIJWNXAXUEEZCWXZIPQMVHXLKXDCNHJMOQXKRRFKMGMJATTYTGRUQAPDV
UIQKIBTODDXHBLQIEETVDTAQNFRGIDVHEGCLGIWPINNEJHMQOWAHLZQIUBCF
QOLGWHTZQUAESSXZTAUTVGMWGHAQRUPMQIRPGSKPFOQWTVHZHGRVWFKTLIMR
VJATTYTYQQXQFSKPXHASRCMVGMFI

Style Rules For Subroutines

Here are a few new style rules, relevant to writing subroutines and using member variables. You are expected to follow these rules, starting with this lab.

Note that JavaDoc comments are covered in the textbook in Section 4.5.4 Be sure to read that section and make sure that you understand how to write JavaDoc and what it is for.