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

java.lang.BigInteger that can be used to handle very large integers. Implement a

ID: 3558502 • Letter: J

Question

java.lang.BigInteger that can be used to handle very large integers. Implement a similar class, called BigInt, that can be used to do simple calculations with very large non-negative integer numbers.

You will need the following:
A data structure to represent large integers: for example, a string or an array of digits in a number.
A constructor. The number can have leading zeros. Do not forget that zero is a valid number.
A toString( ) method. Do not include leading zeros.
public BigInt add(bigInt value)
public BigInt multiply(BigInt value)
extra credit: public BigInt subtract(BigInt value) //only subtract if parameter value is less than the BigInt object value
Create a driver to clearly demonstate that all of the above methods work.

Here is my driver, if anyone could post a solution to the class that would be great!!!

Explanation / Answer

class BigInt{
   String str;
   int []num;
   public BigInt(){
  
   }
   public BigInt(String str1){
       str = str1;
       removeZeros();
       converToInt(this);
   }
   public BigInt(int []arr)
   {
       str = new String();
       int i = 0;
       while(i<arr.length){
           str +=String.valueOf(arr[i]);
           i++;
       }
   }
   public void removeZeros()
   {
       int i=0;
       while(i < str.length() && str.charAt(i) == '0' )
       {
           i++;
       }
       str = str.substring(i);
   }
   public void converToInt(BigInt obj)
   {
       char []chr = obj.str.toCharArray();
       int len = chr.length;
       obj.num = new int[len];
       int i = 0;
       while(i < len)
       {
           obj.num[i] = Integer.parseInt(String.valueOf(chr[i]));
           i++;
       }
   }
   public void shift(BigInt obj,int len)
   {
       char []chr = obj.str.toCharArray();
       int i = len - obj.num.length , j=0;
       obj.num = new int[len];
       while(j < i)
       {
           obj.num[j] = 0;
           j++;
       }
       j=0;
       while(i < len)
       {
           obj.num[i] = Integer.parseInt(String.valueOf(chr[j]));
           j++;
           i++;
       }
   }
   public int sumofnum(int a,int b,int c)
   {
       return a + b + c;
   }
   public void makeLengthSame(BigInt obj){
       int len = this.num.length;
       int len1 = obj.num.length;
       if(len < len1){
           shift(this,len1);
       }
       else if(len > len1)
       {
           shift(obj,len);
       }
   }
   public BigInt add(BigInt obj)
   {
       int b = 0;
       BigInt ret ;
       makeLengthSame(obj);
       if(obj.num.length == this.num.length)
           System.out.println("ok");
       int i = obj.num.length - 1;
       int []arr = new int[obj.num.length + 1];
       for(int j = 0;j<arr.length;j++) arr[j] = 0;
       int sum;
       String str;
       while(i >= 0){
           if(sumofnum(this.num[i], b, obj.num[i]) >= 10)
           {
               str = String.valueOf((this.num[i] + obj.num[i] + b));
               arr[i+1] = Integer.parseInt(str.substring(1));
               b = Integer.parseInt(str.substring(0,1));
           }
           else
           {
               arr[i+1] = this.num[i] + obj.num[i] + b;
               b = 0;
           }
           i--;
       }
       System.out.print(" ");
       arr[0] = b;
       ret = new BigInt(arr);
       return ret;
   }
   public int multiplyofnum(int a,int b,int c)
   {
       return (a * b) + c;
   }
   public BigInt multiply(BigInt obj)
   {
       BigInt mul , one, two;
       int len = this.num.length;
       int len1 = obj.num.length;
       int res[];
       int diff, count = len;
       res = new int[len + len1 ];
       BigInt(this.num);
       two = new BigInt(obj.num);
       diff = len1 - len;
       if(len > len1){
           count = len1;
           diff = len - len1;
       }
      
       converToInt(two);
       converToInt(one);  
       int i = one.num.length - 1 ,store = res.length -1 , extra = 0 , sec = two.num.length - 1 , flow = 0;
       String str;
       for(int j = 0;j<count;j++){
           while(i >= 0 && store >= 0){
               if(multiplyofnum(one.num[i], two.num[sec], extra) >= 10){
                   str = String.valueOf(one.num[i] * two.num[sec] + extra + res[store]);
                   res[store] = Integer.parseInt(str.substring(1));
                   extra = Integer.parseInt(str.substring(0 ,1));  
               }
               else{
                   res[store] += one.num[i] * two.num[sec] + extra;
                   extra = 0;
               }
               System.out.print(res[store]);
               store--;
               i--;
           }
           i = one.num.length - 1;
           sec--;
           flow++;
           store = res.length - flow - diff ;
           System.out.println(" "+store);
       }
       mul = new BigInt(res);
       return mul;
   }
   public String toString()
   {
       removeZeros();
       return str;
   }
   public static void main(String[] args){
   //   BigInt n = new BigInt("434304");
   //   BigInt n1 = new BigInt("0000110");
   //   System.out.println(n.add(n1));
                  BigInt a, b;
              
                  System.out.println("For this driver, the expected result will be surrounded in '='");
                  System.out.println("For example: =1234=");
                  System.out.println();
                  System.out.println("Beginning BigInt tests...");
                  System.out.println();
              
                  // Can create a BigInt from a String
                  System.out.println("Creating a BigInt with value 1234");
                  a = new BigInt("1234");
                  System.out.println();
              
                  // Can print a BigInt out
                  System.out.println("Printing out a BigInt with value 1234");
                  System.out.println(a);
                  System.out.println("=1234=");
                  System.out.println();
              
                  // Can create a BigInt from a String for a very large number
                  System.out.println("Creating a BigInt with value 9876543210987654321");
                  a = new BigInt("9876543210987654321");
                  System.out.println();
              
                  // Can print a very large BigInt out
                  System.out.println("Printing out a BigInt with value 9876543210987654321");
                  System.out.println(a);
                  System.out.println("=9876543210987654321=");
                  System.out.println();
              
                  // Can create a BigInt with leading zeros
                  System.out.println("Creating a BigInt with value 000012304");
                  a = new BigInt("000012304");
                  System.out.println();
              
                  // Can print a BigInt without leading zeros
                  System.out.println("Printing out a BigInt with value 000012304");
                  System.out.println(a);
                  System.out.println("=12304=");
                  System.out.println();
              
                  // Can add two BigInts together
                  a = new BigInt("1234");
                  b = new BigInt("3210");
                  System.out.println("Adding BigInts with values 1234 and 3210 together");
                  System.out.println(a.add(b));
                  System.out.println("=4444=");
                  System.out.println();
              
                  // Can add two different length BigInts together
                  a = new BigInt("1234");
                  b = new BigInt("123443210");
                  System.out.println("Adding BigInts with values 1234 and 123443210 together");
                  System.out.println(a.add(b));
                  System.out.println("=123444444=");
                  System.out.println();
              
                  // Can add two very large BigInts together
                  a = new BigInt("9876543210987654321");
                  b = new BigInt("5432343210987654321");
                  System.out.println("Adding BigInts with values 9876543210987654321 and 5432343210987654321 together");
                  System.out.println(a.add(b));
                  System.out.println("=15308886421975308642=");
                  System.out.println();
   }
}