public class Rational extends java.lang.Number implements java.lang.Comparable<Rational>, java.io.Serializable
toString() in form numerator/denominator.
Note that any value with a numerator of zero will be treated as zero, even if the
denominator is also zero.| Modifier and Type | Field and Description |
|---|---|
private long |
_denominator
Holds the denominator.
|
private long |
_numerator
Holds the numerator.
|
private static long |
serialVersionUID |
| Constructor and Description |
|---|
Rational(long numerator,
long denominator)
Creates a new instance of Rational.
|
| Modifier and Type | Method and Description |
|---|---|
byte |
byteValue()
Returns the value of the specified number as a
byte. |
int |
compareTo(Rational that)
Compares two
Rational instances, returning true if they are mathematically
equivalent (in consistence with equals(Object) method). |
double |
doubleValue()
Returns the value of the specified number as a
double. |
boolean |
equals(java.lang.Object obj)
Compares two
Rational instances, returning true if they are mathematically
equivalent. |
boolean |
equals(Rational other)
Indicates whether this instance and
other are numerically equal,
even if their representations differ. |
boolean |
equalsExact(Rational other)
Indicates whether this instance and
other have identical
Numerator and Denominator. |
float |
floatValue()
Returns the value of the specified number as a
float. |
private static long |
GCD(long a,
long b) |
long |
getDenominator()
Returns the denominator.
|
long |
getNumerator()
Returns the numerator.
|
Rational |
getReciprocal()
Returns the reciprocal value of this object as a new Rational.
|
Rational |
getSimplifiedInstance()
Simplifies the representation of this
Rational number. |
int |
hashCode() |
int |
intValue()
Returns the value of the specified number as an
int. |
boolean |
isInteger()
Checks if this
Rational number is an Integer, either positive or negative. |
boolean |
isZero()
Checks if either the numerator or denominator are zero.
|
long |
longValue()
Returns the value of the specified number as a
long. |
short |
shortValue()
Returns the value of the specified number as a
short. |
java.lang.String |
toSimpleString(boolean allowDecimal)
Returns the simplest representation of this
Rational's value possible. |
java.lang.String |
toString()
Returns a string representation of the object of form
numerator/denominator. |
private static final long serialVersionUID
private final long _numerator
private final long _denominator
public Rational(long numerator,
long denominator)
public double doubleValue()
double.
This may involve rounding.doubleValue in class java.lang.Numberdouble.public float floatValue()
float.
This may involve rounding.floatValue in class java.lang.Numberfloat.public final byte byteValue()
byte.
This may involve rounding or truncation. This implementation simply
casts the result of doubleValue() to byte.byteValue in class java.lang.Numberbyte.public final int intValue()
int.
This may involve rounding or truncation. This implementation simply
casts the result of doubleValue() to int.intValue in class java.lang.Numberint.public final long longValue()
long.
This may involve rounding or truncation. This implementation simply
casts the result of doubleValue() to long.longValue in class java.lang.Numberlong.public final short shortValue()
short.
This may involve rounding or truncation. This implementation simply
casts the result of doubleValue() to short.shortValue in class java.lang.Numbershort.public final long getDenominator()
public final long getNumerator()
public Rational getReciprocal()
public boolean isInteger()
Rational number is an Integer, either positive or negative.public boolean isZero()
public java.lang.String toString()
numerator/denominator.toString in class java.lang.Objectpublic java.lang.String toSimpleString(boolean allowDecimal)
Rational's value possible.public int compareTo(Rational that)
Rational instances, returning true if they are mathematically
equivalent (in consistence with equals(Object) method).compareTo in interface java.lang.Comparable<Rational>that - the Rational to compare this instance to.0 if this Rational is
equal to the argument Rational mathematically; a value less
than 0 if this Rational is less
than the argument Rational; and a value greater
than 0 if this Rational is greater than the argument
Rational.public boolean equals(Rational other)
other are numerically equal,
even if their representations differ.
For example, 1/2 is equal to 10/20 by this method.
Similarly, 1/0 is equal to 100/0 by this method.
To test equal representations, use EqualsExact.other - The rational value to compare withpublic boolean equalsExact(Rational other)
other have identical
Numerator and Denominator.
For example, 1/2 is not equal to 10/20 by this method. Similarly, 1/0 is not equal to 100/0 by this method. To test numerically equivalence, use Equals(Rational).
other - The rational value to compare withpublic boolean equals(java.lang.Object obj)
Rational instances, returning true if they are mathematically
equivalent.public int hashCode()
hashCode in class java.lang.Objectpublic Rational getSimplifiedInstance()
Simplifies the representation of this Rational number.
For example, 5/10 simplifies to 1/2 because both Numerator and Denominator share a common factor of 5.
Uses the Euclidean Algorithm to find the greatest common divisor.
private static long GCD(long a,
long b)