Lab 2: Classes and Encapsulation


  1. Component-view: Implementing Big Natural Numbers

    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.

    Name

    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).

    Constructors

    Your class must provide three constructors:

    Methods

    Your class must provide the following methods:

    Checking Method Signatures

    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.

  2. Client-view: Using Big Natural Numbers

    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.

  3. Submission

    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.