public final class Rational extends Number implements Comparable<Rational>
long
numerator and long
denominator.
Rational numbers are stored in reduced form with the sign stored with the numerator.
Rationals are immutable.
Adapted from sample code featured in
"Intro to Programming in Java: An Interdisciplinary Approach" (Addison Wesley)
by Robert Sedgewick and Kevin Wayne. Permission granted to redistribute under BSD license.Constructor and Description |
---|
Rational(long pNumber) |
Rational(long pNumerator,
long pDenominator) |
Modifier and Type | Method and Description |
---|---|
int |
compareTo(Rational pOther) |
long |
denominator() |
Rational |
divides(Rational pOther) |
double |
doubleValue() |
boolean |
equals(Object pOther) |
float |
floatValue() |
int |
hashCode() |
int |
intValue() |
long |
longValue() |
Rational |
minus(Rational pOther) |
Rational |
negate() |
long |
numerator() |
Rational |
plus(Rational pOther) |
Rational |
reciprocal() |
Rational |
times(Rational pOther) |
String |
toString() |
byteValue, shortValue
public Rational(long pNumber)
public Rational(long pNumerator, long pDenominator)
public long numerator()
public long denominator()
public float floatValue()
floatValue
in class Number
public double doubleValue()
doubleValue
in class Number
public int compareTo(Rational pOther)
compareTo
in interface Comparable<Rational>
public Rational negate()
public Rational reciprocal()
Copyright © 2017. All rights reserved.