Assignment 7: Arrays

The goal of this assignment is to practice writing with arrays. If you haven't already, finish the pre-assignment first.

If you finished the pre-assignment before getting to class today, make sure you are in your assignment 7 directory.

4. Setup

Copy two files from the public directory:

> cp /homeemp/tvandrun/pub/241/GeometricSequence.java .
> cp /homeemp/tvandrun/pub/241/Fractions.java .

5. Geometric sequence

A geometric sequence is one that grows exponentially. A geometic sequence with base x would be ak = x^k. So, if the base is 2, the series (starting with k being 0) is 1, 2, 4, 8, 16, 32, ...

The file GeometricSequence.java contains a program which prompts the user for a base and a limit for the sequence (think of the limit as n, and we want to find the sequence from a0 to an). There is a stub for a method calcGeoSeq which, given a base and a limit, produces an array holding the desired sequence. Write the body for that method.

Hints:

Compile and test.

6. Fractions

The file Fractions.java contains a program that queries a user for two fractions and then shows the result of adding, subtracting, multiplying, and dividing the two fractions, and displays them in reduced form. Figure out how it works (especially how we model the fractions) by reading the documentation and the code.

Of the arithmetic operations, only add has been written. Write sub, mul and div. Follow the pattern of add-- they'll all be similar. For div, you can reduce your work by making it call mul (think about it).

Then implement the method reduce. This one is different. It's a void method. Instead of creating a new array and returning it, we want to modify the array we are given. Reduce the fraction to simplest form using the method gcd.

7. Turn in

Create the script file as before (cat, rm, compile, and run). Run the palindrome test on two palindromes and two non-palindromes. Run the geometric sequence for base 3 and limit 15. Run the fraction program on two different pairs (ie, two runs of the program with different input) of your choosing. Then print it.

 > 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.)

Then turn in the hard copy in class on Wednesday or put it in my box by 5:00 pm.


Thomas VanDrunen
Last modified: Fri Feb 25 15:37:09 CST 2005