So I've been working on this BigNum multiplication method (in short the method takes in a BigNum other and is supposed to return the product of two large positive integers without using the bigint class) for a while and I'm almost done however, I am still having issues appending zeroes. My helper method does not seem to be adding correctly either (as an ex. 444*4 should return as "1776" however it returns as "161616".) I need someone to debug this and help me figure out why it isn't working. Any help is appreciated.
Here's the result I get when I try doing 444*444 as an example
the expected output should be:
1776
17760
177600
197136
actual output with my code:
161616
1616160
1616160
3393936
My methods
/**Multiplies two <tt>BigNum<tt> values together and returns a new
*<tt>BigNum<tt> object with the resulting value.
*
*@param other object
*@returns a new BigNum with resulting value
*/
public BigNum mult(BigNum other) {
BigNum tmp = new BigNum();
BigNum acc = new BigNum();
String s="";
int count=0;
for(int i= 0; i < other.num.length() ; i++) { //each digit x of other
tmp = this.mult(Character.getNumericValue(other.num.charAt(i)));
if(i > 0) {
for(int j=0; j < i; j++) {
s = tmp.num + "0";
}
}else {
s = tmp.num;
}
tmp=new BigNum(s);
count++;
acc = acc.add(tmp);
}
return acc;
}
/**Helper method that adds the other value a set of number of times, 0-9
*
*@param and int n and other object
*@returns resulting value
*/
public BigNum mult(int n) {
String result;
int carry;
if(n==0){
result="0";
}
else{
carry =0;
result = "";
}
for(int i=this.num.length()-1; i >=0; i--){
int temp = n * Character.getNumericValue(this.num.charAt(i))
result=(temp%10) + result;
carry = temp/10;
if(carry > 0){
result = carry + result;
}
}
return new BigNum(result);
}