Question: Problem 4 : BigInt: a class for large integers 6 0 points total; individual - only Overview In Java, the int data type can only
Problem : BigInt: a class for large integers
points total; individualonly
Overview
In Java, the int data type can only represent integers from to This isnt big enough to
store very large numbersfor example, the population of Earth approximately billion as of or to compute n for large values of n
The Java libraries contain a builtin class called BigInteger to handle arbitrarily large integers. In this problem, you will write your own class called BigInt that has a subset of the functionality of the builtin Java class.
Representing integers using an array of digits
Each BigInt object will use an arrays of integers, where each element of the array represents a single digit of the integer.
We will design the class to handle nonnegative integers with up to digits, and thus the array will have a length of
For example, the integer would be represented using this array:
Note that:
Each element of the array is a single digit ie an integer from
The last element of the array represents the least significant digit of the integer ie the rightmost digit
The first nonzero element if there is one represents the most significant digit of the integer. The one exception is when we are representing itself. Its most significant digit is the same as its least significant digit, and it is a zero!
The remaining elements are leading zeros that are not significant digits.
As another example, the integer for billion would be represented using the array
Task : Download and review our starter code
Begin by downloading the following starter file: BigInt.java
Open this file in your IDE and review its contents. We have given you:
a class constant called MAXSIZE that represents the largest number of digits that a BigInt object can have. It will be used when constructing the array of digits.
an instance member digits, which holds a reference to the array of integers in which the digits are stored. The array of digits represents the large integer.
an instance member numSigDigits, which stores the number of significant digits in the integer. This is the same as the number of digits in the integer when it is written without leading zeroes. For example, for a BigInt that represents the integer numSigDigits should be and for a BigInt that represents the integer numSigDigits should be This internal member should be correct for all objects, including the objects that are produced when computing the sum and the product.
a default constructor ie one that takes no arguments; it creates a BigInt object representing the number
a set of tests in the body of a main method. At the moment, these tests are commented out, but you should gradually uncomment them and run them as you add functionality to the class.
Important guidelines
You may not add any additional fields to the BigInt class.
You may not use any variables of type long, any of Javas builtin classes for numbers Long BigInteger, etc. or any of Javas builtin collection classes eg ArrayList
Task : Add your first of several custom constructors
Add a custom constructor with the following header:
public BigIntint arr
It should use the contents of the array that is passed in as the basis of the new BigInt object. For example, we should be able to create a BigInt for as follows:
int arr ;
BigInt val new BigIntarr;
Suggested approach:
Validate that a valid array has been passed to the Constructor and, the array has a length between and MAXSIZE. If the parameter is null, if the length of the array is greater than MAXSIZE, or if any of the elements of the array dont make sense as digits, the method should throw an IllegalArgumentException.
Note: You may want to consider writing a private helper method that helps validate that an element is a valid digit. In general, we encourage you to use a private helper method whenever doing so would help to decompose and simplify your code andor your logic.
Create and initialize the digits array from the input array arr. You cannot assume that the array passed to the method has a length of MAXSIZE. It cannot have a length greater than MAXSIZE but, it can be less. Make sure to validate each element before assigning to the digits array.
The constructor should also determine and initialize the correct value for the numSigDigits field. Make sure that you dont include nonsignificant leading zeroes in the value that you compute for numSigDigits.
Important: Make sure that the digits field ends up referring to a new array of length MAXSIZE. You should not make it refer to the array that is passed in
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
