# beginner – BigInteger Java implementation

I have written a java application that can handle operations on large integers implemented with a list of numbers in a table.

I had trouble implementing the splitting method because when the difference between the two numbers is high, the operation can take a considerable time.

I would like to know your opinion on this implementation and on ways to improve it. Here is the class and its test class:

BigInt.java

``````import java.util.ArrayList;

public class BigInt implements Comparable {

private static final character MINUS_CHAR = -;
private final static character PLUS_CHAR = + + # 39;

// Record the digits of the number - the last element represents the smallest unit of the number
Private list of paintings numberDigits = new ArrayList <> ();

// Indication if the number is negative
negative Boolean private;

// Representation as a string given by the user
private String stringNumber;

BigInt (Chain number) {

if (number.equals ("")) {
stringNumber = "0";
numberDigits.add (0);
}
other {
// Treat the positive / negative signs
char firstChar = number.charAt (0);
if (firstChar == MINUS_CHAR || firstChar == PLUS_CHAR) {
if (firstChar == MINUS_CHAR)
negative = true;

number = number of substrings (1);
}

// regex to remove zeros at the beginning of the number
number = number.replaceFirst ("^ 0 + (?! \$)", "");
stringNumber = number;

// save the numbers
for (int index = 0; index <number.length (); index ++) {
int curDigNumericVal = Character.getNumericValue (number.charAt (index));

// The current character is not a valid digit
if (curDigNumericVal == -1)
throws new IllegalArgumentException ();

numberDigits.add (curDigNumericVal);
}
}
}

private boolean isNegative () {
return negative;
}

private void flipNegativity () {
if (stringNumber == "0")
return;

negative =! negative;
if (stringNumber.charAt (0) == MINUS_CHAR) {
stringNumber = stringNumber.substring (1);
} other {
stringNumber = MINUS_CHAR + stringNumber;
}
}

// Add another bigInt number to the current bigInt number
BigInt plus (BigInt otherNumber) {

// the current is negative, the other is positive - subtract the current from the other
if (negative &&! otherNumber.isNegative ()) {
return otherNumber.minus (new BigInt (stringNumber));
}

// other is negative - subtract value
if (otherNumber.isNegative ()) {
return less (new BigInt (otherNumber.toString ()));
}

// Set the longer number of the two numbers
ArrayList longerNumber, shortNumber;
if (numberDigits.size ()> = otherNumber.numberDigits.size ()) {
longerNumber = numberDigits;
shortNumber = otherNumber.numberDigits;
}
other {
longerNumber = otherNumber.numberDigits;
shortNumber = numberDigits;
}

int lengthsDifferences = longerNumber.size () - shorterNumber.size ();

StringBuilder resultString = new StringBuilder ();

// Initialize a report for each addition
int carry = 0;

// Iteration from the smallest digit to the largest
for (int index = shortNumber.size () - 1; index> = 0; index--) {
int shortNumberDigit = shortNumber.get (index);
int longerNumberDigit = longerNumber.get (index + lengthsDifferences);

int newDigit = shortNumberDigit + longerNumberDigit + carry;

// Calculation of the report and the new figure
carry = newDigit / 10;
newDigit = newDigit% 10;

resultString.append (newDigit);
}

// Add numbers of a longer number
for (int index = lengthsDifferences - 1; index> = 0; index--) {
int currDig = longerNumber.get (index);

// check if need to add carry
if (currDig + carry == 10) {
resultString.append (0);
wear = 1;
} other {
resultString.append (currDig + carry);
wear = 0;
}
}

// Check if there is a last digit to remember
if (wear> 0)
resultString.append (carry);

return new BigInt (resultString.reverse (). toString ());
}

BigInt less (BigInt otherNumber) {

// If the other number is negative, add its value
if (otherNumber.isNegative ()) {
return plus (new BigInt (otherNumber.stringNumber));
}

// subtract a number larger than the current
if (this.compareTo (otherNumber) < 0) {
BigInt result = otherNumber.minus(this);
result.flipNegativity();
return result;
}

// Other number is positive and not greater than current:
int lengthsDifferences = numberDigits.size() - otherNumber.numberDigits.size();

StringBuilder resultString = new StringBuilder();

int carry = 0;

for (int index = otherNumber.numberDigits.size() - 1; index >= 0; index--) {
int largerNumDig = numberDigits.get (index + lengthDifferences) - carry;
int smallernumDig = otherNumber.numberDigits.get (index);

wear = 0;

if (bigger than nature) < smallerNumDig){
carry = 1;
biggerNumDig += 10;
}

resultString.append(biggerNumDig - smallerNumDig);
}

for (int index = lengthsDifferences - 1; index >= 0; index--) {
int currDig = numberDigits.get (index);

// Check if postponement is necessary
if (carry> currDig) {
resultString.append (currDig + 10 - reporter);
wear = 1;
} other {
resultString.append (currDig - carry);
wear = 0;
}
}

return new BigInt (resultString.reverse (). toString ());
}

// Multiplies bigInt
BigInt multiply (BigInt otherNumber) {

BigInt finalResult = new BigInt ("0");
BigInt currentUnit = new BigInt ("1");

for (int otherNumIndex = otherNumber.numberDigits.size () - 1; otherNumIndex> = 0; otherNumIndex -) {
int currentOtherNumDigit = otherNumber.numberDigits.get (otherNumIndex);

// Contains the result of multiplying the number by the current number of the other number

BigInt currentResult = new BigInt ("0");
BigInt currentDigitUnit = new BigInt (currentUnit.toString ());

for (int index = numberDigits.size () - 1; index> = 0; index--) {
int currentDigit = numberDigits.get (index);
int digitsMultiplication = currentDigit * currentOtherNumDigit;

currentResult = currentDigitUnit.MultiplyUnit (digitsMultiplication);
currentDigitUnit.multiplyByTen ();
}

currentUnit.multiplyByTen ();
finalResult = finalResult.plus (currentResult);
}

// check if you have to return negativity
if (otherNumber.isNegative () &&! isNegative () || isNegative () &&! otherNumber.isNegative ())
finalResult.flipNegativity ();

final returnResult;
}

BigInt divide (BigInt otherNumber) {

if (isBigIntZero (otherNumber))
launch a new ArithmeticException ();

// Deal with the case where the current number is positive and the other negative
if (otherNumber.isNegative () &&! isNegative ()) {
BigInt result = divide (new BigInt (otherNumber.stringNumber));
result.flipNegativity ();
return the result;

// Deal with the case where the current number is negative and the other positive
} else if (! otherNumber.isNegative () && isNegative ()) {
Result BigInt = new BigInt (stringNumber) .divide (otherNumber);
result.flipNegativity ();
return the result;
}

int compareResult = this.compareTo (otherNumber);
if (compareResult == 0)
return new BigInt ("1");
else if (compareResult <0)
return new BigInt ("0");

Result BigInt = new BigInt ("0");
BigInt tempNumber = new BigInt ("0");

while (tempNumber.compareTo (this) < 0) {
tempNumber = tempNumber.plus(otherNumber);
result = result.plus(new BigInt("1"));
}

return result;

}

private boolean isBigIntZero(BigInt number) {
return number.stringNumber.replace("0", "").equals("");

}

// Multiply a unit of BigInt with an integer. Example: 1000000000000000000 * 54
private BigInt MultiplyUnit(int majorUnits){

// Setting the string representation
String majorUnitsString = String.valueOf(majorUnits);
String newNumber = majorUnitsString + stringNumber.substring(1);

return new BigInt(newNumber);
}

private void multiplyByTen() {
this.numberDigits.add(0);
stringNumber += '0';
}

@Override
public int compareTo(BigInt other) {

// Current is negative, other is positive
if (isNegative() && !other.isNegative())
return -1;

// Current is positive, other is negative
else if (!isNegative() && other.isNegative()){
return 1;
}

// Both are negative
else if (isNegative()){
// Current is negative and has more digits - therefore it is smaller
if (numberDigits.size() > other.numberDigits.size ())
return -1;
// The current is negative and has fewer digits - so it is bigger
else if (numberDigits.size () <other.numberDigits.size ())
returns 1;

// both have the same number of digits - you must iterate
other
for (int index = 0; index < numberDigits.size(); index++) {

// Current has bigger negative digit - therefore it is smaller
if (numberDigits.get(index) > other.numberDigits.get (index))
return -1;

// Current has a smaller negative number - so smaller
else if (numberDigits.get (index) < other.numberDigits.get(index))
return 1;
}

// If we have reached here, the numbers are completely identical
return 0;
}

// If we have reached here, both numbers are positive

// Current is positive and has more digits - Therefore it is bigger
if (numberDigits.size() > other.numberDigits.size ()) {
returns 1;
}

// The current is positive and has fewer digits - so it's smaller
else if (numberDigits.size () <other.numberDigits.size ())
return -1;

// both have the same number of digits - you must iterate
other
for (int index = 0; index < numberDigits.size(); index++) {

// Current has bigger positive digit - therefore it is bigger
if (numberDigits.get(index) > other.numberDigits.get (index))
returns 1;

// Current has a smaller positive number - so smaller
else if (numberDigits.get (index) <other.numberDigits.get (index))
return -1;
}

// If we have reached here, the numbers are completely identical
returns 0;
}

@Override
public boolean equals (Object o) {
// auto control
if (this == o)
return true;

// void check
if (o == null)
returns false;

// type check and cast
if (getClass ()! = o.getClass ())
returns false;

BigInt else = (BigInt) o;
// field comparison

return other.toString (). equals (stringNumber);
}

@Override
public String toString () {
return stringNumber;
}
}
``````

main

``````import com.sun.javaws.exceptions.InvalidArgumentException;

import java.util.Scanner;

public class Main {

static private scanner scanner = new scanner (System.in);

public static void main (String[] args) {
BigInt firstNumber;
BigInt secondNumber;

System.out.println ("Enter the first number:");
firstNumber = inputBigIntNumber ();

System.out.println ("Enter the second number:");
secondNumber = inputBigIntNumber ();

System.out.println ("The further result is:" + firstNumber.plus (secondNumber));
System.out.println ("The result of minus is:" + firstNumber.minus (secondNumber));
System.out.println ("The result of multiply is:" + firstNumber.multiply (secondNumber));

try {
System.out.println ("The result of the division is:" + firstNumber.divide (secondNumber));
} catch (ArithmeticException ex) {
System.out.println ("Unable to divide by zero");
}

}

// Take a valid whole entry from the user (greater than 0)
Private Static BigInt inputBigIntNumber () {

String str = scanner.nextLine ();

while (true) {
try {
return new BigInt (str);
}
catch (IllegalArgumentException ex) {
System.out.println ("Invalid number, please try again:");
}
}
}
}
``````

Posted on