Serializable, Comparable<Rational>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 | Description |
|---|---|
Rational(long pNumber) |
|
Rational(long pNumerator,
long pDenominator) |
| Modifier and Type | Method | 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, shortValuepublic Rational(long pNumber)
public Rational(long pNumerator,
long pDenominator)
public long numerator()
public long denominator()
public float floatValue()
floatValue in class Numberpublic double doubleValue()
doubleValue in class Numberpublic int compareTo(Rational pOther)
compareTo in interface Comparable<Rational>public Rational negate()
public Rational reciprocal()
Copyright © 2018. All rights reserved.