EDU.ksu.cis.calculator
Class LargeInteger

java.lang.Object
  |
  +--EDU.ksu.cis.calculator.LargeInteger
All Implemented Interfaces:
Comparable

public final class LargeInteger
extends Object
implements Comparable

A class which implements aribitrary-precision integer arithmetic. LargeIntegers are stored in any radix within the range 2-36, and radix conversions are done only when necessary; i.e., computations involving LargeIntegers of the same radix are done within that radix. Thus, if inputs and outputs are all to be in decimal, all of the computations are done in decimal, thereby saving expensive radix conversions.

Author:
Rod Howell (howell@cis.ksu.edu)

Constructor Summary
LargeInteger(String s)
          Constructs a new LargeInteger in base 10 from the given String.
LargeInteger(String s, int b)
          Constructs a new LargeInteger in the given base from the given String.
 
Method Summary
 LargeInteger abs()
           
 LargeInteger add(LargeInteger other)
          Returns the sum of this LargeInteger and the given LargeInteger.
 int compareTo(Object o)
          Compares this LargeInteger with the given Object.
 LargeInteger[] divide(int a)
          Returns an array whose first element is this / a and whose second element is this % a.
 LargeInteger[] divide(LargeInteger other)
          Returns a 2-element array whose first element is this / other and whose second element is this % other.
 int getBase()
           
 LargeInteger multiply(int a)
          Returns the product of this LargeInteger and a.
 LargeInteger multiply(LargeInteger other)
          Returns the product of this LargeInteger and the given LargeInteger.
 LargeInteger negate()
          Returns the LargeInteger obtained by changing the sign of this LargeInteger.
 LargeInteger pow(int exp)
          Returns this LargeInteger raised to the power exp.
static LargeInteger pow(int a, int exp, int db)
          Returns the LargeInteger aexp, stored in base db.
static LargeInteger pow(int a, LargeInteger exp)
          Returns a raised to the power exp.
 LargeInteger pow(LargeInteger other)
          Returns this LargeInteger raised to the power other.
 LargeInteger subtract(LargeInteger other)
          Returns the difference of this LargeInteger and the given LargeInteger.
 LargeInteger toBase(int b)
          Converts this LargeInteger to the given base.
 String toString()
          Returns the String representation of this LargeInteger.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LargeInteger

public LargeInteger(String s)
             throws NumberFormatException
Constructs a new LargeInteger in base 10 from the given String.

Throws:
NumberFormatException - if s does not encode a decimal integer.

LargeInteger

public LargeInteger(String s,
                    int b)
             throws NumberFormatException
Constructs a new LargeInteger in the given base from the given String.

Throws:
NumberFormatException - if s does not encode an integer in the given base, or if the given base is less than 2 or greater than 36.
Method Detail

compareTo

public int compareTo(Object o)
              throws ClassCastException
Compares this LargeInteger with the given Object.

Specified by:
compareTo in interface Comparable
Returns:
a negative integer, zero, or a positive integer as this LargeInteger is less than, equal to, or greater than the specified object.
Throws:
ClassCastException - if the specified object is not a LargeInteger.

add

public LargeInteger add(LargeInteger other)
Returns the sum of this LargeInteger and the given LargeInteger.


subtract

public LargeInteger subtract(LargeInteger other)
Returns the difference of this LargeInteger and the given LargeInteger.


negate

public LargeInteger negate()
Returns the LargeInteger obtained by changing the sign of this LargeInteger.


multiply

public LargeInteger multiply(LargeInteger other)
Returns the product of this LargeInteger and the given LargeInteger.


divide

public LargeInteger[] divide(LargeInteger other)
                      throws ArithmeticException
Returns a 2-element array whose first element is this / other and whose second element is this % other. Thus, if the two returned values are q and r, respectively, the following hold:

Throws:
ArithmeticException - if other is 0.

abs

public LargeInteger abs()

toBase

public LargeInteger toBase(int b)
                    throws NumberFormatException
Converts this LargeInteger to the given base.

Throws:
NumberFormatException - If b < 2 or b > 36.

pow

public LargeInteger pow(LargeInteger other)
                 throws ArithmeticException
Returns this LargeInteger raised to the power other. Note that it is very easy to generate an OutOfMemoryError.

Throws:
ArithmeticException - If other is negative.

pow

public LargeInteger pow(int exp)
                 throws ArithmeticException
Returns this LargeInteger raised to the power exp.

Throws:
ArithmeticException - If exp is negative.

pow

public static LargeInteger pow(int a,
                               LargeInteger exp)
                        throws ArithmeticException
Returns a raised to the power exp.

Throws:
ArithmeticException - If exp is negative.

pow

public static LargeInteger pow(int a,
                               int exp,
                               int db)
                        throws ArithmeticException
Returns the LargeInteger aexp, stored in base db.

Throws:
ArithmeticException - If exp is negative.
NumberFormatException - If db < 2 or db > 36

multiply

public LargeInteger multiply(int a)
Returns the product of this LargeInteger and a.


divide

public LargeInteger[] divide(int a)
Returns an array whose first element is this / a and whose second element is this % a.

Throws:
ArithmeticException - If a = 0.

toString

public String toString()
Returns the String representation of this LargeInteger. The format is the same as that of Integer.toString(int), where the given int is the radix associated with this LargeInteger.

Overrides:
toString in class Object

getBase

public int getBase()