public class Natural
extends java.lang.Object
implements java.lang.Comparable
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.
The MIT License (MIT)
Copyright (c) 2013 John Dalbey
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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)
|
int |
hashCode()
Implementation of hashCode()
|
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.
|
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 negativejava.lang.NumberFormatException
- if max is negativejava.lang.NumberFormatException
- if val > maxpublic Natural add(Natural val)
val
- value to be added to this Natural.public void increment()
ArithmeticException()
- if this natural has a maximum value
and the result of incrementing exceeds the maximum value.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 int hashCode()
hashCode
in class java.lang.Object
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)