A natural number is a number greater than or equal to 0.
Java does not provide a primitive type that corresponds
to natural numbers:
Only boolean and char
are unsigned and neither provides very many
distinct values
(2 in the case of boolean and
65,536 in the case of char).
Your task here is to implement a class that supports arbitrary-sized natural numbers (that is, limited only by memory availability for the JVM, not by some constant limit defined a priori by your class). This class is similar, at least in intent, to the Natural component from the Resolve/C++ component catalog.
Note that Java does provide, as part of the standard libraries,
an implementation of an arbitrary-sized integer (BigInteger).
For the purpose of this lab, however, you are asked not to use the
BigInteger class in your own solution.
Specific requirements for your implementation are given below.
Your class must be named BigNatural. It should be in the default package (i.e., it should not be explicitly declared to be in any package).
Your class must provide three constructors:
int argument.
For the purposes of this lab, you should assume that the constructor will
never be called with a negative argument. That is, you do not have
to handle this error condition.
Your class must provide the following methods:
increment, which increases the value of the
number by 1 (and returns nothing)
decrement, which decreases the value of the
number by 1 if possible, otherwise leaves it unchanged (and returns nothing).
toString, which returns a string representing the value
of the natural number. The string should contain only the digits of the
number—no commas, decimals, leading zeros, or other extraneous characters.
For example, this method would return "0", "134", and "4332993" for the
corresponding natural numbers.
To help you check that you have implemented all the required method signatures, a small test program is provided here. Your implementations of the class described above should compile with this test program. Note: this test program is not a very thorough test of the functionality of your solution. Grading will be carried out with a more exhaustive test program.
Write a TestDriver class with a main method
that creates and exercises instances of your BigNatural class.
At the very least, this client should illustrate the use of every
method (and constructor) of BigNatural.
Submit the .java files (not .class) for both your BigNatural
and your TestDriver implementations. Include a readme text
file that explains what output is expected from running your program.
Your readme file should also describe how to run your program, even though
these instructions will probably be as simple as
just executing java TestDriver.