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

I am missing the min and max methods and cant get the calculator to output the w

ID: 3744713 • Letter: I

Question

I am missing the min and max methods and cant get the calculator to output the way they have it.

Java provides a class 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 nonnegative numbers. Design thhis class carefully. You will need the following:"

- A data structure to represent large numbers: an ArrayList for the digits in a number.

- public BigInt( ArrayList val):

A constructor that uses a string representation of the integer for initialization. The string may contain leading zeros. Do not forget that zero is a valid number .

- public String toString():

Returns the String representation of this BigInt. It should not include leading zeros, but if the number consists of all zeros, it should return a String with a single zero.

- public BigInt max(BigInt val):

A method that returns a BigInt whose value is the maximum of val and the instance of BigInt that invokes max

. - public BigInt min(BigInt val):

A method that returns a BigInt whose value is the minimum of val and the instance of BigInt that invokes min.

- public BigInt add(BigInt val):

A method that returns a BigInt whose value is the sum of val and the instance of BigInt that invokes add.

- public BigInt multiply(BigInt val):

A method that returns a BigInt whose value is the product of val and the instance of Bigint that invokes multiply.

Write a program that acts as an interactive calculator capable of handling very large nonnegative integers that uses the BigInt class. This calculator need perform only the operations of addition and multiplication. In this program each input line is of the form num1 op num2 and should produce output such as

num1

op num2

----------------

num3

where num1 and num2 are (possibly very large) nonnegative numbers, op is the single character + or *, and num3 is the integer that results from the desired calculation. Be sure your interface is user friendly

Explanation / Answer

ANS:

CODE:

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