myutil
Class Fraction

java.lang.Object
  extended by myutil.Fraction

public class Fraction
extends java.lang.Object

An instance of this class represents a fraction.


Field Summary
private  int denominator
          the denominator of this fraction
private  int numerator
          the numerator of this fraction
 
Constructor Summary
Fraction()
          Creates a fraction 0/1
Fraction(Fraction frac)
          Creates a copy of frac
Fraction(int number)
          Creates a fraction number/1
Fraction(int num, int denom)
          Creates a fraction num/denom.
 
Method Summary
 Fraction add(Fraction frac)
          Returns the sum of this Fraction and the parameter frac.
 Fraction add(int number)
          Returns the sum of this Fraction and the int parameter number.
private  double decimal()
          Returns the decimal equivalent of this fraction
 Fraction divide(Fraction frac)
          Returns the quotient of this Fraction divided by the parameter frac.
 Fraction divide(int number)
          Returns the quotient of this Fraction divided by the int parameter number.
 boolean equals(Fraction frac)
          Compares this fraction and the parameter frac for equality.
static int gcd(int m, int n)
          Returns the greatest common divisor of the parameters m and n
 int getDenominator()
          Returns the denominator of this fraction
 int getNumerator()
          Returns the numerator of this fraction
static Fraction min(Fraction f1, Fraction f2)
          Returns the smaller of the two parameters f1 and f2
 Fraction multiply(Fraction frac)
          Returns the product of this Fraction and the parameter frac.
 Fraction multiply(int number)
          Returns the product of this Fraction and the int parameter number.
 void setDenominator(int denom)
          Sets the denominator of this fraction
 void setNumerator(int num)
          Sets the numerator of this fraction
 Fraction simplify()
          Returns a new Fraction object that is in the simplest form of this Fraction object.
 Fraction subtract(Fraction frac)
          Returns the difference of this Fraction and the parameter frac.
 Fraction subtract(int number)
          Returns the difference of this Fraction and the int parameter number.
 java.lang.String toString()
          Returns the String representation of this Fraction
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

numerator

private int numerator
the numerator of this fraction


denominator

private int denominator
the denominator of this fraction

Constructor Detail

Fraction

public Fraction()
Creates a fraction 0/1


Fraction

public Fraction(int number)
Creates a fraction number/1

Parameters:
number - the numerator

Fraction

public Fraction(Fraction frac)
Creates a copy of frac

Parameters:
frac - a copy of this parameter is created

Fraction

public Fraction(int num,
                int denom)
Creates a fraction num/denom. Create a negative fraction as -num and denom. If negative values are specified for both num and denom, the fraction is converted to a positive. If num is positive and denom is negative, the fraction will be converted to have negative num and positive denom. When the num is zero, denom is set to 1. Zero is always represented as 0/1

Parameters:
num - the numerator
denom - the denominator
Method Detail

gcd

public static int gcd(int m,
                      int n)
Returns the greatest common divisor of the parameters m and n

Parameters:
m - the first number
n - the second number
Returns:
the greatest common divisor of m and n

min

public static Fraction min(Fraction f1,
                           Fraction f2)
Returns the smaller of the two parameters f1 and f2

Parameters:
f1 - the first fraction to compare
f2 - the second fraction to compare
Returns:
the smaller of the two parameters

add

public Fraction add(Fraction frac)
Returns the sum of this Fraction and the parameter frac. The sum returned is NOT simplified.

Parameters:
frac - the Fraction to add to this Fraction
Returns:
the sum of this and frac

add

public Fraction add(int number)
Returns the sum of this Fraction and the int parameter number. The sum returned is NOT simplified.

Parameters:
number - the integer to add to this Fraction
Returns:
the sum of this Fraction and number

divide

public Fraction divide(Fraction frac)
Returns the quotient of this Fraction divided by the parameter frac. The quotient returned is NOT simplified.

Parameters:
frac - the divisor of the division
Returns:
the quotient of this fraction divided by frac

divide

public Fraction divide(int number)
Returns the quotient of this Fraction divided by the int parameter number. The quotient returned is NOT simplified.

Parameters:
number - the divisor
Returns:
the quotient of this Fraction divided by number

equals

public boolean equals(Fraction frac)
Compares this fraction and the parameter frac for equality. This method compares the two by first reducing them to the simplest form.

Parameters:
frac - the fraction object to compare
Returns:
true if this Fraction object and frac are equal

getDenominator

public int getDenominator()
Returns the denominator of this fraction

Returns:
the denominator of this fraction

getNumerator

public int getNumerator()
Returns the numerator of this fraction

Returns:
the numerator of this fraction

multiply

public Fraction multiply(Fraction frac)
Returns the product of this Fraction and the parameter frac. The product returned is NOT simplified.

Parameters:
frac - the multiplier of the multiplication
Returns:
the product of this fraction and the parameter frac

multiply

public Fraction multiply(int number)
Returns the product of this Fraction and the int parameter number. The product returned is NOT simplified.

Parameters:
number - the muliplier
Returns:
the product of this Fraction and number

setDenominator

public void setDenominator(int denom)
Sets the denominator of this fraction

Parameters:
denom - the denominator of this fraction

setNumerator

public void setNumerator(int num)
Sets the numerator of this fraction

Parameters:
num - the numerator of this fraction

simplify

public Fraction simplify()
Returns a new Fraction object that is in the simplest form of this Fraction object. If this Fraction is zero, then a simple copy of it is returned.

Returns:
a Fraction object in the simplest form of this Fraction

subtract

public Fraction subtract(Fraction frac)
Returns the difference of this Fraction and the parameter frac. The difference returned is NOT simplified.

Parameters:
frac - the Fraction to subtract from this Fraction
Returns:
the difference of this and frac

subtract

public Fraction subtract(int number)
Returns the difference of this Fraction and the int parameter number. The difference returned is NOT simplified.

Parameters:
number - the int value to subtract
Returns:
the difference of this and number

toString

public java.lang.String toString()
Returns the String representation of this Fraction

Overrides:
toString in class java.lang.Object
Returns:
the String representation of this Fraction

decimal

private double decimal()
Returns the decimal equivalent of this fraction

Returns:
the decimal equivalent of this fraction