net.sourceforge.webcompmath.data
Class Fraction

java.lang.Object
  extended by net.sourceforge.webcompmath.data.Fraction
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Comparable

public class Fraction
extends java.lang.Object
implements java.lang.Cloneable, java.lang.Comparable, java.io.Serializable

An immutable class representing fractions as pairs of longs. Fractions are always maintained in reduced form. Invalid fractions have a denominator of 0.

See Also:
Serialized Form

Field Summary
protected  long denominator_
          The denominator
protected  long numerator_
          The numerator
 
Constructor Summary
Fraction(double d)
          Try to create a fraction from a real number.
Fraction(Fraction f)
          Create a fraction with the same value as Fraction f
Fraction(long num, long den)
          Create a Fraction equal in value to num / den
 
Method Summary
 double asDouble()
          Return the value of the Fraction as a double
 java.lang.Object clone()
          Clone the object
 int compareTo(long n)
          return a number less, equal, or greater than zero reflecting whether this Fraction is less, equal or greater than n.
 int compareTo(java.lang.Object other)
          return a number less, equal, or greater than zero reflecting whether this Fraction is less, equal or greater than the value of Fraction other.
 long denominator()
          Return the denominator
 Fraction dividedBy(Fraction b)
          return a Fraction representing this Fraction divided by b
 Fraction dividedBy(long n)
          return a Fraction representing this Fraction divided by n
 boolean equals(long n)
          Checks if a Fraction is equal to a number
 boolean equals(java.lang.Object other)
          Checks if two Fractions are equal
static long gcd(long a, long b)
          Compute the nonnegative greatest common divisor of a and b.
 int hashCode()
          Implements hashcode
 Fraction inverse()
          return a Fraction representing 1 / this Fraction
 Fraction minus(Fraction b)
          return a Fraction representing this Fraction minus b
 Fraction minus(long n)
          return a Fraction representing this Fraction minus n
 Fraction negative()
          return a Fraction representing the negated value of this Fraction
 long numerator()
          Return the numerator
 Fraction plus(Fraction b)
          return a Fraction representing this Fraction plus b
 Fraction plus(long n)
          return a Fraction representing this Fraction plus n
 Fraction times(Fraction b)
          return a Fraction representing this Fraction times b
 Fraction times(long n)
          return a Fraction representing this Fraction times n
 java.lang.String toString()
          Return a string representation of the fraction
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

numerator_

protected final long numerator_
The numerator


denominator_

protected final long denominator_
The denominator

Constructor Detail

Fraction

public Fraction(long num,
                long den)
Create a Fraction equal in value to num / den

Parameters:
num - the numerator
den - the denominator

Fraction

public Fraction(Fraction f)
Create a fraction with the same value as Fraction f

Parameters:
f - the fraction to duplicate

Fraction

public Fraction(double d)
Try to create a fraction from a real number. If can't, then the denominator is set to 0

Parameters:
d - the real number to convert to a fraction
Method Detail

numerator

public final long numerator()
Return the numerator

Returns:
the numerator

denominator

public final long denominator()
Return the denominator

Returns:
the denominator

toString

public java.lang.String toString()
Return a string representation of the fraction

Overrides:
toString in class java.lang.Object
See Also:
Object.toString()

clone

public java.lang.Object clone()
Clone the object

Overrides:
clone in class java.lang.Object
See Also:
Object.clone()

asDouble

public double asDouble()
Return the value of the Fraction as a double

Returns:
the real number value

gcd

public static long gcd(long a,
                       long b)
Compute the nonnegative greatest common divisor of a and b. (This is needed for normalizing Fractions, but can be useful on its own.)

Parameters:
a - the first number
b - the other number
Returns:
the gcd

negative

public Fraction negative()
return a Fraction representing the negated value of this Fraction

Returns:
negated fraction

inverse

public Fraction inverse()
return a Fraction representing 1 / this Fraction

Returns:
the reciprocal fraction

plus

public Fraction plus(Fraction b)
return a Fraction representing this Fraction plus b

Parameters:
b - the fraction to add to this
Returns:
the summed fraction

plus

public Fraction plus(long n)
return a Fraction representing this Fraction plus n

Parameters:
n - the number to add to the fraction
Returns:
the summed fraction

minus

public Fraction minus(Fraction b)
return a Fraction representing this Fraction minus b

Parameters:
b - the fraction to subtract from this
Returns:
the difference

minus

public Fraction minus(long n)
return a Fraction representing this Fraction minus n

Parameters:
n - the number to subtract
Returns:
the difference

times

public Fraction times(Fraction b)
return a Fraction representing this Fraction times b

Parameters:
b - the fraction to multiply by
Returns:
the product

times

public Fraction times(long n)
return a Fraction representing this Fraction times n

Parameters:
n - the number to muplitply by
Returns:
the product

dividedBy

public Fraction dividedBy(Fraction b)
return a Fraction representing this Fraction divided by b

Parameters:
b - the fraction to divide by
Returns:
the quotient

dividedBy

public Fraction dividedBy(long n)
return a Fraction representing this Fraction divided by n

Parameters:
n - the number to divide by
Returns:
the quotient

compareTo

public int compareTo(java.lang.Object other)
return a number less, equal, or greater than zero reflecting whether this Fraction is less, equal or greater than the value of Fraction other.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
other - the fraction to compare with
Returns:
number indicating the comparison

compareTo

public int compareTo(long n)
return a number less, equal, or greater than zero reflecting whether this Fraction is less, equal or greater than n.

Parameters:
n - the number to compare with
Returns:
number indicating the comparison

equals

public boolean equals(java.lang.Object other)
Checks if two Fractions are equal

Overrides:
equals in class java.lang.Object
See Also:
Object.equals(java.lang.Object)

equals

public boolean equals(long n)
Checks if a Fraction is equal to a number

Parameters:
n - the number to compare
Returns:
true or false

hashCode

public int hashCode()
Implements hashcode

Overrides:
hashCode in class java.lang.Object
See Also:
Object.hashCode()