Writing your own BigInteger Class in JAVA
Submitted by moazkhan on Sunday, June 29, 2014 - 23:28.
TUTORIAL NO 9
Writing your own BigInteger Class
In this tutorial you will learn: 1.Console Applications 2.Input in console 3.Strings 4.Arrays 5.Conversion of Strings Today I decided to write a different type of tutorial. Instead of working with GUI I decided to make a console application this time only because we didn’t made any console application in our tutorials. So today I am going to teach you how to make a BigInteger class. Integer occupies 8 bytes i.e 32bits in memory and the last 32nd bit is reserved for the sign. What if we want to handle a number greater than 32bit’s. So today we will solve this problem and make our own bigInteger class we will make it only for addition today to keep things simple and you can extend the functionality later on. Basic step: Download and install ECLIPSE and set up a JAVA PROJECT. Then create a new class and name it BigInteger. Then follow the steps 1.IMPORT STATEMENTS First of all write this import statements in your .java file- import java.util.*;
- boolean valid_num1 = true;
- boolean valid_num2 = true;
- int zeros_count = 0;// keeping track of zeros
- int carry = 0; // keeping track of end carry
Now we will write a print statement and then take an input using the nextLine function which will take a string input. Then we will store the string to num1 variable. After the second print statement we will take the input and store it in the num2 variable which is the number we want to add in the first number
CONDITIONS:
- if(num1.length() != num2.length()) // if num1 is not equal to num2 then add zeros in the smaller number
- {
- if(num1.length()>num2.length()){
- zeros_count = num1.length()-num2.length(); // difference of length of two strings will give us the required number of zeros
- for(int i = 1; i<=zeros_count;i++)
- num2 = zero+num2;
- }
- else{
- zeros_count = num2.length()-num1.length(); // difference of length of two strings will give us the required number of zeros
- for(int i = 1; i<=zeros_count;i++)
- num1 = zero+num1;
- }
- }
- char[] temp_num1 = num1.toCharArray();
- char[] temp_num2 = num2.toCharArray(); // converting both number to char array
- for(int x = 0; x<temp_num1.length; x++) // checking for invalid entries in number1
- {
- if((temp_num1[x] < '0' | temp_num1[x] > '9'))
- {
- valid_num1 = false;
- }
- }
- for(int x = 0; x<temp_num2.length; x++) // checking for invalid entries in number2
- {
- if((temp_num2[x] < '0' | temp_num2[x] > '9'))
- {
- valid_num2 = false;
- }
- }
- if((valid_num1 == true) && (valid_num2 == true))
- {
- for(int i=temp_num1.length-1; i >= 0; i--)
- {
- if(((temp_num1[i]-'0')+(temp_num2[i])+carry)<='9') //subtracting ascii of 0 and then adding to number2 and the carry
- {
- ans=(char) ((temp_num1[i]-'0')+(temp_num2[i])+carry)+ans;
- carry = 0;
- }
- else
- {
- ans=(char) (((temp_num1[i])-'0'+(temp_num2[i])+carry)-10)+ans; //subtracting ascii of 0 and then adding to number2 and the end carry
- carry = 1;
- }
- }
- if(carry ==1)
- ans="1"+ans; // if the final end carry is 1 add one to the string
- }
- else
- return;
- }//end main
- }//end class

Add new comment
- 620 views