CMPS 144 Spring 2019
Prog. Assg. #3: Big Integers
Due: 11:59pm, Friday, March 22

In the previous programming assignment, you were to have implemented the BigIntUnsigned class, instances of which represent nonnegative integers of arbitrarily large magnitude. (The link in the previous sentence goes to a solution.) For this assignment, you are to make use of that class in implementing the BigInt class, instances of which represent integers —positive or negative— of arbitrarily large magnitude.

That the methods in BigInt are in nearly one-to-one correspondence to those in BigIntUnsigned should be of no surprise, as the arithmetic and relational operations applicable to integers, in general, are the same as those applicable to nonnegative integers. The one "new" operation in BigInt is to compute absolute value. Of course, there was no point in having such a method in the BigIntUnsigned class, given that the absolute value of every nonnegative integer is itself.

Welcome to the BigInt Calculator!

> -25 * 3
Computing -25 * 3
-75

> -25 < -13
Computing -25 < -13
true

> +159 * -37
Computing 159 * -37
-5883

> -23456 % 7
Computing -23456 % 7
-6

> -23456 % -7
Computing -23456 % -7
-6

> 23 + -77
Computing 23 + -77
-54

> 164 - -1000
Computing 164 - -1000
1164

> 78 - 123
Computing 78 - 123
-45

> 9834567 / -23456
Computing 9834567 / -23456
-419

> -0 = +0
Computing 0 = 0
true

> 12 > -99
Computing 12 > -99
true

> 940834512342134 + -34567891234
Computing 940834512342134 + -34567891234
940799944450900

> -303487612 * -345678323
Computing -303487612 * -345678323
104909088767434676

>
As is the case with instances of BigIntUnsigned, instances of BigInt are to be immutable, which is to say that the class includes no mutator methods (or what your textbook authors refer to as transformer methods). Rather, the methods that implement the arithmetic operations are generators, which means that they produce new instances of the class rather than modifying the state of any such instance. For example if x and y refer to instances of BigInt, the method call

x.plus(y)

returns a new instance of the class representing the sum of the values represented by x and y. The states of the objects referred to by x and y are not changed.

The reader is assumed to understand how to carry out arithmetic and relational operations that involve one or two negative numbers. For example, letting Z = |X| * |Y|, we have that either X*Y = Z or X*Y = −Z, the former if X and Y have the same sign and the latter otherwise. (|X| and |Y| denote the absolute values of X and Y, respectively.) A similar rule applies to division. Addition and subtraction are a bit more complicated, as not only the signs but also the magnitudes of the operands determine how to proceed.

The mod() operation, being somewhat quirky (and not consistent with how it is defined in most math books), deserves mention here. For our purposes, we define A % B to be A − B*(A/B). In the case that A is negative, this yields a negative result (or zero).

For testing purposes, you can use the BI_Calculator application. A dialog between it and a user appears to the right.


Implementation Suggestions

Note that there is no reason why the BigInt class should include any loops, arrays, or ArrayLists. If you find yourself thinking about using them, you are making this project more difficult than necessary.


Program Submission

Use the file submission system (see the link near the top of the course web page) to submit your completed BigInt.java file into the prog3_dir folder. Make sure to complete the comments indicated near the top of the source code that has been provided. In particular, you should put your name and list the names of anyone who aided/collaborated with you in doing the work. Also, you are to describe any behavioral defects that your class has (e.g., characterizations of test cases that it fails). If there are defects of which you are unaware, it probably means that you did a poor job of testing your work. Thus, of two submissions having similar defects, one in which those defects are acknowledged deserves a better grade than one in which they are not acknowledged.