|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.lang.Number hw3.RatNum
public final class RatNum
RatNum represents an immutable rational number. It includes all of the elements in the set of rationals, as well as the special "NaN" (not-a-number) element that results from division by zero.
The "NaN" element is special in many ways. Any arithmetic operation (such as addition) involving "NaN" will return "NaN". With respect to comparison operations, such as less-than, "NaN" is considered equal to itself, and larger than all other rationals.
Examples of RatNums include "-1/13", "53/7", "4", "NaN", and "0".
Field Summary | |
---|---|
static RatNum |
NaN
A constant holding a Not-a-Number (NaN) value of type RatNum |
static RatNum |
ZERO
A constant holding a zero value of type RatNum |
Constructor Summary | |
---|---|
RatNum(int n)
|
|
RatNum(int n,
int d)
|
Method Summary | |
---|---|
RatNum |
add(RatNum arg)
Addition operation. |
int |
compareTo(RatNum rn)
Compares two RatNums. |
RatNum |
div(RatNum arg)
Division operation. |
double |
doubleValue()
Approximates the value of this rational. |
boolean |
equals(Object obj)
Standard equality operation. |
float |
floatValue()
Returns a float approximation for this. |
int |
hashCode()
Standard hashCode function. |
int |
intValue()
Returns an integer approximation for this. |
boolean |
isNaN()
Returns true if this is NaN |
boolean |
isNegative()
Returns true if this is negative. |
boolean |
isPositive()
Returns true if this is positive. |
long |
longValue()
Returns a long approximation for this. |
RatNum |
mul(RatNum arg)
Multiplication operation. |
RatNum |
negate()
Returns the additive inverse of this RatNum. |
RatNum |
sub(RatNum arg)
Subtraction operation. |
String |
toString()
|
static RatNum |
valueOf(String ratStr)
Makes a RatNum from a string describing it. |
Methods inherited from class java.lang.Number |
---|
byteValue, shortValue |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final RatNum NaN
public static final RatNum ZERO
Constructor Detail |
---|
public RatNum(int n)
public RatNum(int n, int d)
Method Detail |
---|
public boolean isNaN()
public boolean isNegative()
public boolean isPositive()
public int compareTo(RatNum rn)
compareTo
in interface Comparable<RatNum>
public double doubleValue()
doubleValue
in class Number
Double.NaN
, and the Double.NaN
value
is treated in a special manner by several arithmetic operations,
such as the comparison and equality operators. See the
Java Language Specification, section 4.2.3, for more details.public int intValue()
intValue
in class Number
public float floatValue()
floatValue
in class Number
public long longValue()
longValue
in class Number
public RatNum negate()
public RatNum add(RatNum arg)
public RatNum sub(RatNum arg)
public RatNum mul(RatNum arg)
public RatNum div(RatNum arg)
public int hashCode()
hashCode
in class Object
public boolean equals(Object obj)
equals
in class Object
public String toString()
toString
in class Object
public static RatNum valueOf(String ratStr)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |