Lab 12: Oh, no!

This goal of this project is to practice handling exceptions.

As usual, make a new directory for this project and cd into it.

1. Introduction

Bozo the programmer is trying out what he thinks are super efficient or compact versions of methods similar to what we've done before. However, nothing seems to be working. Use exception handling to save the day. For each of the four sections (2-5), uncomment the next part of the main method, find out what exceptions are being thrown, and use that to determine what to catch.

Copy the following files from the course directory and look at them.

> cp /homeemp/tvandrun/pub/235/Bozo.java .
> cp /homeemp/tvandrun/pub/235/Node.java .

2. Summing an array

Bozo is particularly excited about his method for summing all the elements in an array. Take a look at it. The method takes a short cut and skips the loop if it's working on an empty array, and all the adding happens in just one line. He particularly likes how the loop header hardly takes up any space. You try to tell him that this will result in an infinite loop, but he insists that when he has tested it, the loop finishes. Sure, the program crashes, but at least the loop does finish.

Fix sum(int[] array) so that it works correctly, but do not change or remove any of the lines of code that are already there (in particular, make sure Bozo's favorite line is untouched). Instead, just add something around Bozo's favorite line.

3. Summing a list

Bozo is convinced that if statements are inefficient. He has now written a method that sums linked lists that doesn't use an if statement. Unfortunately, it doesn't work.

Fix sum(Node node) so that it works---but do not use an if statement.

4. Fibonacci numbers

Bozo has also written a recursive Fibonacci function. He know that this recursive version is inefficient, but who cares--- it fits all on one line!

Now Bozo's curious... "I wonder what the negative first Fibonacci number is." When he tries to call fib(-1), something goes wrong, be he can't tell what because the error message fills the entire screen.

To figure out what the problem is (if you can't guess on your own), try starting a script file and running the program. Then exit the script and look at the file using the command less. Typing less filename will display to the screen the contents of the file called filename; you can scroll through it using the arrow keys.

This time, do not make any changes to the method fib. Instead, put the line that calls fib(-1) inside a try/catch clause.

5. Rangify

Bozo has also written an interesting method which he calls rangify. It takes an array and an integer n and returns the ratio of the value in the array at n positions before the midpoint to the value in the array at n positions after the midpoint. However, he has tried it on several arrays, and it always causes the program to crash-- in fact, it seems to crash for different reasons every time.

Bozo wants that if the method receives bad parameters that it print a message to the screen explaining what's wrong and return 0. "But don't use any ifs", Bozo insists.

6. Turn in

Copy the files you modified to

/homestu/jmichalk/labTA/turnInProjects/yourFirstName,yourLastInitial/lab12


Thomas VanDrunen
Last modified: Tue Nov 21 08:56:37 CST 2006