CIT 590 Assignment 7: Fractions
Fall 2015, David Matuszek

Purposes of this assignment

General idea of the assignment

Implement an API for Fractions. You will need two classes:

Arithmetic with Fractions

Components Two integers, known as the numerator and the denominator.
How printed n/d, where n is the numerator and d is the denominator. However, if the denominator is 1, then only the numerator n is printed.
Restrictions The denominator may not be zero. 
Normalization The fraction is always kept in the lowest terms, that is, the Greatest Common Divisor (GCD) of n and d is 1 (use Euclid's algorithm--you can look this up); and
The denominator is never negative.
Addition a/b + c/d is (ad + bc)/bd
Subtraction a/b - c/d is (ad - bc)/bd
Multiplication (a/b) * (c/d) is (a*c)/(b*d)
Division (a/b) / (c/d) is (a*d)/(b*c)
Absolute Value Assuming that a/b is normalized:
a > 0 ? a / b : -a / b
Negation -(a/b) is -a/b
Special Inverse of a/b is b/a
Equality If in lowest terms, numerators are equal and denominators are equal.
Comparison a/b is less than c/d if ad < bc
Zero Zero is represented with a numerator of zero and a denominator of one.

class Fraction implements Comparable<Fraction>

The Fraction class is an API--an Application Programmer's Interface. That is, it isn't a program in itself; it is designed to be used by programmers who, for whatever reason, need to use fractions in their programs.

The Fraction class should have three constructors:

Fractions should be constructed in normalized form. For example, if the user says new Fraction(6, 8), the fraction should be created with a numerator of 3 and a denominator of 4 (not 6 and 8). Similarly, if the user says new Fraction(1, -3), the fraction should be created with a numerator of -1 and a denominator of 3 (not 1 and -3).

Constructors should throw an ArithmeticException if called with zero for a denominator.

And these public methods:

Notes

Fractions should be immutable, that is, there should be no way to change their components after the numbers have been created. Most of your methods simply return a new number.

Fractions should always be kept in a normalized form. Since fractions are immutable, and there is no way to normalize one from outside the Fraction class, normalization should be done by a private method.

You probably won't use compareTo in this assignment. However, by supplying equals and compareTo methods, you make it possible to do other "numerical" things with fractions, like sorting them.

When you override equals, notice that it requires an Object as a parameter. This means that the first thing the method should do is make sure that its parameter is in fact a Fraction, and return false if it is not.

When you write your JUnit tests, you may think that you need to get to the numerator and denominator of your fractions. You don't. Once you write an equals method, JUnit's assertEquals method will use your equals method, and this is all you really need.

Your goal in writing the JUnit class is to test for every possible error. This includes making sure that the correct Exceptions are thrown when appropriate.

class FractionTest

Write tests for all the public methods in Fraction. Remember, your goal in testing is not to "prove" that your methods are correct. Rather, it is to try to "break" your methods in every way you can think of.

Eclipse will create this class for you. See the "Advice" section below.

Advice

The best way to do this is to write all the constructors and methods of the Fraction class as stubs. That is, the constructors have empty bodies, and the methods return a meaningless or intentionally wrong result (often, null). Once you have done this, use Eclipse to generate the JUnit 4 test class for you. If you step through the dialog boxes carefully, and make sure the right boxes are checked, Eclipse will generate test stub methods for all of your constructors and methods. Then all you have to do is fill in the test code.

Please use TDD--Test Driven Design. That's where you write the test first, run it to make sure it fails, then write the method, and run the test again to make sure it succeeds. Since you have to write both the method and its test anyway, you might as well do it in the correct order. To begin, test and write both a constructor and the equals method, because these are needed for everything else.

Due date

Turn in your one zip file to Canvas by 6am Wednesday, October 21.

Notes on submitting your work: