Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Number
com.drew.lang.Rational
public class Rational
extends java.lang.Number
implements Serializable
numerator/denominator
.
Constructor Summary | |
|
Method Summary | |
byte |
|
double |
|
boolean |
|
float |
|
int |
|
int |
|
Rational |
|
Rational |
|
int |
|
boolean |
|
long |
|
short |
|
String |
|
String |
|
public Rational(int numerator, int denominator)
Creates a new instance of Rational. Rational objects are immutable, so once you've set your numerator and denominator values here, you're stuck with them!
public final byte byteValue()
Returns the value of the specified number as abyte
. This may involve rounding or truncation. This implementation simply casts the result ofdoubleValue()
tobyte
.
- Returns:
- the numeric value represented by this object after conversion to type
byte
.
public double doubleValue()
Returns the value of the specified number as adouble
. This may involve rounding.
- Returns:
- the numeric value represented by this object after conversion to type
double
.
public boolean equals(Object obj)
Compares twoRational
instances, returning true if they are mathematically equivalent.
- Parameters:
obj
- the Rational to compare this instance to.
- Returns:
- true if instances are mathematically equivalent, otherwise false. Will also return false if
obj
is not an instance ofRational
.
public float floatValue()
Returns the value of the specified number as afloat
. This may involve rounding.
- Returns:
- the numeric value represented by this object after conversion to type
float
.
public final int getDenominator()
Returns the denominator.
public final int getNumerator()
Returns the numerator.
public Rational getReciprocal()
Returns the reciprocal value of this obejct as a new Rational.
- Returns:
- the reciprocal in a new object
public Rational getSimplifiedInstance()
Simplifies the Rational number. Prime number series: 1, 2, 3, 5, 7, 9, 11, 13, 17 To reduce a rational, need to see if both numerator and denominator are divisible by a common factor. Using the prime number series in ascending order guarantees the minimun number of checks required. However, generating the prime number series seems to be a hefty task. Perhaps it's simpler to check if both d & n are divisible by all numbers from 2 -> (Math.min(denominator, numerator) / 2). In doing this, one can check for 2 and 5 once, then ignore all even numbers, and all numbers ending in 0 or 5. This leaves four numbers from every ten to check. Therefore, the max number of pairs of modulus divisions required will be:
4 Math.min(denominator, numerator) - 1 -- * ------------------------------------ + 2 10 2 Math.min(denominator, numerator) - 1 = ------------------------------------ + 2 5
- Returns:
- a simplified instance, or if the Rational could not be simpliffied, returns itself (unchanged)
public final int intValue()
Returns the value of the specified number as anint
. This may involve rounding or truncation. This implementation simply casts the result ofdoubleValue()
toint
.
- Returns:
- the numeric value represented by this object after conversion to type
int
.
public boolean isInteger()
Checks if this rational number is an Integer, either positive or negative.
public final long longValue()
Returns the value of the specified number as along
. This may involve rounding or truncation. This implementation simply casts the result ofdoubleValue()
tolong
.
- Returns:
- the numeric value represented by this object after conversion to type
long
.
public final short shortValue()
Returns the value of the specified number as ashort
. This may involve rounding or truncation. This implementation simply casts the result ofdoubleValue()
toshort
.
- Returns:
- the numeric value represented by this object after conversion to type
short
.
public String toSimpleString(boolean allowDecimal)
Returns the simplest represenation of this Rational's value possible.
public String toString()
Returns a string representation of the object of formnumerator/denominator
.
- Returns:
- a string representation of the object.