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

I need some help with this program: /* PF2 lab4 In this lab, you will practice w

ID: 3748310 • Letter: I

Question

I need some help with this program:

/* PF2 lab4 In this lab, you will practice working with one dimensional arrays. Design and implement a BigInteger class that can add, subtract and multiply integers up to 25 digits. To help you get started, I have provided you with methods to input and output the numbers. You merely have to implement the add, subtract and multiply methods. SIMPLIFYING ASSUMPTION: Don't worry about negative numbers. When subtracting numbers, the difference will always be positive. But your program should be able to subtract, for instance, 5 from 1001. (has to handle "borrowing") Put your lab in ~/PF2/lab4/Lab4.java */

This is what I have so far, thank you in advance:

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 BigInteger add(BigInteger bi)
{
BI bsum = new BI();
int tmp, carry=0;

for (int i=INTSIZ-1; i>=0; i--)
{
tmp = intArray[i] + bi.intArray[i] + carry;

bsum.intArray[i] = tmp%10;
carry = tmp/10;
}
return bsum;
}
public void printBigInteger()
{
for (int i=0; i<INTSIZ; i++) {
System.out.print(intArray[i]);
}
System.out.println();
}


// 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(" =========================");
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);
b3 = b1.multiply(b2);

System.out.print("PRODUCT: "); b3.printBigInteger();
System.out.println();
}
}

Explanation / Answer


// Java program to find large factorials using BigInteger
import java.math.BigInteger;
import java.util.Scanner;

public class Example
{
    // Returns Factorial of N
    static BigInteger factorial(int N)
    {
        // Initialize result
        BigInteger f = new BigInteger("1"); // Or BigInteger.ONE

        // Multiply f with 2, 3, ...N
        for (int i = 2; i <= N; i++)
            f = f.multiply(BigInteger.valueOf(i));

        return f;
    }

    // Driver method
    public static void main(String args[]) throws Exception
    {
        int N = 25;
        System.out.println(factorial(N));
    }
}