Lab 12: Oh, no!

This goal of this project is to practice handling exceptions.

Clone the repository for this week's lab:

hg clone /cslab/class/csci235/lab12

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.

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

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

5. Summing a list

This is something new for us, and it actually includes a useful idea. Because an instance of a class can contain a reference to another instance, we can use those references to chain together data structures. In this case, we can think of a list as recursive structure, where a list is either

In this program, a list is represented by the class Node. The last Node in a list will be followed by an empty list; i.e., it's next value will be null.

Look at the first line of part 5. Draw a picture of the variable list and what it points to.

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

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

6. Turn in

Script your program's execution. Turn in the typescript and your source file as lab12.


Thomas VanDrunen, Cary Gray
Last modified: Thu Mar 17 16:31:33 CDT 2011