Calvin seal CS 108: Introduction to Computing
Spring 2006

Project #6

The Project

Do Project #6.1 from the HoTJ lab manual. You may find this wikipedia article useful for some of the operations.

Write up an OCD for two arithmetic methods (your choice). Use internal perspective to describe the behavior of your method. Put your OCDs in the Design/project06.txt file.

Do not implement a driver. Concentrate on the JUnit tests. You may and are strongly encouraged to share test data. Absolutely do not share any code, but interesting values to test should be shared with others.

Extra Credit

You cannot get any credit for the extra credit unless the rest of the assignment is completed. This extra credit is worth 10 points.

Instead of using ints for the numerator and denominator, use longs. The number of fractions you can now represent are much larger.

However, there is still a limit on the magnitude of the numerator and denominator since all integer types only store a finite number of digits. For example, if we humans could only remember three-digit decimal numbers, then it would be impossible for us to deal with fractions like 1000/3 or 11/2045.

In a situation like this, some arithmetic operations can overflow a numerator or denominator when it's really not necessary. To continue the three-digit decimal example, suppose we multiply 5/8 by 701/500; your present multiplication algorithm would first try to compute the product 3505/4000 and then simplify it (which would be 701/800); however, since we can only deal with three-digit fractions, the fraction 3505/4000 actually looks like 505/000 which is an invalid fraction!

We have this same problem with binary numbers, although it manifests itself a little differently.

However, if we note that the numerator of 5/8 shares a common factor with the denominator of 701/500, we can actually divide by the common factor before multiplying the numerators and denominators. So 5/8 * 701/500 turns into 1/8 * 701/100. This latter multiplication can be done without overflow.

Modify all of the arithmetic and relational operators of the Fraction class to avoid these unnecessary overflows. Be sure to add tests to prove your success.