Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Fubarable

Thread: adding two objects

  1. #1
    apiwowar is offline Member
    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    14,793
    Rep Power
    20

    Default Re: adding two objects

    How can i get rid of the leading 0's?
    How/why are the leading 0s added? If you don't want them, don't add them.

    Can you explain your algorithm for adding two BigInt's?

  3. #3
    apiwowar is offline Member
    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Default 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

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    14,793
    Rep Power
    20

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

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,252
    Blog Entries
    1
    Rep Power
    24

    Default Re: adding two objects

    Quote Originally Posted by apiwowar View Post
    i'm not sure how theyre added, ...
    Is this your code? Or have you found it somewhere?
    Norm likes this.

Similar Threads

  1. Replies: 6
    Last Post: 09-05-2011, 01:25 PM
  2. adding objects into arrayLists
    By socboy6579 in forum New To Java
    Replies: 19
    Last Post: 12-10-2010, 06:59 AM
  3. Method for adding and drawing several objects
    By Pillow in forum New To Java
    Replies: 2
    Last Post: 09-08-2010, 11:24 PM
  4. Replies: 1
    Last Post: 05-04-2010, 11:00 AM
  5. Replies: 1
    Last Post: 01-22-2009, 04:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •