CIT 590 Assignment 7: Fractions
Fall 2015, David Matuszek

# Purposes of this assignment

• To get you started in Java
• To get you started in Eclipse

# General idea of the assignment

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

• `Fraction` -- An object of this class represents a fraction. The class contains methods for operating on the fraction.
• `FractionTest` -- A JUnit test class for the functions defined in `Fraction`.

# Arithmetic with Fractions

Components Two integers, known as the numerator and the denominator. `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. The denominator may not be zero. 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. `a/b + c/d` is `(ad + bc)/bd` `a/b - c/d` is `(ad - bc)/bd` `(a/b) * (c/d) `is `(a*c)/(b*d)` `(a/b) / (c/d)` is` (a*d)/(b*c)` Assuming that `a/b` is normalized: `a > 0 ? a / b : -a / b` `-(a/b)` is `-a/b` Inverse of `a/b` is `b/a` If in lowest terms, numerators are equal and denominators are equal. `a/b` is less than `c/d` if ```ad < bc ``` 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:

• A `public` constructor that takes two `int` parameters, a numerator and a denominator.
• A `public` constructor that takes one `int` parameter, the numerator (the denominator is assumed to be one)
• A `public` constructor that takes a `String` such as `"5/10"`, or `"2"` (allow blanks in the string).
• You can use `split("/")` to get the two parts, then `trim()` both parts, then use `Integer.parseInt(part)` to convert each part from type `String` to type `int`.

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:

• `public Fraction add(Fraction f)`
• `public Fraction subtract(Fraction f)`
• `public Fraction multiply(Fraction f)`
• `public Fraction divide(Fraction f)`
• `public Fraction abs()`
• `public Fraction negate()`
• `public Fraction inverse()`
• Taking the inverse of zero (`0/1`) should throw an `ArithmeticException`.
• `private void normalize() `
• Normalizes "`this`" fraction. Used by constructors; should not be needed elsewhere.
• ```@Override public boolean equals(Object o)```
• To get you started:
• You should return `false` if `o` isn't a `Fraction`: `if (!(o instanceof Fraction)) return false;`
• If `o` is a `Fraction`, you need to cast it and save it in a variable of type `Fraction`: `Fraction f = (Fraction)o;`
• ```@Override public int hashCode() ```
• Returns the produce of the numerator and the denominator. This is needed in case the Fraction is ever used in a set or a map.
• ```@Override public int compareTo(Fraction f) ```
• Compares this fraction with the specified fraction. Returns a negative integer, zero, or a positive integer as this fraction is less than, equal to, or greater than the specified fraction, respectively.
• ```@Override public String toString()```
• Returns a String of the form `n/d`, unless `d` is `1`, in which case it returns just `n`.

## 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 `Exception`s 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:

• Please put your name as a comment at the beginning of your program.
• Please do not use non-ASCII characters in your program or in the name of your program or in the name of the folder containing your program.
• If you are zipping an entire folder, rename the zipped folder to have your (ASCII) name as part of the name of the folder.
• Do not zip individual files, only folders or groups of files.