Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Hints/Explanation public static int[] intToBigInt(int n) -- Convert the integer n into a big integer; if n < 0, then return NaBI; note that n
Hints/Explanation public static int[] intToBigInt(int n) -- Convert the integer n into a big integer; if n < 0, then return NaBI; note that n wont overflow the array, since its an int. public static int[] stringToBigInt(String s) -- Convert the String s into a big integer; if s does not represent a legal big int (i.e., contains a character other than 0 .. 9 or is longer than SIZE characters) return NaBI. public static String bigIntToString(int[] A) -- Return a String representation of the big integer (with leading zeros suppressed); if any member of A is NOT a digit (e.g., is negative or > 9) return "NaBI". public static int compareTo(int[] A, int[] B) -- Compare the two big integers A and B and return -1, 0, or 1, depending on whether A < B, A == B, or A > B, respectively. public static int[] add(int[] A, int[] B) -- Add two big integers and return a new array representing their sum; if the result overflows, i.e., contains more than SIZE digits, return NaBI Code Below import java.util.Arrays; // so that we can print out arrays using Arrays.toString(...) public class BigInt { public static final int SIZE = 20; // length of arrays representing big ints public static final int[] NaBI = { -1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; // error value public static int[] intToBigInt(int n) { // your code here return null; // just to get it to compile } public static int[] stringToBigInt(String s) { // your code here return null; // just to get it to compile } public static String bigIntToString(int[] A) { // your code here return ""; // just to get it to compile } // Compare the two big integers A and B and return -1, 0, or 1, depending // on whether A < B, A == B, or A > B, respectively. public static int compareTo(int[] A, int[] B) { // your code here return 0; // just to get it to compile } // Add two big integers and return a new array representing their sum; if the result overflows, // i.e., contains more than SIZE digits, return NaBI. public static int[] add(int[] A, int[] B) { // your code here return null; // just to get it to compile }
public static void main(String [] args) { System.out.println(" Unit Test for BigInt Library. "); System.out.println("Test 1: Should be 1234567"); int[] A = { 0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7 }; System.out.println(BigInt.bigIntToString(A)); System.out.println(); System.out.println("Test 2: Should be 0"); int[] B = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; System.out.println(BigInt.bigIntToString(B)); System.out.println(); System.out.println("Test 3: Should be NaBI"); int[] C = { 0,0,0,0,23,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; System.out.println(BigInt.bigIntToString(C)); System.out.println(); System.out.println("Test 4: Should be [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7]"); A = BigInt.intToBigInt( 1234567 ); System.out.println(Arrays.toString(A)); System.out.println(); System.out.println("Test 5: Should be 1234567"); System.out.println(BigInt.bigIntToString(A)); System.out.println(); System.out.println("Test 6: Should be 0"); B = BigInt.intToBigInt( 0 ); System.out.println(BigInt.bigIntToString(B)); System.out.println(); System.out.println("Test 7: Should be NaBI"); C = BigInt.intToBigInt( -4 ); System.out.println(BigInt.bigIntToString(C)); System.out.println(); System.out.println("Test 8: Should be [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 4, 1, 5, 9, 2]"); System.out.println(Arrays.toString( BigInt.stringToBigInt( "3141592" ) )); System.out.println(); System.out.println("Test 9: Should be 0"); System.out.println(BigInt.bigIntToString( BigInt.stringToBigInt( "0" ) )); System.out.println(); System.out.println("Test 10: Should be NaBI"); System.out.println(BigInt.bigIntToString( BigInt.stringToBigInt( "234h56" ) )); System.out.println(); System.out.println("Test 11: Should be NaBI"); System.out.println(BigInt.bigIntToString( BigInt.stringToBigInt( "23456379238472937829384272939472937429374" ) )); System.out.println(); System.out.println("Test 12: Should be 0"); System.out.println(BigInt.compareTo( BigInt.stringToBigInt("12375"), BigInt.stringToBigInt("12375"))); System.out.println(); System.out.println("Test 13: Should be -1"); System.out.println(BigInt.compareTo( BigInt.stringToBigInt("12375"), BigInt.stringToBigInt("12378"))); System.out.println(); System.out.println("Test 14: Should be 1"); System.out.println(BigInt.compareTo( BigInt.stringToBigInt("12395"), BigInt.stringToBigInt("12375"))); System.out.println(); System.out.println("Test 15: Should be 0"); System.out.println(BigInt.compareTo( BigInt.stringToBigInt("0"), BigInt.stringToBigInt("0"))); System.out.println(); System.out.println("Test 16: Should be -1"); System.out.println(BigInt.compareTo( BigInt.stringToBigInt("0"), BigInt.stringToBigInt("12375"))); System.out.println(); System.out.println("Test 17: Should be 1"); System.out.println(BigInt.compareTo( BigInt.stringToBigInt("111"), BigInt.stringToBigInt("0"))); System.out.println(); System.out.println("Test 18: Should be 123456789123456789"); System.out.println(BigInt.bigIntToString(BigInt.add( BigInt.stringToBigInt("36182736036182736"), BigInt.stringToBigInt("87274053087274053")))); System.out.println(); System.out.println("Test 19: Should be 3141592653598"); System.out.println(BigInt.bigIntToString(BigInt.add( BigInt.stringToBigInt("0"), BigInt.stringToBigInt("3141592653598")))); System.out.println(); System.out.println("Test 20: Should be 10000000000000000000"); System.out.println(BigInt.bigIntToString(BigInt.add( BigInt.stringToBigInt("9999999999999999999"), BigInt.stringToBigInt("1")))); System.out.println(); System.out.println("Test 21: Should be NaBI"); System.out.println(BigInt.bigIntToString(BigInt.add( BigInt.stringToBigInt("99999999999999999999"), BigInt.stringToBigInt("1")))); System.out.println(); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started