Question: I need to design and implement a BigInteger class that can add, subtract, and multiply integers up to 25 digits. So far, I've gotten the

I need to design and implement a BigInteger class that can add, subtract, and multiply integers up to 25 digits. So far, I've gotten the add method and subtract method, but am having trouble with the multiplication method. I have some psuedo code written out but am stuck. It would be similar to having to do multiplication by hand, where you need to multiply each digit once with each other and then add it together.

import java.io.*;

class BigInteger { private final int INTSIZ=25; private int intArray[] = new int[INTSIZ];

// As it turns out, my BigInteger constructor didn't do anything, // so I just commented it out and will let the system provide a // "default" contstructor. If you find that you want/need to write // a constructor, that is fine. // public BigInteger() // { // }

public void printBigInteger() { for (int i=0; i

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

return biclone; }

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

}

return bidifference; }

private void shift(int n) { for(int i=0; i

public BigInteger multiply(BigInteger biref) { BigInteger bitmp = new BigInteger(this); BigInteger product = new BigInteger(); //to store result int carry = 0; for(int i=INTSIZ-1; i>=0; i--) { for(int j=INTSIZ-1; i>=0; i--) {

}

bitmp = bitmp.shift(INTSIZ-1-i); ?++ ??? add ??? biproduct += bitmp

//two lines of code //method called shift and then add

int tmp = (intArray[i]+carry)*biref.intArray[i]; carry = tmp/10; product.intArray[i] = tmp%10; }

return product; }

// 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

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(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!