# Assignment 4: Testing the Fraction Class

Assigned: Friday, February 12, 1999
Due: Friday, February 17, 1999

You are permitted to work in groups of up to size 3.

Required files:

• `Fraction.java`

1. Create a small application with a graphical user interface that can be used to test the `Fraction` class. At minimum, your application should allow the user to enter two fractions, select an operation, and see the result.

2. Update the `simplify` method in the fraction class to simplify fractions. For example, for the fraction 2/8, when simplified, is 1/4. Similarly, the fraction 6/21, when simplified, is 2/7.

3. Add methods that make it possible to add an integer to a fraction, subtract an integer from a fraction, multiply a fraction by an integer, and divide a fraction by an integer. All of these should return a fraction.

4. Divise an appropriate test plan for the `Fraction` class and, using your application from step 1, carry out that test plan.

Here is the code for the `Fraction` class.

```/**
* A simple (and partial) implementation of fractions.
*
* @author Samuel A. Rebelsky
* @version 1.0 of February 1999
*/
public class Fraction {
// +--------+--------------------------------------------------
// | Fields |
// +--------+

/** The numerator. */
protected long num;
/** The denominator. */
protected long denom;

// +--------------+--------------------------------------------
// | Constructors |
// +--------------+

/**
* Create the fraction a/b (simplifying if necessary).
*/
public Fraction(long a, long b) {
this.num = a;
this.denom = b;
this.simplify();
} // Fraction(long,long)

/**
* Create the ``zero'' fraction (0/1 for convenience).
*/
public Fraction() {
this.num = 0;
this.denom = 1;
} // Fraction()

/**
* Create a ``whole'' fraction (the fraction corresponding
* to an integer).
*/
public Fraction(long val) {
this.num = val;
this.denom = 1;
} // Fraction(long)

// +------------------+----------------------------------------
// | Standard Methods |
// +------------------+

/**
* Determine if this fraction equals another fraction.
*/
public boolean equals(Fraction other) {
// A denominator of 0 is problematical.  If both are 0, we'll
// assume that they're equal.
if ((this.denom == 0) && (other.denom == 0)) {
return true;
}
// If only one denominator is 0, we'll assume they're different.
else if ((this.denom == 0) || (other.denom == 0)) {
return false;
}
// a/b = c/d if a*d=b*c.  (There's some disadvantage to this
// technique because a*c or b*d can overflow.)
else {
return (this.num*other.denom) == (other.num*this.denom);
}
} // equals(Fraction)

/**
* Determine if this fraction equals another object.
*/
public boolean equals(Object other) {
return (other instanceof Fraction) && (equals((Fraction) other));
} // equals(Object)

/**
* Convert this fraction to a string.
*/
public String toString() {
// Special case: denominator is 0.  Return an error string.
if (this.denom == 0) {
return "Not a number";
}
// Special case: denominator is 1.  Return just the numerator.
else if (this.denom == 1) {
return Long.toString(this.num);
}
// Normal case.  Return the fraction in the form a/b.
else  {
return (this.num + "/" + this.denom);
}
} // toString()

// +--------------------+--------------------------------------
// +--------------------+

/**
* Compute the fraction that results from adding this fraction
* to another fraction.
*
* a/b + c/d = (a*d + b*c)/(b*d)
*/
return new Fraction(
this.num*other.denom + other.num*this.denom,
this.denom*other.denom);

/**
* Compute the fraction that results from dividing this
* fraction by another fraction.
*
* (a/b)/(c/d) = a/b * d/c = (a*d)/(b*c)
*/
public Fraction divide(Fraction other) {
return new Fraction(
this.num*other.denom,
this.denom*other.num);
} // divide(Fraction)

/**
* Compute the fraction that results from multiplying this
* fraction by another fraction.
*
* a/b * c/d = (a*c)/(b*d)
*/
public Fraction multiply(Fraction other) {
return new Fraction(
this.num*other.num,
this.denom*other.denom);
} // multiply(Fraction)

/**
* Compute the fraction that results from subtracting another
* fraction from this fraction.
*
* a/b - c/d = (a*d - b*c)/(b*d)
*/
public Fraction subtract(Fraction other) {
return new Fraction(
this.num*other.denom - other.num*this.denom,
this.denom*other.denom);
} // subtract(Fraction)

// +----------------+------------------------------------------
// | Helper Methods |
// +----------------+

/**
* Compute the greatest common divisor of nonnegative integers
* a and b.  May be useful in simplifying a fraction.  Uses a
* legendary algorithm.  Untested.
*/
protected int gcd(int a, int b) {
// Special case, required by the algorithm.  If b is 0, then
// return a.
if (b == 0) { return a; }
// Other case.
else { return gcd(b, a%b); }
} // gcd(int,int)

/**
* Simplify the current fraction.  Currently unimplemented.
*/
protected void simplify() {
} // simplify()

} // class Fraction
```

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.