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

This is not my program- but I was wondering why the division and modulus methods

ID: 666569 • Letter: T

Question

This is not my program- but I was wondering why the division and modulus methods do not work? Any help would be appreciated!

import java.util.Scanner;

public class BigInt

{

private boolean neg; // negative

private int[] array;

   public BigInt(String b1)

   {

       String value = b1;

       neg = false;

       if (b1.charAt(0) == '+' || b1.charAt(0) == '-')

       {

           if (b1.length() > 1)

           {

               value = b1.substring(1);

           }

           else

           {

               System.out.println("this contains only sign: " + b1);

               return;

           }

           if (b1.charAt(0) == '-')

               neg = true;

       }

       array = new int[value.length()];

       for (int i = 0; i < value.length(); i++)

       {

           if (Character.isDigit(value.charAt(i)))

           {

               array[array.length - 1 - i] = Character.digit(value.charAt(i), 10);

           }

           else

           {

               System.out.println("this contains non numeric values: " + b1);

               return;

           }

       }

   }

   public String toString()

   {

       StringBuilder st = new StringBuilder();

       if (neg)

           st.append('-');

       else

           st.append('+');

       for(int i=0; i<array.length; i++)

       {

           st.append(array[array.length - 1 - i]);

       }

       return st.toString();

   }

   private int[] removeLeadingZero(int[] a)

   {

       int i = a.length - 1;

       while (i > 0 && a[i] == 0)

           i--;

       int[] c = new int[i+1];

       for (int j = 0; j < c.length; j++)

           c[j] = a[j];

       return c;

   }

// return 0 if same, <0 if a<b, > 0 if a > b

   private int compareArr(int[] a, int[] b)

   {

       if (a.length != b.length)

           return a.length - b.length;

       for (int i = a.length - 1; i >= 0; i--)

       {

           if (a[i] != b[i])

               return a[i] - b[i];

       }

       return 0;

   }

   private int[] addArr(int[] a, int[] b)

   {

       int size = Math.max(a.length, b.length) + 1;

       int[] c = new int[size];

       int carry = 0;

       for (int i = 0; i < a.length || i < b.length || carry > 0; i++)

       {

           int sum = carry;

           if (i < a.length)

               sum += a[i];

           if (i < b.length)

               sum += b[i];

           carry = sum / 10;

           sum = sum % 10;

           c[i] = sum;

       }

       return removeLeadingZero(c);

   }

// assume a >= b

           private int[] subArr(int[] a, int[] b)

   {

       int[] c = new int[a.length];

       int carry = 0;

       for (int i = 0; i < a.length || i < b.length || carry != 0; i++)

       {

           int r = carry;

           if (i < a.length)

               r += a[i];

           if (i < b.length)

               r -= b[i];

           carry = (r < 0 ? -1 : 0);

           if (r < 0)

               r += 10;

           c[i] = r;

       }

       return removeLeadingZero(c);

   }

           public BigInt add(BigInt bb)

           {

               BigInt r = new BigInt("0");

               if (neg && bb.neg)

               {

                   r.neg = true;

                   r.array = addArr(array, bb.array);

               }

               else if (!neg && !bb.neg)

               {

                   r.neg = false;

                   r.array = addArr(array, bb.array);

               }

               else if (!neg) // positive + negative

               {

                   if (compareArr(array, bb.array) >= 0)

                   {

                       r.neg = false;

                       r.array = subArr(array, bb.array);

                   }

                   else

                   {

                       r.neg = true;

                       r.array = subArr(bb.array, array);

                   }

               }

               else // negative + positive

               {

                   if (compareArr(array, bb.array) <= 0)

                   {

                       r.neg = false;

                       r.array = subArr(bb.array, array);

                   }

                   else

                   {

                       r.neg = true;

                       r.array = subArr(array, bb.array);

                   }

               }

               return r;

           }

           public BigInt subtract(BigInt b)

           {

               BigInt r = new BigInt("0");

               if (!neg && b.neg) // positive - negative

               {

                   r.neg = false;

                   r.array = addArr(array, b.array);

               }

               else if (neg && !b.neg) // negative - positive

               {

                   r.neg = true;

                   r.array = addArr(array, b.array);

               }

               else if (!neg && !b.neg) // positive - positive

               {

                   if (compareArr(array, b.array) >= 0)

                   {

                       r.neg = false;

                       r.array = subArr(array, b.array);

                   }

                   else

                   {

                       r.neg = true;

                       r.array = subArr(b.array, array);

                   }

               }

               else // negative - negative

               {

                   if (compareArr(array, b.array) <= 0)

                   {

                       r.neg = false;

                       r.array = subArr(b.array, array);

                   }

                   else

                   {

                       r.neg = true;

                       r.array = subArr(array, b.array);

                   }

               }

               return r;

           }

           public BigInt multiply(BigInt bb)

           {

               BigInt r = new BigInt("+0");

               int p = 1;

               for (int i = 0; i < array.length; i++)

               {

                   int times = array[i] * p;

                   for (int j = 0; j < times; j++)

                       r = r.add(bb);

                   p *= 10;

               }

               if (neg)

                   r.neg = !r.neg;

               return r;

           }

           public BigInt divideBy(BigInt b)

           {

               BigInt r = new BigInt("+0");

               int p = 1;

               for (int i = 1; i < array.length; i++)

               {

                   int times = array[i] / p;

                   for (int j = 1; j < times; j++)

                       r = r.add(b);

                   p /= 10;

               }

               if (neg)

                   r.neg = !r.neg;

               return r;

           }

           public BigInt modulus(BigInt bb)

           {

               BigInt r = new BigInt("0");

               int p = 1;

               for (int i = 1; i < array.length; i++)

               {

                   int times = array[i] % p;

                   for (int j = 1; j < times; j++)

                       r = r.add(bb);

                   p %= 10;

               }

               if (neg)

                   r.neg = !r.neg;

               return r;

           }

           public static void main(String[] args)

           {

               Scanner in = new Scanner(System.in);

               System.out.print("Enter the first number : ");

               BigInt ba = new BigInt(in.next());

               System.out.print("Enter the second number : ");

               BigInt bb = new BigInt(in.next());

               System.out.println(ba.toString() + " + " + bb.toString() + " = " +

   ba.add(bb).toString());

               System.out.println(ba.toString() + " - " + bb.toString() + " = " +

   ba.subtract(bb).toString());

               System.out.println(ba.toString() + " * " + bb.toString() + " = " +

   ba.multiply(bb).toString());

               System.out.println(ba.toString() + " / " + bb.toString() + " = " +

   ba.divideBy(bb).toString());

               System.out.println(ba.toString() + " % " + bb.toString() + " = " +

   ba.modulus(bb).toString());

           }

}

Explanation / Answer

BigIntDemo.java

BigInt.java

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote