java.lang.Object Natural
public class Natural
A natural number is a non-negative integer. All operations behave like Java's primitive integer types. Natural provides analogues to most of Java's primitive integer operators, and most relevant methods from java.lang.Math. Additionally, Natural provides operations for modular arithmetic.
Semantics of arithmetic operations exactly mimic those of Java's integer arithmetic operators, as defined in The Java Language Specification. For example, division by zero throws an ArithmeticException, and division of a negative by a positive yields a negative (or zero) remainder.
Comparison operations perform signed integer comparisons, analogous to those performed by Java's relational and equality operators.
For the sake of brevity and clarity, pseudo-code is used throughout the descriptions of Natural methods. The pseudo-code expression (i + j) is shorthand for "a Natural whose value is that of the Natural i plus that of the Natural j." The pseudo-code expression (i == j) is shorthand for "true if and only if the Natural i represents the same value as the the Natural j." Other pseudo-code expressions are interpreted similarly.
Constructor Summary | |
---|---|
Natural(int val)
Constructs a Natural with the specified int value. |
|
Natural(int val,
int max)
Constructs a Natural with the specified int value and maximum. |
|
Natural(java.lang.String val)
Translates the decimal String representation of a Natural into a Natural. |
Method Summary | |
---|---|
Natural |
add(Natural val)
Returns a Natural whose value is (this + val). |
int |
compareTo(int val)
Compares this Natural with the specified integer. |
int |
compareTo(Natural val)
Compares this Natural with the specified Natural. |
int |
compareTo(java.lang.Object x)
Same as compareTo() above, just taking an object as parameter. |
void |
decrement()
Subtracts one from this Natural. |
Natural |
divide(Natural val)
Returns a Natural whose value is (this / val). |
double |
doubleValue()
Converts this Natural to a double. |
boolean |
equals(java.lang.Object x)
Compares this Natural with the specified Object for equality. |
float |
floatValue()
Converts this Natural to a float. |
int |
getMax()
Returns the max value of the natural number (-1 means no max) |
void |
increment()
Adds one to this Natural. |
int |
intValue()
Converts this Natural to an int. |
long |
longValue()
Converts this Natural to a long. |
static void |
main(java.lang.String[] args)
A local test driver |
Natural |
max(Natural val)
Returns the maximum of this Natural and val. |
Natural |
min(Natural val)
Returns the minimum of this Natural and val. |
Natural |
multiply(Natural val)
Returns a Natural whose value is (this * val). |
Natural |
pow(int exponent)
Returns a Natural whose value is (thisexponent). |
Natural |
remainder(Natural val)
Returns a Natural whose value is (this % val). |
Natural |
square()
Returns a Natural whose value is (this2). |
Natural |
subtract(Natural val)
Returns a Natural whose value is (this - val). |
java.lang.String |
toString()
Returns the decimal String representation of this Natural. |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public Natural(java.lang.String val)
val
- decimal String representation of Natural.
java.lang.NumberFormatException
- val is not a valid representation
of a Natural.Character.digit(char, int)
public Natural(int val)
val
- int representation of Natural.
java.lang.NumberFormatException
- if val is negativepublic Natural(int val, int max)
val
- int representation of Natural.max
- int value that is the max this number can take.
java.lang.NumberFormatException
- if val is negative
java.lang.NumberFormatException
- if max is negativeMethod Detail |
---|
public Natural add(Natural val)
val
- value to be added to this Natural.
public void increment()
public Natural subtract(Natural val)
val
- value to be subtracted from this Natural.
java.lang.ArithmeticException
- if result would be negative.public void decrement()
java.lang.ArithmeticException
- if result would be negative.public Natural multiply(Natural val)
val
- value to be multiplied by this Natural.
public Natural square()
public Natural divide(Natural val)
val
- value by which this Natural is to be divided.
java.lang.ArithmeticException
- val==0public Natural remainder(Natural val)
val
- value by which this Natural is to be divided, and the
remainder computed.
java.lang.ArithmeticException
- val==0public Natural pow(int exponent)
exponent
- exponent to which this Natural is to be raised.
java.lang.ArithmeticException
- exponent is negative. (This would
cause the operation to yield a non-integer value.)public int compareTo(Natural val)
val
- Natural to which this Natural is to be compared.
public int compareTo(int val)
val
- int to which this Natural is to be compared.
public int compareTo(java.lang.Object x)
compareTo
in interface java.lang.Comparable
public boolean equals(java.lang.Object x)
equals
in class java.lang.Object
x
- Object to which this Natural is to be compared.
public Natural min(Natural val)
val
- value with with the minimum is to be computed.
public Natural max(Natural val)
val
- value with with the maximum is to be computed.
public java.lang.String toString()
toString
in class java.lang.Object
Character.forDigit(int, int)
public int intValue()
public long longValue()
public float floatValue()
public double doubleValue()
public int getMax()
-1if there is no max, otherwise the max
public static void main(java.lang.String[] args)