|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectnet.sourceforge.webcompmath.data.Fraction
public class Fraction
An immutable class representing fractions as pairs of longs. Fractions are always maintained in reduced form. Invalid fractions have a denominator of 0.
| 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 |
|---|
protected final long numerator_
protected final long denominator_
| Constructor Detail |
|---|
public Fraction(long num,
long den)
num - the numeratorden - the denominatorpublic Fraction(Fraction f)
f - the fraction to duplicatepublic Fraction(double d)
d - the real number to convert to a fraction| Method Detail |
|---|
public final long numerator()
public final long denominator()
public java.lang.String toString()
toString in class java.lang.ObjectObject.toString()public java.lang.Object clone()
clone in class java.lang.ObjectObject.clone()public double asDouble()
public static long gcd(long a,
long b)
a - the first numberb - the other number
public Fraction negative()
public Fraction inverse()
public Fraction plus(Fraction b)
b - the fraction to add to this
public Fraction plus(long n)
n - the number to add to the fraction
public Fraction minus(Fraction b)
b - the fraction to subtract from this
public Fraction minus(long n)
n - the number to subtract
public Fraction times(Fraction b)
b - the fraction to multiply by
public Fraction times(long n)
n - the number to muplitply by
public Fraction dividedBy(Fraction b)
b - the fraction to divide by
public Fraction dividedBy(long n)
n - the number to divide by
public int compareTo(java.lang.Object other)
compareTo in interface java.lang.Comparableother - the fraction to compare with
public int compareTo(long n)
n - the number to compare with
public boolean equals(java.lang.Object other)
equals in class java.lang.ObjectObject.equals(java.lang.Object)public boolean equals(long n)
n - the number to compare
public int hashCode()
hashCode in class java.lang.ObjectObject.hashCode()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||