Results 1 to 5 of 5
Thread: adding two objects
- 09-15-2011, 03:27 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 5
- Rep Power
- 0
adding two objects
Thers a class called BigInt and it has a size and digit. size is the number of digits in the integer that is entered and digit is an array that holds the integer.
I think ive got it worked out, the only thing is that when the sum goes up a tens digit it adds a leading 0. for example if i put in 99 and 1 the sum comes out as 0100 or if i put in 999 and 1 the sum comes out as 001000. How can i get rid of the leading 0's?
This is the code
Java Code:add two BigInt’s public BigInt plus(BigInt arg) { BigInt sum = new BigInt();//Create sum object //declare and intialize carry int carry = 0; int i, temp; //change size of sum if(this.size > arg.size) sum.size = this.size; else sum.size = arg.size; //add two BigInts for(i = 0; i <= sum.size - 1; i++) { temp = this.digit[i] + arg.digit[i] + carry; System.out.println(i + " Temp is " + temp); sum.digit[i] = temp % 10; carry = temp / 10; System.out.println(i + "Carry is " + carry); System.out.println("sum.digit[" + i +"] is " + sum.digit[i]); if(temp > 9) { if(sum.digit[i] == sum.digit[sum.size-1]) { sum.size = sum.size + 1; System.out.println("The while loop works when i is " + i); } } } return sum; }
- 09-15-2011, 03:32 AM #2
Re: adding two objects
How/why are the leading 0s added? If you don't want them, don't add them.How can i get rid of the leading 0's?
Can you explain your algorithm for adding two BigInt's?
- 09-15-2011, 03:35 AM #3
Member
- Join Date
- Aug 2011
- Posts
- 5
- Rep Power
- 0
Re: adding two objects
i'm not sure how theyre added, i think they get added when i increase the size of sum in the if statement
- 09-15-2011, 03:38 AM #4
Re: adding two objects
If you don' t know where it happens, you need to debug your code to see.
Add some printlns to print out the values of the variables that are controlling the process.
-
Re: adding two objects
Similar Threads
-
Performance issue adding and removing lots of objects to Array list, need better way.
By Neilos in forum New To JavaReplies: 6Last Post: 09-05-2011, 01:25 PM -
adding objects into arrayLists
By socboy6579 in forum New To JavaReplies: 19Last Post: 12-10-2010, 06:59 AM -
Method for adding and drawing several objects
By Pillow in forum New To JavaReplies: 2Last Post: 09-08-2010, 11:24 PM -
adding or removing an object from an array of objects
By javanew in forum New To JavaReplies: 1Last Post: 05-04-2010, 11:00 AM -
read txt file,with some records, create objects and store objects in tables of a db.
By stamv in forum JDBCReplies: 1Last Post: 01-22-2009, 04:25 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks