Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

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

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

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning VB 2008 Databases

Authors: Vidya Vrat Agarwal, James Huddleston

1st Edition

1590599470, 978-1590599471

More Books

Students also viewed these Databases questions

Question

How are index CDS used by portfolio managers?

Answered: 1 week ago