de.laliluna.date
Class Fraction

java.lang.Object
  extended by de.laliluna.date.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.

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
 Fraction abs()
           
 double asDouble()
          Return the value of the Fraction as a double *
 java.lang.Object clone()
           
 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 *
 Fraction dividedBy(long num, long den)
          Creates a fraction from the passed num and den value and divides the current value by this fraction.
 boolean equals(long n)
           
 boolean equals(java.lang.Object o)
           
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 minus(long num, long den)
          Creates a fraction from the passed num and den value and subtracts it.
 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 plus(long num, long den)
          Creates a fraction from the passed num and den value and adds it.
 Fraction times(Fraction b)
          return a Fraction representing this Fraction times b *
 Fraction times(long n)
          return a Fraction representing this Fraction times n *
 Fraction times(long num, long den)
          Creates a fraction from the passed num and den value and multiplies it with the current value.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

numerator

protected final long numerator

denominator

protected final long denominator
Constructor Detail

Fraction

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


Fraction

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

Method Detail

numerator

public final long numerator()
Return the numerator *


denominator

public final long denominator()
Return the denominator *


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object

asDouble

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


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


negative

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


inverse

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


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 *


plus

public Fraction plus(long num,
                     long den)
Creates a fraction from the passed num and den value and adds it.

Parameters:
num - - numinator of a fraction
den - - denominator of a fraction
Returns:
total of this and the passed 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 *


minus

public Fraction minus(long num,
                      long den)
Creates a fraction from the passed num and den value and subtracts it.

Parameters:
num - - numinator of a fraction
den - - denominator of a fraction
Returns:
difference of this and the passed fraction

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 *


times

public Fraction times(long num,
                      long den)
Creates a fraction from the passed num and den value and multiplies it with the current value.

Parameters:
num - - numinator of a fraction
den - - denominator of a fraction
Returns:
this multiplied with the passed fraction

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 *


dividedBy

public Fraction dividedBy(long num,
                          long den)
Creates a fraction from the passed num and den value and divides the current value by this fraction.

Parameters:
num - - numinator of a fraction
den - - denominator of a fraction
Returns:
this divided by the passed fraction

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

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.


equals

public boolean equals(long n)

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

abs

public Fraction abs()


Copyright © 2009 Sebastian Hennebrueder, http://www.laliluna.de