Serializable, Comparable<RationalNumber>public final class RationalNumber extends Number implements Comparable<RationalNumber>
| 构造器 | 说明 |
|---|---|
RationalNumber(int numerator,
int denominator) |
A constructor used to create a
RationalNumber instance with a given numerator and denominator. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
int |
compareTo(RationalNumber another) |
Compares the current
RationalNumber object with a given object. |
static RationalNumber |
createRationalFromString(String rationalString) |
Creates a
RationalNumber object based on a given string. |
double |
doubleValue() |
Obtains the value of the current
RationalNumber object as a double-precision floating point number. |
boolean |
equals(Object obj) |
Checks whether a given object is the same as the current
RationalNumber object. |
float |
floatValue() |
Obtains the value of the current
RationalNumber object as a floating point number. |
static int |
getCommonDivisor(int number1,
int number2) |
Obtains the greatest common divisor of two specified numbers.
|
int |
getDenominator() |
Obtains the denominator of the current
RationalNumber object. |
int |
getNumerator() |
Obtains the numerator of the current
RationalNumber object. |
int |
hashCode() |
Obtains the hash code value of the current
RationalNumber object. |
int |
intValue() |
Obtains the value of the current
RationalNumber object as an integer. |
boolean |
isFinite() |
Checks whether the current
RationalNumber object represents a finite value. |
boolean |
isInfinite() |
Checks whether the current
RationalNumber object represents an infinite value. |
boolean |
isNaN() |
Checks whether the current
RationalNumber object represents a Not-a-Number (NaN) value. |
boolean |
isZero() |
Checks whether the current
RationalNumber object represents the value 0. |
long |
longValue() |
Obtains the value of the current
RationalNumber object as a long integer. |
String |
toString() |
Obtains a string representation of the current
RationalNumber object. |
byteValue, shortValuepublic RationalNumber(int numerator,
int denominator)
RationalNumber instance with a given numerator and denominator.numerator - Indicates the numerator.denominator - Indicates the denominator.public static int getCommonDivisor(int number1,
int number2)
number1 - Indicates the first number.number2 - Indicates the second number.IllegalArgumentException - Throws this exception if the specified number1 or number2 is
Integer#MIN_VALUE.public boolean equals(Object obj)
RationalNumber object.equals 在类中 Objectobj - Indicates the object to compare.true if the given object is the same as the current object; returns false otherwise.Object.hashCode(),
HashMappublic int hashCode()
RationalNumber object.hashCode 在类中 ObjectRationalNumber object.Object.equals(java.lang.Object),
System.identityHashCode(java.lang.Object)public String toString()
RationalNumber object.toString 在类中 ObjectNaN if the numerator and denominator of the current object are both 0;PositiveInfinity if the denominator is 0 and the numerator is a positive number;NegativeInfinity if the denominator is 0 and the numerator is a negative number;<i>Numerator</i>/<i>Denominator</i>, for example, 3/5, in other circumstances.public double doubleValue()
RationalNumber object as a double-precision floating point number.doubleValue 在类中 Numberpublic float floatValue()
RationalNumber object as a floating point number.floatValue 在类中 Numberpublic int intValue()
RationalNumber object as an integer.public long longValue()
RationalNumber object as a long integer.public int getDenominator()
RationalNumber object.public int getNumerator()
RationalNumber object.public boolean isFinite()
RationalNumber object represents a finite value.true if the denominator is not 0; returns false otherwise.public boolean isInfinite()
RationalNumber object represents an infinite value.true if the denominator is 0 and the numerator is not 0; returns false otherwise.public boolean isNaN()
RationalNumber object represents a Not-a-Number (NaN) value.true if the denominator and numerator are both 0; returns false otherwise.public boolean isZero()
RationalNumber object represents the value 0.true if the value represented by the current object is 0; returns false otherwise.public static RationalNumber createRationalFromString(String rationalString) throws NumberFormatException
RationalNumber object based on a given string.
The given string must be in the format <i>Numerator</i>/<i>Denominator</i>, for example, 3/5.
To create an object representing an NaN value, pass NaN to this method.
To create an object representing a positive infinity value, pass PositiveInfinity.
To create an object representing a negative infinity value, pass NegativeInfinity.
rationalString - Indicates the string used to create the RationalNumber object.RationalNumber object generated based on the given string.NullPointerException - Throws this exception if the rationalString is null.NumberFormatException - Throws this exception if the rationalString is illegal.public int compareTo(RationalNumber another)
RationalNumber object with a given object.compareTo 在接口中 Comparable<RationalNumber>another - Indicates the object to compare.0 if the two objects are equal; returns 1 if the given object is less than the current object;
returns -1 if the given object is greater than the current object.NullPointerException - Throws this exception if the another is null.