Class Fraction

java.lang.Object
  extended by Fraction

public class Fraction
extends java.lang.Object

Maintains a fraction of the form n/d where n and d are integers.


Author Steve Weiss


Field Summary
private  int den
          The denominator of the fraction.
private  int num
          The numerator of the fraction.
 
Constructor Summary
Fraction()
          Default constructor: set fraction to 0/1.
Fraction(int n)
          One parameter constructor: set fraction to n/1.
Fraction(int n, int d)
          Two parameter constructor: set fraction to n/d.
 
Method Summary
 Fraction addFraction(Fraction f)
          Fraction adder.
private  int gcd(int i, int j)
          Greatest common devisor of i and j.
 int getDen()
          Denominator reader.
 int getNum()
          Numerator reader.
 double getValue()
          Get the (double) value of the fraction.
private  void reduce()
          Reduct the fraction to canonical form.
 void setFraction(int n, int d)
          Fraction writer: set fraction to n/d.
 java.lang.String toString()
          Return a pretty String version of the fraction.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

num

private int num
The numerator of the fraction.


den

private int den
The denominator of the fraction.

Constructor Detail

Fraction

Fraction()
Default constructor: set fraction to 0/1.


Fraction

Fraction(int n)
One parameter constructor: set fraction to n/1.

Parameters:
n - the numerator of the fraction.

Fraction

Fraction(int n,
         int d)
Two parameter constructor: set fraction to n/d.

Parameters:
n - the numerator of the fraction.
d - the denominator of the fraction.
Method Detail

gcd

private int gcd(int i,
                int j)
Greatest common devisor of i and j.

Parameters:
i - An integer
j - Another integer
Returns:
The greatest integer that evenly divides i and j.

reduce

private void reduce()
Reduct the fraction to canonical form. The numerator and denominator are divided by their gcd, and either both numerator and denominator are positive (positive fraction), or the numerator is negative and the denominator is positive (negative fraction)


setFraction

public void setFraction(int n,
                        int d)
Fraction writer: set fraction to n/d.


getNum

public int getNum()
Numerator reader.


getDen

public int getDen()
Denominator reader.


getValue

public double getValue()
Get the (double) value of the fraction.


toString

public java.lang.String toString()
Return a pretty String version of the fraction.

Overrides:
toString in class java.lang.Object

addFraction

public Fraction addFraction(Fraction f)
Fraction adder.

Returns:
a new Fraction object whose value is the sum of this fraction and f.