Home CPSC 340

Assignment 2 - Big Ints


Due: February 17



To gain experience using dynamic arrays, dealing with references, and implementing a data structure.



In Java, integers have size limits. An integer is 4 bytes. The biggest integer type, long, is 8 bytes or 64 bits. This type can hold values up to $2^{64} - 1$, or $18446744073709551615$. If we want to store values larger than this, we can't use any built in type to do it.

Your task for this assignment is to write a class, called BigInt that can store non-negative numbers of any size, from 0 all the way up without limit (although, at some point, a computer can run out of memory). Your class will store the digits of the number in an array, and provide a number of methods to allow users of the class to use it.

The use of this class is demonstrated in this main program which you can use to test your BigInt class. For this program, you aren't writing your own main function, you can simply use the one provided.



  1. The BigInt class should store the number as an array of digits. Your class should create the array of digits in the constructor.
  2. Your class should have a default constructor (one with no parameters) that sets the BigInt to the value of 0.
  3. There must be another constructor that takes an int as a parameter and sets the BigInt to the value of that parameter.
  4. A third constructor will take a String as a parameter and set the BigInt to the value of the string (you can assume that the string contains only digits). This will allow us to create BigInts that won't fit in standard integers such as:
    BigInt large = new BigInt("10000000000000000000000000");
  5. In order to print our your BigInt, you should have a method called "toString" which returns a String representation of the BigInt. It should contain all of the digits with no extra text.
  6. You should create a method called "add" which will allow the addition of two BigInt objects. The method should take a second BigInt as a parameter, and return the sum of the two objects. It should make the sum as a new BigInt, not modify the current object. You should use the same algorithm that you use to add numbers using pencil and paper — and remember to carry!
  7. You should create a method called "compareTo" which will allow us to test the order of BigInts. It should take a second BigInt as a parameter. It will return -1 if this BigInt is less than the second, 1 if it is greater, and 0 if they are equal.
  8. All BigInts will be non-negative!




Extra Credit

For extra credit, also provide a method called "multiply" which takes a second BigInt as a parameter and returns the product.


General Requirements



You can view my solution here:




When you are done, submit your program by emailing your code to ifinlay@umw.edu.

Copyright © 2019 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.