Lab 5: Review

The goal of this lab is to review everything we have done so far: flow of control, writing methods, and making different kinds of variables.

Problem I: Arithmetic Sequence

1. Introduction

An arithmetic sequence is a sequence in the form ak = c + k * d. For example, if c = 2 and d = 3, then we have the sequence a0, a1, a2, a3 = c + 0*d, c + 1*d, c + 2*d, c + 3*d ... = 2 + 0*3, 2 + 1*3, 2 + 2*3, 2 + 3*3 = 2, 5, 8, 11

An arithmetic series is the sum of all elements a0 + a1 + ... + an, up to some given n. From our previous example, a0 + a1+ a2+ a3 = 2 + 5 + 8 + 11 = 26. The program you will write in the lab will do various implementations of arithmetic sequences and series.

2. Setup

As in previous assignments, make a directory for this assignment, and change into it.

mkdir lab5
cd lab5

Copy the file ArithmeticSeries.java from the public directory for this class:

cp /homes/tvandrun/Public/cs235/ArithmeticSeries.java .

You will notice that the main method is already written for you, to test drive what you will write. The only changes you will need to make to the main method is un-commenting the parts that are commented out.

3. Part 1: Computing a term in the sequence

The first part of the program queries the user for an index, and then computes the term in the sequence (just the sequence, not the series) for that index, using the method computeTerm(). Write this method, for the sequence ak = 1 + k*5 (i.e, c = 1, d = 5). It should be a simple, one-liner. Uncomment the section in the main method, and make sure it works.

4. Part 2: Computing the series

The next part queries the user for an index, but computes the series, that is, the sum of the terms in the sequence from zero to the given index, using the method computeSum(). Write this method so that it (1) make use of the method you write in Part 1, and (2) computes the sum. Uncomment the section in the main method, and make sure it works.

5. Part 3: Computing an array to store the terms of the series

The previous two parts computed only single answers. Now we want to keep an array that contains a collection of answers. The program will use an array called series which will contain the sums of the series for various limits. For example, series[5] will contain the summation of the sequence from 0 to 5. We will also keep this array in a static variable, so all parts of the program can use it without passing it back and forth among the methods.

So, (1) declare a static variable series, and (2) write the method computeSumArray() which, for a given maximum m, will (a) create an array of size m and (b) fill it so that each position i is assigned the sum of the sequence from 0 to i. How can you reuse the methods you wrote in the previous parts? There is more than one way. What should the return type of computeSumArray() be? Uncomment the section in the main method, and make sure it works.

6. Part 4: Computing the series, again

The advantage of storing all these results is that, since they are pre-computed, you can look them up later if needed rather than re-computing them. Write a method findSum() which, for a given index k, will return the sum of the terms in the sequence from 0 to k. However, instead of computing it directly as in computeSum(),

The net effect of this method is to return a pre-computed value if there is one available; and to compute the required value if not already present, storing the newly pre-computed results (ie, making the series array to grow).

Problem II: 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 oppressed 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 course public directory.

cp /homes/tvandrun/Public/cs235/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.

Copy the following files from the course public directory.

cp /homes/tvandrun/Public/cs235/Encrypto.class .
cp /homes/tvandrun/Public/cs235/Decrypto.java .
cp /homes/tvandrun/Public/cs235/mystery* .

Encrypto is a program I have written for you which will encode a text file using a random key. At the command line, give it the name of a text file and the name of a file you want to contain the encrypted version. For example, if you have a file named somefile.txt, the n running

java Encrypto somefile.txt somefile.cph

will produce a file called somefile.cph which will contain the encrypted text.

The file Decrypto.java is a partially written class for you to finish. It already contains code to read in a file, storing the contents of the file as a string referred to by the variable cipherText. Finish this program so that it prints the deciphered version to the screen (on the first try, without any other human intervention).

The files mystery1, mystery2, and mystery3 are encrypted texts you can try to decode.

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 Sep 29 16:57:36 CDT 2008