BigInteger is a subclass of java

advertisement
CS610 Software Engineering Homework2
Student Name: Tao Jiang
b) Description of the BigInteger class based on its interface and its implementation.
Particular in data representation and all interesting algorithms used.
BigInteger is a subclass of java.lang.number represents integers that can be arbitrarily
large -- i.e., integers that are not limited to the 32 int or 64 long bits available with the
long data type. This class extend from number class, also implements Comparable
interface. The Number class is an abstract class which is the superclass of classes Byte,
Double, Float, Integer, and Short. The BigInteger extend from superclass will provide or
implement methods to convert the represented numeric value to byte, double, float, int,
long or short.
Class BigInteger has six constructors with different constructive functions in the class.
BigInteger(byte[] v) translates a byte array containing the two's-complement binary
representation of a BigInteger into a BigInteger. BigInteger(String v) translates the
decimal String representation of a BigInteger into a BigInteger. BigInteger(int signum,
byte[] magnitude) translates the sign-magnitude representation of a BigInteger into a
BigInteger. Other constructors all construct a BigInteger object according different
conditions. (see constructor summary in Doc description)
BigInteger defines methods that duplicate the functionality of the standard Java arithmetic
and bit-manipulation operators. Those operations are provided to compute residues,
perform exponentiation, and compute multiplicative inverses. These methods always
return a non-negative result, between 0 and 1, inclusive. All functions of those methods
perform same operation as Java primitive integer does, the difference is just using method
instead of operator. One special static method is valueOf(), which can convert a long to a
BigInteger object. Additionally, BigInteger provides operations for modular arithmetic,
GCD calcualtion, primality testing, prime generation, bit manipulation, and a few other
miscellaneous operations.
Comparsion operations perform signed integer comparsions, analogous to those
performed by Java's relational and equality operations -- i. e. the compareTo() method
compares two BigInteger objects, and returns -1, 0, or 1 to indicate the result of the
comparison.
Two static fields are one and zero which can concrete two's-complement binary
representation within BigInteger class.
By reading BigInteger source code, I have several points related class algorithms want to
point out:
There are six different private fields in the class: 1) int signum: which identify the number
is negative or positive by using -1, 0 or 1. '0' here is to ensures that there is exactly one
representation for each BigInteger value. 2) byte[] magnitude: The magnitude must be
minimal in which the most-significant byte must be non-zero. this is necessary to ensure
that there is exactly one representation for each BigInteger value. zero-length magnitude
array. 3) int bitCount: the bigCount of the BigInteger, as returned by bitCount(). 4) int
bitLength: the bigLength of this BigInteger, as returned by bitLength(). 5) int
lowestSetBit: the lowest set bit of this BigInteger, as returned by getLowestSetBit(). 6)
int firstNonzeroByteNum: The type-number of the lowest-order nonzero byte in the
magnitude of this BigInteger, or -2. The significant byte has byte-number 0, the next
bytes in order of increasing significance has byte-number 1, and so forth.
The function of all six constructors to concrete a new BigInteger object, but the parameter
pass is different.
BigInteger(byte[] val) and BigInteger(int signum, byte[] magnitude) are passing byte
array to construct a BigInteger object with private field: magnitude of the type byte
array.
BigInteger(String val, int radix) construct an object according to specified radix and it
also has used several library classes, such as Character, String, Long and so on. There are
two variable in this method which are 'cursor' and numDigits. First one is to use skiping
leading zero and compute number of digits in magnitude. Second is to hold number of
digits in the 'val' passing variable.
BigInteger(String val) is simply used BigInteger(String val, int radix) with parameter 10.
BigInteger(int numBits, Random rnd) and BigInteger(int bitLength, ubt certainty,
Random rnd) all construct a randomly generated positive BigInteger with different
parameter.
Arithmetic methods exactly mimic those of Java's integer arithmetic operators, as defined
in The Java Language. For example, division by zero throws an ArithmeticException,
and division of a negative by a positive yields a negative (or zero) remainder.
Static method valueOf(Long val) is returning a BigInteger whose value is equal to that of
the specified long and this static factory method is provided in prefence to a (long)
constructor because it allows for reuse of frequently used BigIntegers.
Download