Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to implement a BigInteger Class that can add, subtract and multiply

ID: 3869728 • Letter: I

Question

I'm trying to implement a BigInteger Class that can add, subtract and multiply integers up to 25 digits using a one-dimensional array. I got the addition method done and the subtract I think but am having trouble with the multiply method.

import java.io.*;

class BigInteger
{
    private final int INTSIZ=25; //change back to 25
    private int intArray[] = new int[INTSIZ];


    public void printBigInteger()
    {
        for (int i=0; i<INTSIZ; i++) {
            System.out.print(intArray[i]);
        }
        System.out.println();
    }

    public BigInteger add(BigInteger biref)
    {
        BigInteger bisum = new BigInteger();
        int tmp, carry=0;
        for(int i=(INTSIZ-1); i>=0; i--){
            tmp = intArray[i] + biref.intArray[i] + carry;
            carry = tmp/10;
            bisum.intArray[i] = tmp%10;
        }

        return bisum;
    }

    private BigInteger clone(BigInteger biref)
    {
        BigInteger biclone = new BigInteger();
        for(int i=0; i<INTSIZ; i++){
            biclone.intArray[i] = biref.intArray[i];
        }

        return biclone;
    }

    public BigInteger subtract(BigInteger biref)
    {
        BigInteger bidifference = new BigInteger();
        int tmpIntArray[] = new int[INTSIZ];
        int borrow = 0;
        for(int i=INTSIZ-1; i>=0; i--){
            if(tmpIntArray[i] < biref.intArray[i])
            {
                tmpIntArray[i] = tmpIntArray[i] + 10;
                tmpIntArray[i-1] = tmpIntArray[i-1] - 1;
            }
            int tmp = tmpIntArray[i] - biref.intArray[i] - borrow;
            borrow = tmp/10;
            bidifference.intArray[i] = tmp%10;

        }

        return bidifference;
    }


    private BigInteger shift(int n)
    {
        for(int i=0; i<n; i++){
            for(int j=1; j<INTSIZ; j++){
                intArray[j-1] = intArray[j];
                }
        intArray[INTSIZ-1] = 0;
        }
    }


    public BigInteger multiply(BigInteger biref)
    {
    }

    // don't worry about the implementation of this method. We haven't
    // covered some of the String methods below, but will ... very soon!
    public void inputBigInteger() throws IOException
    {
        BufferedReader input = new BufferedReader
                              (new InputStreamReader(System.in));

        System.out.print("enter the BigInteger: (do not pad with zeros): ");
        String str = input.readLine();

        if (str.length() > INTSIZ) throw new ArithmeticException("OVERFLOW!");

        for (int i=0; i<str.length(); i++)
        {
            intArray[INTSIZ-str.length()+i] =
                    Integer.parseInt(str.substring(i, i+1));
        }
    }
}

public class Lab4
{
    public static void main(String argv[]) throws IOException
    {
        BigInteger b1,b2,b3;

        b1 = new BigInteger();
        b2 = new BigInteger();

        System.out.println("input the first BigInteger:");
        b1.inputBigInteger();
        System.out.println("input the second BigInteger:");
        b2.inputBigInteger();

        System.out.print("BigInt #1: "); b1.printBigInteger();
        System.out.print("BigInt #2: "); b2.printBigInteger();
        System.out.println("            =========================");
        b3 = b1.add(b2);
        System.out.print("SUM:        "); b3.printBigInteger();
        System.out.println();

        System.out.print("BigInt #1: "); b1.printBigInteger();
        System.out.print("BigInt #2: "); b2.printBigInteger();
        System.out.println("            =========================");
        b3 = b1.subtract(b2);
        System.out.print("DIFFERENCE: "); b3.printBigInteger();
        System.out.println();

        System.out.print("BigInt #1: "); b1.printBigInteger();
        System.out.print("BigInt #2: "); b2.printBigInteger();
        System.out.println("            =========================");
        b3 = b1.multiply(b2);
        System.out.print("PRODUCT:    "); b3.printBigInteger();
        System.out.println();
    }
}

Explanation / Answer

import java.io.*;
class BigInteger
{
private final int INTSIZ=25; //change back to 25
private int intArray[] = new int[INTSIZ];

public void printBigInteger()
{
for (int i=0; i<INTSIZ; i++) {
System.out.print(intArray[i]);
}
System.out.println();
}
public BigInteger add(BigInteger biref)
{
BigInteger bisum = new BigInteger();
int tmp, carry=0;
for(int i=(INTSIZ-1); i>=0; i--){
tmp = intArray[i] + biref.intArray[i] + carry;
carry = tmp/10;
bisum.intArray[i] = tmp%10;
}
return bisum;
}
private BigInteger clone(BigInteger biref)
{
BigInteger biclone = new BigInteger();
for(int i=0; i<INTSIZ; i++){
biclone.intArray[i] = biref.intArray[i];
}
return biclone;
}
public BigInteger subtract(BigInteger biref)
{
BigInteger bidifference = new BigInteger();
int tmpIntArray[] = new int[INTSIZ];
int borrow = 0;
for(int i=INTSIZ-1; i>=0; i--){
if(tmpIntArray[i] < biref.intArray[i])
{
tmpIntArray[i] = tmpIntArray[i] + 10;
tmpIntArray[i-1] = tmpIntArray[i-1] - 1;
}
int tmp = tmpIntArray[i] - biref.intArray[i] - borrow;
borrow = tmp/10;
bidifference.intArray[i] = tmp%10;
}
return bidifference;
}

private BigInteger shift(int n)
{
for(int i=0; i<n; i++){
for(int j=1; j<INTSIZ; j++){
intArray[j-1] = intArray[j];
}
intArray[INTSIZ-1] = 0;
}
return null;
}

public BigInteger multiply(BigInteger biref)
{
BigInteger result = new BigInteger();//to store result..
BigInteger BigInteger();//to subtract 1, at a time
one.intArray[INTSIZ-1]=1;//storing one
BigInteger New = new BigInteger();

int c=1,i;
for( i=(INTSIZ-1); i>=0; i--)
{
New.intArray[i]=intArray[i];
}

while(c>1)
{
   for( i=(INTSIZ-1); i>=0; i--)//checking multiplier is empty or not
   {
       if(intArray[i]!=0)break;
   }
   if(i<=0)break;//breacking loop if multiplication is done
  
   result = result.add(biref);
  
   New=New.subtract(one);
          
}
return result;//returning result..

}

// don't worry about the implementation of this method. We haven't
// covered some of the String methods below, but will ... very soon!
public void inputBigInteger() throws IOException
{
BufferedReader input = new BufferedReader
(new InputStreamReader(System.in));
System.out.print("enter the BigInteger: (do not pad with zeros): ");
String str = input.readLine();
if (str.length() > INTSIZ) throw new ArithmeticException("OVERFLOW!");
for (int i=0; i<str.length(); i++)
{
intArray[INTSIZ-str.length()+i] =
Integer.parseInt(str.substring(i, i+1));
}
}
}

public class Lab4
{
public static void main(String argv[]) throws IOException
{
BigInteger b1,b2,b3;
b1 = new BigInteger();
b2 = new BigInteger();
System.out.println("input the first BigInteger:");
b1.inputBigInteger();
System.out.println("input the second BigInteger:");
b2.inputBigInteger();
System.out.print("BigInt #1: "); b1.printBigInteger();
System.out.print("BigInt #2: "); b2.printBigInteger();
System.out.println(" =========================");
b3 = b1.add(b2);
System.out.print("SUM: "); b3.printBigInteger();
System.out.println();
System.out.print("BigInt #1: "); b1.printBigInteger();
System.out.print("BigInt #2: "); b2.printBigInteger();
System.out.println(" =========================");
b3 = b1.subtract(b2);
System.out.print("DIFFERENCE: "); b3.printBigInteger();
System.out.println();
System.out.print("BigInt #1: "); b1.printBigInteger();
System.out.print("BigInt #2: "); b2.printBigInteger();
System.out.println(" =========================");
b3 = b1.multiply(b2);
System.out.print("PRODUCT: "); b3.printBigInteger();
System.out.println();
}
}