Java BigInt Class: Adding Two Objects with Carryover | Plus Method

  • Context: Comp Sci 
  • Thread starter Thread starter apiwowar
  • Start date Start date
  • Tags Tags
    Java
Click For Summary

Discussion Overview

The discussion revolves around the implementation of a method called "plus" in a Java class named BigInt, which is designed to add two BigInt objects together while handling carryover in a manner similar to traditional addition. Participants explore the method's logic, the handling of digit arrays, and the correct implementation of carryover during the addition process.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes the structure of the BigInt class, including its size and digit array, and presents an initial implementation of the plus method.
  • Another participant suggests that the local variable "number" is unnecessary and questions the assumption of a fixed size for the digit array.
  • Concerns are raised about the for loop's direction and the need to handle cases where the two BigInts have different sizes.
  • Participants discuss the need to correctly implement carryover logic and the appropriate use of terms like "carry" versus "remainder."
  • One participant proposes comparing the sizes of the two BigInts to determine the starting point for the addition loop.
  • Another participant points out an off-by-one error in the array indexing, suggesting the loop should iterate from 49 to 0 instead of 50 to 0.
  • There are mentions of issues with the current implementation returning incorrect sums, particularly when the sum exceeds a single digit.
  • Participants discuss the timing of carry calculations and suggest reversing the order of operations in the carry logic to resolve issues with sums greater than 9.
  • One participant notes that the implementation produces incorrect results when adding certain combinations of numbers, indicating further refinement is needed.

Areas of Agreement / Disagreement

Participants generally agree on the need for corrections and improvements to the plus method, but there is no consensus on the final implementation or the best approach to handle carryover and array indexing issues.

Contextual Notes

Limitations include assumptions about the maximum size of the BigInt objects and the handling of carryover logic, which remains unresolved in the current discussion.

  • #31
i changed my test code to what i posted below. it seems that the numbers go in right. num3.digit[47] is 1, num3.digit[48] is 0 and num3.digit[49] is 0 and everything before 47 is 0. but if i just print out num 3 it comes out as 001000...

Code:
import java.util.Scanner;

public class BigIntSimpleTestAlt
 {
	public static void main(String[] args)
	{

	Scanner input = new Scanner(System.in);
	
	//int number1 = input.nextInt();
	//int number2 = input.nextInt();
	BigIntAlternate num1 = new BigIntAlternate(99);
		
	BigIntAlternate num2 = new BigIntAlternate(1);
	
	BigIntAlternate num3 = new BigIntAlternate();
	
	num3 = num1.plus(num2);
	for(int i = 0; i <= 49; i++)
	{
	
	
	System.out.println(i + " Num1 + Num2 = " + num3.digit[i]);
	}
	System.out.println("\ncompare num1 and num2 " + num1.compareTo(num2));
	
	}
}
 
Physics news on Phys.org
  • #32
So don't use print or println to display all of the digits. What you did by using a loop to go through each element in the digit array is fine.

Does this work?
Code:
System.out.print(num3.digit)

An even better solution would be to write print and println methods for your class to display the digit array.

The code would look like this:
Code:
// Display the digits of a BigNum object.
void print(BigNum bn)
{
   for (int i = 0; i < max; i++)
   {
      print(bn.digit[i]);
   }
}

The code for println would be almost identical, except that it would print a newline character after the last digit.

Your last code snippet is much improved, but could use two tweaks - all of the statements in the body of main should be indented, and the body of the for loop should be indented, like the following. I've deleted a few of the lines you aren't using.

Code:
public static void main(String[] args)
{
   BigIntAlternate num1 = new BigIntAlternate(99);
   BigIntAlternate num2 = new BigIntAlternate(1);
   BigIntAlternate num3 = new BigIntAlternate();
	
   num3 = num1.plus(num2);
   for(int i = 0; i <= 49; i++)
   {
      System.out.println(i + " Num1 + Num2 = " + num3.digit[i]);
   }
   System.out.println("\ncompare num1 and num2 " + num1.compareTo(num2));
}
 

Similar threads

  • · Replies 23 ·
Replies
23
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K