CIT 597 Assignment 9: Fractions and Unit Testing

Fall 2008, David Matuszek

# Purpose of this assignment:

- To get you started with unit testing in Ruby.

# General idea:

Implement a `Fraction`

class. Write tests for it.

Because this is a very short assignment, it will be worth 25 points
instead of the usual 100 points.

# Programming details:

## Fraction

Implement a `Fraction`

class. It should have:

- A constructor that takes two arguments (the numerator and denominator)
- The instance methods
`add`

, `subtract`

, `multiply`

,
and `divide`

. Each of these should take one operand, the second
argument, and return a new fraction (for example,
`f1 = f2.add(f3)`

)).
- A
**private** `reduce!`

method, which uses
Euclid's
algorithm
to put a fraction into the lowest terms (it divides both the numerator
and the denominator by the gcd, Greatest Common Divisor, of the two).
- A
`to_s`

method, for returning a string representation (for
example, `"2/3"`

) of this fraction.

All fractions should *automatically* be kept in lowest terms. For example,
if you construct a fraction with `f = Fraction.new 8, 12`

,
you will get the fraction `2/3`

. Likewise, if you add `1/4`

and
`1/4`

, you should get `1/2`

. Also, there is *nothing
wrong* with so-called
"improper" fractions (fractions greater than 1, such as `7/3`

);
they are perfectly good fractions.

To keep this assignment simple, you don't have to worry about negative numbers.

## FractionTest

Implement a `FractionTest`

class containing unit tests
for each of the four arithmetic methods, and a test for the constructor.
You can test the constructor by testing, for example, whether `Fraction.new 8, 12`

is
equal to `2/3`

.

# Due date:

Wednesday, November 26, by midnight. Submit, via Blackboard, one file
named `fractions.rb`

(you can put both classes into this one file).