CS 162, Fall 2003
Programming Assignment 3
Due: Wednesday, April 28th

Cipher Application

In this assignment you will combine material from Chapters 12 and 15. The application you are to develop is a cipher application. The window for the application consists of four graphical items. At the top is a button labeled open. To the west is a slider bar, to the south a label box, and in the center a text area. The window looks as follows:

The following example shows how to to place the various graphical elements.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Cipher extends JPanel 
{
	public static void main(String [ ] args) 
	{
		JFrame appFrame = new JFrame();
		appFrame.setDefaultCloseOperation(3);
		appFrame.setTitle("Cipher Application");
		appFrame.setSize(400, 400);
		appFrame.setContentPane(new Cipher());
		appFrame.show();
	}

	public Cipher() 
	{
		setLayout(new BorderLayout());
		add(openButton, BorderLayout.NORTH);
		add(keyLabel, BorderLayout.SOUTH);
		add(text, BorderLayout.CENTER);
		add(key, BorderLayout.WEST);
	}

	private JLabel keyLabel = new JLabel("Key is 0");
	private JButton openButton = new JButton("Open");
	private JTextArea text = new JTextArea();
	private JScrollBar key = new JScrollBar(JScrollBar.VERTICAL, 5, 5, 0, 25);
}
The program will work as follows. The slider selects a value from 0 to 25. The current value of the slider should be displayed in the bottom label box. When the open button is pressed a file dialog is displayed (see Section 15.3). The selected file is opened as a text file, and the contents of the file are then encoded according to a variation on the caeser cipher and displayed in the central text area.

Section 15.4 presents a simple caesar cipher. This program reads a binary file, end encodes each character as a byte value. Our program will be slightly different. It will read a text file, and encode only letter characters; spaces and newlines will remain unchanged. In addition, case will be preserved. That means lower case letters will be mapped into lower case letters, and upper case letters into upper case letters.

The verison of the Caeser cipher you will use is actually closer to the original than the program given by Cay. If the key is set as 3, for example, the letter a will be encoded as d, the letter b as e, and so on until you reach the letter v. The letter v is encoded as z, the letter x as a, and the letter y as b and the letter z as c. So you see the alphabet just wraps around the letters. The same goes for the upper case letters. Characters that are not lower or upper case letters, such as white space, digits, or anything else, are left unchanged.

In addition to using the Sun documentation to learn about the graphical elements, will will probably also find the class Character useful in this assignment. The TA will have further hints in the recitation sections.