Lab 7: Review

The goal of this lab is to review the topics we have learned and apply them to a couple of interesting problems.

I. The Caesar Cipher

1. Introduction

A Caesar cipher is an encryption algorithm that shifts each letter down a set number of places in the alphabet. The shift amount serves as the key to the encryption scheme. For example, if we wanted to encrypt the string "hello" with shift amount 2, the result would be "jgnnq." Similarly we would shift each letter back by 2 to decrypt the word. Letters at the end of the alphabet would wrap around to the front. So, 'y' shifted ahead 5 would be 'd'.

You are a programmer for the ancient Gauls who are being opressed by Roman invaders under Julius Caesar. The Romans, of course, are using the Caesar cipher, invented by their general. Your spies are able to intercept much of the Roman communication, but you do not have any of the keys (ie, the number of letters to shift).

You are commissioned to write a program that will decrypt any message, even if you don't have the key. The idea is that there are only 26 possible keys (because if you shift the message any more than 26 positions, then it simply wraps around; shifting the message 27 characters is the same as shifting it 1). You will write a program that will allow a user to shift a message ahead until an intelligible message appears.

2. Setup

Copy the following file from the coure public directory.

cp /homeemp/tvandrun/pub/235/Cipher.java .

3. Finishing the program

This program calls a method that shifts a String ahead one character. The main method asks the user for a message, and then prints the next shifted version every time the user hits enter. For example, if the text the user enters is "My name is Vercingetorix", pressing enter will change the text to "Nz obnf jt Wfsdjohfupsjy." Pressing enter again will change the text to "Oa pcog ku Zgtekpigvqtkz." (The user will have to hit control-c to quit.)

Write the method shiftAhead()-- but make sure that you preserve capitalization, punctuation, and spaces. A capital letter should be shifted to a capital letter, a lowercase to a lowercase, and punctuation not at all.

4. Testing

Test your program by decoding the following messages sent over the front lines:

5. Extra credit

Instead of making the user shift until he or she recognizes the message, rewrite the program so that it makes use of known facts about letter and letter group frequencies in English to make an initial guess.

II. The Sieve of Eratosthanes

1. Introduction

The Sieve of Eratosthanes is a method for finding prime numbers. The idea is first to write out all numbers (from 2 up to a certain highest number) in a list.

Then cross off all the multiples of 2.

The next uncrossed number (in this case 3) is also the next prime number. Cross off every 3rd number that's not already crossed off.

Repeat this process. Find the next uncrossed number (say, n) and cross off every nth number after that.

2. Setup

Copy the following file from the course public directory:

cp /homeemp/tvandrun/pub/235/Seive.java

This program test drives a method which, given an integer, returns an array containing all the primes smaller than that integer.

3. Finishing the program

Fill-in the body of the method findPrimes() using the Seive of Eratosthanes method. Hint: Think of how a boolean array can be used to simulate the list. Think of the numbers in the list as indices into the array, and use the array to keep track of whether the number has been crossed-off or not.

III. To turn in

Use a script file to demonstrate the working of both of your programs. Print it out and hand in a hard copy.

a2ps -P sp (the name of the script file)

Thomas VanDrunen
Last modified: Mon Jul 23 14:39:56 CDT 2007