Lab 6: 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.

1. Introduction

An arithmetic sequence is a sequence in the form ak = c + k * d. An arithmetic series is the sum of all elements a0 + a1 + ... + an, up to some given n. The program you will write in the lab will do various implementations of arithmetic sequences and series.

2. Setup

As in previous assignments, move into your cs235 directory. Make a directory for this assignment, and change into it.

cd cs235
mkdir lab6
cd lab6

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

cp /homeemp/tvandrun/pub/235/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 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).

7. Turn in

Create the script file as before (cat, rm, compile, and run). Print it out and hand in a hard copy.

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

(This will print "two up", meaning two pages shown next to each other on one pice of paper. If you use a2ps on a Java file, it will format it nicely like in the handouts I've given in class. The command lpr works similarly except it does no formatting and doesn't print two up by default.)


Thomas VanDrunen
Last modified: Tue Oct 3 08:49:54 CDT 2006