Question
Hello. I have this program called BigInt that takes 2 big numbers as strings, put them in arrays not arraylist, and is suppose to add
Hello. I have this program called BigInt that takes 2 big numbers as strings, put them in arrays not arraylist, and is suppose to add sub mul and div them. I have the adding and sub working and have been working on the mul but it keeps droping the last number when mul number with 2 digits such as 37*26=962 but only outputs 96 and it works for 12*2=24. If anyone knows how to do the div and mod and fix the mulp please help thank you. The first is the Demo and the 2nd is my code.
Demo
package BigNum;
import BigNum.BigInt; import BigNum.StringErrorException;
public class BigIntConstructorDemo {
public static void main(String[] args) throws StringErrorException {
b1 = new BigInt("12"); b2 = new BigInt("2"); b3 = b1.add(b2); System.out.println(" 2) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); b3 = b1.div(b2); System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); b3 = b1.modulus(b2); System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("-1"); b2 = new BigInt("1"); b3 = b1.add(b2); System.out.println(" 3) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); b3 = b1.div(b2); System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); b3 = b1.modulus(b2); System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("1"); b2 = new BigInt("-1"); b3 = b1.add(b2); System.out.println(" 4) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); b3 = b1.div(b2); System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); b3 = b1.modulus(b2); System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("-1"); b2 = new BigInt("-1"); b3 = b1.add(b2); System.out.println(" 5) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); b3 = b1.div(b2); System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); b3 = b1.modulus(b2); System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("+1"); b2 = new BigInt("+1"); b3 = b1.add(b2); System.out.println(" 6) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); b3 = b1.div(b2); System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); b3 = b1.modulus(b2); System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("-9"); b2 = new BigInt("9"); b3 = b1.add(b2); System.out.println(" 7) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); b3 = b1.div(b2); System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); b3 = b1.modulus(b2); System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("100"); b2 = new BigInt("-100"); b3 = b1.add(b2); System.out.println(" 8) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); // b3 = b1.divideBy(b2); // System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); // b3 = b1.modulus(b2); // System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("-100"); b2 = new BigInt("-100"); b3 = b1.add(b2); System.out.println(" 9) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); // b3 = b1.divideBy(b2); // System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); // b3 = b1.modulus(b2); // System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("100"); b2 = new BigInt("100"); b3 = b1.add(b2); System.out.println(" 10) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); // b3 = b1.divideBy(b2); // System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); // b3 = b1.modulus(b2); // System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("200"); b2 = new BigInt("-0"); b3 = b1.add(b2); System.out.println(" 11) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); //b3 = b1.divideBy(b2); //System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); //b3 = b1.modulus(b2); //System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("-200"); b2 = new BigInt("-0"); b3 = b1.add(b2); System.out.println(" 12) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); //b3 = b1.divideBy(b2); //System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); //b3 = b1.modulus(b2); //System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("-0"); b2 = new BigInt("200"); b3 = b1.add(b2); System.out.println(" 13) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); // b3 = b1.divideBy(b2); // System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); // b3 = b1.modulus(b2); // System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("-0"); b2 = new BigInt("-200"); b3 = b1.add(b2); System.out.println(" 14) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); // b3 = b1.divideBy(b2); // System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); // b3 = b1.modulus(b2); // System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("37"); b2 = new BigInt("26"); b3 = b1.add(b2); System.out.println(" 15) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); // b3 = b1.divideBy(b2); // System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); // b3 = b1.modulus(b2); // System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("-37"); b2 = new BigInt("26"); b3 = b1.add(b2); System.out.println(" 16) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); // b3 = b1.divideBy(b2); // System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); // b3 = b1.modulus(b2); // System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("37"); b2 = new BigInt("-26"); b3 = b1.add(b2); System.out.println(" 17) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); // b3 = b1.divideBy(b2); // System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); // b3 = b1.modulus(b2); // System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("-37"); b2 = new BigInt("-26"); b3 = b1.add(b2); System.out.println(" 18) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); // b3 = b1.divideBy(b2); // System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); // b3 = b1.modulus(b2); // System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("-200111111111111111199999999"); b2 = new BigInt("3333333333333388888888888888888888555555555555555555555555"); b3 = b1.add(b2); System.out.println(" 19) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); // b3 = b1.divideBy(b2); // System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); // b3 = b1.modulus(b2); // System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("66666666666666666666677777777777777777777711111111111111111200"); b2 = new BigInt("-3333333333333333333344444444444"); b3 = b1.add(b2); System.out.println(" 20) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); // b3 = b1.divideBy(b2); // System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); // b3 = b1.modulus(b2); // System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("-66666666666666666666677777777777777777777711111111111111111200"); b2 = new BigInt("-333333"); b3 = b1.add(b2); System.out.println(" 21) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); // b3 = b1.divideBy(b2); // System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); // b3 = b1.modulus(b2); // System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("6666666"); b2 = new BigInt("3333333333333333333344444444444444444444455555555555555550"); b3 = b1.add(b2); System.out.println(" 22) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); // b3 = b1.divideBy(b2); // System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); // b3 = b1.modulus(b2); // System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("1111111111111111111111111111111122222222222222222222222222222222222333333333333333333333333333333334444444444444444444444444"); b2 = new BigInt("99999999999999999999999999999888888888888888888888888888888881111111111111111111111111111111122222222222222222222222222222222222333333333333333333333333333333334444444444444444444444444"); b3 = b1.add(b2); System.out.println(" 23) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); // b3 = b1.divideBy(b2); // System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); // b3 = b1.modulus(b2); // System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("-1111111111111111111111111111111122222222222222222222222222222222222333333333333333333333333333333334444444444444444444444444"); b2 = new BigInt("99999999999999999999999999999888888888888888888888888888888881111111111111111111111111111111122222222222222222222222222222222222333333333333333333333333333333334444444444444444444444444"); b3 = b1.add(b2); System.out.println(" 24) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); // b3 = b1.divideBy(b2); // System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); // b3 = b1.modulus(b2); // System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("1111111111111111111111111111111122222222222222222222222222222222222333333333333333333333333333333334444444444444444444444444"); b2 = new BigInt("-99999999999999999999999999999888888888888888888888888888888881111111111111111111111111111111122222222222222222222222222222222222333333333333333333333333333333334444444444444444444444444"); b3 = b1.add(b2); System.out.println(" 25) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); // b3 = b1.divideBy(b2); // System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); // b3 = b1.modulus(b2); // System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3);
b1 = new BigInt("-1111111111111111111111111111111122222222222222222222222222222222222333333333333333333333333333333334444444444444444444444444"); b2 = new BigInt("-99999999999999999999999999999888888888888888888888888888888881111111111111111111111111111111122222222222222222222222222222222222333333333333333333333333333333334444444444444444444444444"); b3 = b1.add(b2); System.out.println(" 26) sum b3 is " + b1 +" + " + b2 + " = " + b3); b3 = b1.sub(b2); System.out.println("difference b3 is " + b1 +" - " + b2 + " = " + b3); b3 = b1.mul(b2); System.out.println("product b3 is " + b1 +" * " + b2 + " = " + b3); // b3 = b1.divideBy(b2); // System.out.println("quotient b3 is " + b1 +" / " + b2 + " = " + b3); // b3 = b1.modulus(b2); // System.out.println("modulus b3 is " + b1 +" mod " + b2 + " = " + b3); } }
BigInt
package BigNum; import BigNum.StringErrorException; import BigNum.BigInt;
public class BigInt { private boolean neg; // negative private int[] digit;//array of digits private int max=1000;
// constructor default public BigInt() { neg=true; digit= new int[max]; } // BigInt String public BigInt(String b1) throws StringErrorException {
String value = b1;
neg = false;
if (b1.charAt(0) == '+' || b1.charAt(0) == '-') {
if (b1.length() > 1) {
value = b1.substring(1);
} else { throw new StringErrorException("This contains only a sign!!" + b1);
}
if (b1.charAt(0) == '-') //if is negative
neg = true;
}
digit = new int[value.length()];
for (int i = 0; i < value.length(); i++) {
if (Character.isDigit(value.charAt(i))) { digit[digit.length - 1 - i] = Character.digit(value.charAt(i), 10); } else { throw new StringErrorException("Illegal value input!" + b1);
}
}
}
public String toString()
{ // The StringBuilder class has a length() method that returns the length of the character sequence in the builder. StringBuilder st = new StringBuilder();
if (neg)
st.append('-');
for(int i=0; i { st.append(digit[digit.length - 1 - i]); } return st.toString(); } // an array to remove leading zeroes 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 0 if a > b private int compare(int[] a, int[] b) { if (a.length != b.length)//if the length of b1 is not equal to the b2 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); } // adding two ints public BigInt add(BigInt other) throws StringErrorException { BigInt answer = new BigInt("0"); if (this.neg && other.neg) { answer.neg = true; answer.digit = addArr(this.digit, other.digit);// is going to go to this add Array } else if (!this.neg && !other.neg)// if they both are positive { answer.neg = false; answer.digit = addArr(this.digit, other.digit);// is going to the add array method } //Here is where it adds the b1 that is positive and the b2 that is negative number else if (!neg) // positive + negative { //is comparing if the lengths have the same length or not if (compare(this.digit, other.digit) >= 0) { answer.neg = false; answer.digit = subArr(this.digit, other.digit); } else { answer.neg = true; answer.digit = subArr(other.digit, this.digit); } } // adding the b1 is negative and the b2 is positive else // negative + positive { if (compare(this.digit, other.digit) <= 0) { answer.neg = false; answer.digit = subArr(other.digit, this.digit); } else { answer.neg = true; answer.digit = subArr(digit, other.digit); } } return answer; } public BigInt sub(BigInt b) throws StringErrorException { BigInt answer = new BigInt("0"); //If the numbers of b1 is positive subtracts b2 negative if (!neg && b.neg) // positive - negative { answer.neg = false; answer.digit = addArr(digit, b.digit); } else if (neg && !b.neg) // negative - positive { answer.neg = true; answer.digit = addArr(digit, b.digit); } else if (!neg && !b.neg) // positive - positive { if (compare(digit, b.digit) >= 0) { answer.neg = false; answer.digit = subArr(digit, b.digit); } else { answer.neg = true; answer.digit = subArr(b.digit, digit); } } else // negative - negative { if (compare(digit, b.digit) <= 0) { answer.neg = false; answer.digit = subArr(b.digit, digit); } else { answer.neg = true; answer.digit = subArr(digit, b.digit); } } return answer; } public BigInt mul(BigInt other) throws StringErrorException { BigInt answer = new BigInt("0"); if(this.neg && other.neg) { answer.neg = false; answer.digit = mulArr(this.digit, other.digit); } else if (!this.neg && !other.neg) { answer.neg = false; answer.digit = mulArr(this.digit, other.digit); } else if (!this.neg && other.neg) { answer.neg = true; answer.digit = mulArr(this.digit, other.digit); } else if (this.neg && !other.neg) { answer.neg = true; answer.digit = mulArr(this.digit, other.digit); } return answer; } private int[] mulArr(int[] a, int[] b) { int size = (a.length + b.length); int len1 = a.length; int len2 = b.length; int count; int[] c = new int[size]; if(len1>len2) { count = a.length; } else { count = b.length; } for(int i=0; i } return removeLeadingZero(c); } public BigInt div(BigInt b) throws StringErrorException { } public BigInt modulus(BigInt b) throws StringErrorException { } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started