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

I need a correction on my methods. I will put the whole program at the bottom in

ID: 3558084 • Letter: I

Question

I need a correction on my methods. I will put the whole program at the bottom in case you want to run it to check if the methods are working. All my other methods are great, the program works great, I just need a couple corrections on:

My multiplication, my division and my modulus.

My multiplication method is correct, however I want to change something and I dont know how to do it exactly:

I don't want to multiply by 10, because there is only so many times you can mutliply by 10. So I need to find a different way to get the correct answer.

public BigInt multiply(BigInt b)

   {

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

           p *= 10; //here is the problem

}

       if (neg)

           r.neg = !r.neg;

       return r;

   }

My division and modulus are not giving me the correct answer, also I want to remove the divide by 10 and the % by 10 here as well:

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

   }

Here is my whole program in case you need it:

import java.util.Scanner;

public class BigInt {

   private boolean neg;

   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;

   }

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

   }

   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 b)

   {

       BigInt r = new BigInt("0");

       if (neg && b.neg)

       {

           r.neg = true;

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

       }

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

       {

           r.neg = false;

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

       }

       else if (!neg)

       {

           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

       {

           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 subtract(BigInt b)

   {

       BigInt r = new BigInt("0");

       if (!neg && b.neg)

       {

           r.neg = false;

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

       }

       else if (neg && !b.neg)

       {

           r.neg = true;

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

       }

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

       {

           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

       {

           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 b)

   {

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

           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 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 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

The below is one implementation for bigInt class where mutable can be used. The required prototype methods have been provided in mutaable class.

import java.util.Scanner;

class MutableBigInteger
{

int[] value;

int intLen;

int offset = 0;

private final static long LONG_MASK = 0xffffffffL;

MutableBigInteger()
{

value = new int[1];

intLen = 0;

}
MutableBigInteger(int val)
{

   value = new int[1];

   intLen = 1;

   value[0] = val;

   }
MutableBigInteger(int[] val, int len)
{
value = val;

intLen = len;

}
MutableBigInteger(int[] val)
{

   value = val;

   intLen = val.length;

   }
public void divide(MutableBigInteger b1, MutableBigInteger q,
       MutableBigInteger r) {
   // TODO Auto-generated method stub
  
}

}
public class BigInt
{

   private boolean neg;

   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 BigInt(MutableBigInteger q, boolean neg2) {
   // TODO Auto-generated constructor stub
}

public BigInt(MutableBigInteger q, int[] array2) {
   // TODO Auto-generated constructor stub
}

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;

   }

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

   }

   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 b)

   {

       BigInt r = new BigInt("0");

       if (neg && b.neg)

       {

           r.neg = true;

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

       }

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

       {

           r.neg = false;

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

       }

       else if (!neg)

       {

           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

       {

           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 subtract(BigInt b)

   {

       BigInt r = new BigInt("0");

       if (!neg && b.neg)

       {

           r.neg = false;

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

       }

       else if (neg && !b.neg)

       {

           r.neg = true;

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

       }

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

       {

           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

       {

           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 b)

   {

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

           p *= 10;

       }

       if (neg)

           r.neg = !r.neg;

       return r;

   }

   public BigInt[] divideBy(BigInt b)

   {

       BigInt[] result = new BigInt[2];
       MutableBigInteger q = new MutableBigInteger(),
                         r = new MutableBigInteger(),
                         a = new MutableBigInteger(this.array),
                         b1 = new MutableBigInteger(val.mag);
       a.divide(b1, q, r);
       result[0] = new BigInt(q, this.array);
       result[1] = new BigInt(r, this.neg);
       return result;
   }

   public BigInt modulus(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 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());

   }
}

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