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
SUMMARY

The discussion focuses on the implementation of the plus method in a custom BigInt class designed to handle large integers. The method aims to add two BigInt objects while managing carryover during addition. Key issues identified include incorrect indexing in the for loop, improper handling of carry values, and assumptions about the size of the digit array. The final implementation correctly calculates the sum and handles carryover, ensuring accurate results for various input sizes.

PREREQUISITES
  • Understanding of Java programming and object-oriented concepts
  • Familiarity with arrays and their indexing in Java
  • Knowledge of basic arithmetic operations and carryover in addition
  • Experience with debugging Java code and handling exceptions
NEXT STEPS
  • Review Java array handling and indexing to prevent ArrayIndexOutOfBoundsException
  • Learn about implementing carryover logic in addition algorithms
  • Explore Java debugging techniques to identify and resolve NullPointerException
  • Study best practices for designing custom data types in Java, such as BigInt
USEFUL FOR

Java developers, software engineers, and computer science students interested in implementing custom data types and algorithms for handling large integers.

  • #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
2K
  • · 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