Category Archives: CSC

BigInt class is to solve a very large integer problem using large data structures and short methods that work together to solve the operations of add, subtract multiply, divide, and modulus.

The purpose of the BigInt class is to solve a very large integer problem using large data structures and short methods that work together to solve the operations of add, subtract multiply, divide, and modulus. 
The primitive data types in a computer are limited in precession and scale due to their set storage size. We can create a storage structure that can hold an arbitrarily large value. To accomplish this we will use an array list to hold individual digits of the number.
There are several problems to overcome when converting an input string of numeric characters to an internal array list representation of a number. A few examples are:
    Leading signs and leading zeros need to be accounted for.
    Improper characters, including spaces, need to be identified.
    While a leading sign is correct a sign in the middle of a number is not correct.
Constructors cancall methodsthat evaluate the input string. For example you might call the method setSignAndRemoveItIfItIsThere() to identify the sign of the input number, remove it, and store it in the data structure.  This helper method shouldreceivethe string that was sent to the constructor and set a boolean variable positive to true or false and then returns a string without the sign that can then be processed by the constructor to store the string in an ArrayList. 
The public add, subtract, multiply, and divide methodsdo not do the actual addition, subtraction, multiplication, and division but call helper methods to do the work. 
This is a challenging project that requires information hiding to be successful. 
There are two main programs that you must use.  I will supply a test program for the constructor and toString() methods.  This tester must be run when you are working on the constructor and toString() method.  There is a second BigInt math tester for add, subtract, multiply and divide.  This math tester must be used when you test addition, subtraction, multiplication, division, and modulus.
Due Dates:
BigInt constructors and toString() method.  Consider a String argument, an integer argument, and a BigInt argument to the constructor.  Create a RuntimeException class that throws an exception when the input string does not hold a properly formed integer.
I have attached the ArrayList code we covered in class and the BigInt tester Eclipse project file.

BigInt Notes

BigInt Constructor and toString Considerations
Constructors initialize the private data at the point of declaration.  The constructor has a String parameter.  Java code must analyze this string and if it correctly represents a BigInt convert the characters to integers and store them in an ArrayList or an array.  Example strings your constructor must correctly deal with are: 100, -5, -0, +0, +, -, 56DDD8,   456  The last four examples do not represent an integer and must be rejected.  A BigInt consists of a boolean sign and the array or ArrayList holding the absolute value of the number.  If an array is used a third variable holding the size of the BigInt must also be included.  There should be no other instance variables.  Instance variables can be seen throughout the class.  You will need other String, int and double variables that will be declared in methods and will be local to that method. These local variables can be passed as arguments to other methods. The algorithm could be:
1) Read a String
2) If the string has a or + as the first character remove it and set the sign. .  Using a very descriptive name the method might be called setSignAndRemoveItIfItIsThere(num). If the sign is the only character in the string end the program. 
3) Use a for loop to work through the string from the end of the string to the front. Store the string characters as integers. The Character wrapper class method isDigit() can make this conversion, or the character can be cast as an integer remembering to subtract 48 from the ASCII value. If a character other than a digit is encountered end the program.
4) Think of the indexes of the ArrayList values going right to left starting with 0.  With the string 549 the 9 can be placed int location 0, the 4 in location 1 and the 5 in location 2.  With an ArrayList this happens automatically with the add method. Example: bigNum.add(value). (See ArrayList documentation.  I will put some on my website.)
5) The toString() method creates and returns a String with the sign if it is negative and then adding the  ArrayList integers starting from the back of the list to the front.
6) You must be familiar with the String methods to turn a string into integers that can be stored in an array.  You then need to take the integers stored in an array and turn them back into a string in the toString() method.
7)  important reminders:
String num = -1234; // creates a string of 5 characters
int i = num.length(); // strings know their length. i has a value of 5
the string method charAt returns the character value at the specified location
if(num.charAt(0) == ‘+’ || num.charAt(0) == ‘-‘)
if(num.charAt(i) >= 0 &&num.charAt(i) <= 9) it is a digit.
substring(start) returns a new string having the same characters as the substring that begins at index start of this string through to the end of the string.  Index numbers begin at 0. Example:
num = num.substring(1) will assign the string 1234 to num
given int value = 5;
given num is 1234
num = num + value;// num now is the string 12345
Other String operations may be useful. 
You can also test if a character is a digit with Character.isDigit()
Do not put a main method in the BigInt class.  I will provide testing classes.  One is to test the constructor and toString method and one is to test the operations add, subtract, multiply, divide, and modulus of the class. These testing classes are in different packages as the BigInt class.
Do not use an instance of the Scanner class for keyboard input in side the Bigint Class.  The constructor provides the strings for input.

Add two public methods to the BigInt Class.
For each of these new public methods use private helper methods to solve the arithmetic.
The BigIntTesterAdditionAndSubtractionOutput document shows the terminal output of a correctly working BigInt class with its error handler.

Finish the class by adding:
multiply(BigInt):BigInt – BigInt
modulus(BigInt):BigInt – BigInt
divide(BigInt):BigInt – BigInt
equals(BigInt):boolean – BigInt
equalsIgnoreSign(Bibint):boolean – BigInt
Hint: Multiplication is a series of additions. Division and Modulus are both a series of subtractions. Remember remove leading zeros from subtractions and add multiplier offset zeros.