Instructions
Objective
Write a java program to implement big int in java.
Requirements and Specifications
Create a class BigInt which is used for storing very large integer values. The class should have a private array or arraylist of integers that stores the digits of the BigInt and a boolean to indicate positive/negative Add methods for BigInt add(BigInt other), BigInt subtract(BigInt other) and String toString() Include unit tests to ensure 100% code coverage
Source Code
BIG INT
import java.util.ArrayList;
import java.util.List;
import jdk.jfr.Timestamp;
public class BigInt {
int number;
List<Integer> digits = new ArrayList<Integer>();
boolean positive;
public BigInt(int n)
{
number = n;
positive = true;
if(number < 0)
positive = false;
// convert integer to string
String number_str = String.valueOf(number);
// Now, get all digit as character, convert it to int and add to the list
int startIdx = 0;
if(!positive)
startIdx = 1;
for(int i = startIdx; i < number_str.length(); i++)
{
Character c = number_str.charAt(i);
int digit = Integer.valueOf(String.valueOf(c));
digits.add(digit);
}
}
// getters
public boolean isPositive() {return positive;}
public List<Integer> getDigits() {return digits;}
public int getOriginalNumber() {return number;}
// methods
public BigInt add(BigInt other)
{
return new BigInt(number + other.getOriginalNumber());
}
public BigInt subtract(BigInt other)
{
return new BigInt(number - other.getOriginalNumber());
}
@Override
public String toString()
{
return String.valueOf(number);
}
}
TEST BIG INT
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class TestBigInt {
@Test
void testConstructor() {
// Test positive integer
BigInt myint = new BigInt(159156859);
assertEquals(myint.isPositive(), true);
// Test negative integer
BigInt myint2 = new BigInt(-1591568598);
assertEquals(myint2.isPositive(), false);
// Test the number of digits
assertEquals(myint.getDigits().size(), 9);
assertEquals(myint2.getDigits().size(), 10);
}
@Test
void testDigitsList()
{
// Test that the list contains the correct digits in the correct positions
BigInt myint = new BigInt(123456789);
assertEquals(myint.getDigits().size(), 9);
// Test that the digits are in the correct position
int[] digits = {1,2,3,4,5,6,7,8,9};
for(int i = 0; i < myint.getDigits().size(); i++)
assertEquals(myint.getDigits().get(i), digits[i]);
}
@Test
void testAdd()
{
BigInt myint1 = new BigInt(100000000);
BigInt myint2 = new BigInt(123456789);
// Test 1
BigInt result = myint1.add(myint2);
int[] result_digits = {2,2,3,4,5,6,7,8,9};
for(int i = 0; i < result.getDigits().size(); i++)
{
assertEquals(result.getDigits().get(i), result_digits[i]);
}
// Test 2
myint2 = new BigInt(-5897456);
result = myint1.add(myint2);
result_digits = new int[] {9,4,1,0,2,5,4,4};
for(int i = 0; i < result.getDigits().size(); i++)
assertEquals(result.getDigits().get(i), result_digits[i]);
}
@Test
public void testSubtract()
{
BigInt myint1 = new BigInt(100000000);
BigInt myint2 = new BigInt(123456789);
// Test 1
BigInt result = myint1.subtract(myint2);
int[] result_digits = {2,3,4,5,6,7,8,9};
for(int i = 0; i < result.getDigits().size(); i++)
assertEquals(result.getDigits().get(i), result_digits[i]);
// Test that the number is negative
assertEquals(result.isPositive(), false);
// Test 2
myint2 = new BigInt(-5897456);
result = myint1.subtract(myint2);
result_digits = new int[] {1,0,5,8,9,7,4,5,6};
for(int i = 0; i < result.getDigits().size(); i++)
assertEquals(result.getDigits().get(i), result_digits[i]);
// The result of 100000000-(-5897456) is a positive number
assertEquals(result.isPositive(), true);
}
@Test
public void testToString()
{
// Test 1
BigInt myint = new BigInt(158974563);
assertEquals(myint.toString(), "158974563");
// Test 2
myint = new BigInt(-102532145);
assertEquals(myint.toString(), "-102532145");
// Test 3
myint = new BigInt(-987654321);
BigInt myint2 = new BigInt(100000000);
BigInt result = myint.add(myint2);
assertEquals(result.toString(), "-887654321");
// Test 4
result = myint.subtract(myint2);
assertEquals(result.toString(), "-1087654321");
}
}