public final class Rational extends java.lang.Number implements java.lang.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(java.lang.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) |
java.lang.String |
toString() |
public Rational(long pNumber)
public Rational(long pNumerator,
long pDenominator)
public long numerator()
public long denominator()
public int intValue()
intValue in class java.lang.Numberpublic long longValue()
longValue in class java.lang.Numberpublic float floatValue()
floatValue in class java.lang.Numberpublic double doubleValue()
doubleValue in class java.lang.Numberpublic int compareTo(Rational pOther)
compareTo in interface java.lang.Comparable<Rational>public int hashCode()
hashCode in class java.lang.Objectpublic boolean equals(java.lang.Object pOther)
equals in class java.lang.Objectpublic java.lang.String toString()
toString in class java.lang.Objectpublic Rational negate()
public Rational reciprocal()
Copyright © 2020. All Rights Reserved.