EDU.oswego.cs.dl.util.concurrent.misc

Class Fraction

Implemented Interfaces:
Cloneable, Comparable, Serializable

public class Fraction
extends Object
implements Cloneable, Comparable, Serializable

An immutable class representing fractions as pairs of longs. Fractions are always maintained in reduced form.

See Also:
Serialized Form

Field Summary

protected long
denominator_
protected long
numerator_

Constructor Summary

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 *
Object
clone()
int
compareTo(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.
int
compareTo(long n)
return a number less, equal, or greater than zero reflecting whether this Fraction is less, equal or greater than n.
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(Object other)
boolean
equals(long n)
static long
gcd(long a, long b)
Compute the nonnegative greatest common divisor of a and b.
int
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 *
String
toString()

Field Details

denominator_

protected final long denominator_


numerator_

protected final long numerator_

Constructor Details

Fraction

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


Fraction

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

Method Details

asDouble

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


clone

public Object clone()


compareTo

public int compareTo(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.


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.


denominator

public final long denominator()
Return the denominator *


dividedBy

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


dividedBy

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


equals

public boolean equals(Object other)


equals

public boolean equals(long n)


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.)


hashCode

public int hashCode()


inverse

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


minus

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


minus

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


negative

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


numerator

public final long numerator()
Return the numerator *


plus

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


plus

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


times

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


times

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


toString

public String toString()