Look up the JavaDocs for the Integer class and read page
254 in the text, about Integer objects and the int primitive
type. We will create an ArrayList of Integers, but you
can think of them as ints because (in Java 1.5) an int is
automatically converted to an Integer when put into a
collection. Review the arithmetic operators in Appendix D; note that
"/" does integer division (dropping any remainder) and "%" is the
"modulo" operator which gives the remainder.
The primitive int and Integer types provided by
Java have only limited width. Values of type byte are
eight bits in width, values of
type char or short are sixteen bits in
width,
values of type int or Integer are thirty-two
bits in width,
and values of type long are sixty-four bits in width.
For some applications, fixed-precision values are an inconvenience:
what you would rather have is a data type capable of representing
integer values of arbitrarily many bits. Such a data type is
traditionally
called a "bignum".
In this lab, you are to implement a class Bignum, whose
objects represent arbitrary-precision non-negative integers. Bignum
objects can be implemented using lists; in particular, you can
represent
a Bignum by the list of its digits, say, in base 10.
For example, the value 123 could be represented by a
list containing Integer objects with values 1, 2,
and 3. Or, you might find it more convenient to list the digits in
reverse order, so that 123 is represented by the list
of values 3, 2, 1.
Your Bignum class should have the following skeleton,
providing at least the methods indicated below.
import.java.util.ArrayList;
public class Bignum {
public Bignum(int intValue);
public Bignum add(Bignum num);
public Bignum mul(Bignum num);
public Bignum exp(int n);
public String toString();
}
The constructor creates a Bignum having a specified
integer value.
The toString method should create a printable base-10
representation of a Bignum value.
The add method adds the Bignum "num"
to "this" Bignum, and returns a Bignum
representing
the sum. The mul method behaves
similarly, implementing multiplication.
Note that Bignum objects should be immutable:
once a Bignum object has been created, there should be no
way
to change the value it represents. So add
and mul return a new value, rather than
modifying "this" Bignum.
Your code for add and mul
can work by simulating the "grade-school" algorithms you learned as
a child. For example, you can implement add by working
one digit at a time starting from the least-significant
(i.e. the "ones") digit and progressing toward the
most-significant digit, keeping track of the current "carry" as you go.
You may want to make an accessor to get the kth digit of a Bignum. For mul,
you might find it helpful to first implement a
private method for multiplying an arbitrary Bignum by
a single digit. You may want to make a shiftLeft(k) method which return
this times 10 to the kth power.You can then implement mul
by performing
several such multiplications by single-digits and using add
to add up the results. The exponentiation method takes an int argument,
as we will not need large exponents, and can work by repeated
multiplication.
You can test your class with a Test class that has a
constructor similar to this:
public Test()
{
Bignum x = new
Bignum(100004); // a sample product
Bignum y = new Bignum(33);
Bignum z = x.mul(y);
System.out.println(x.toString() +
" * " + y.toString() + " = " + z.toString());
for (int i=0; i<=50; i++)
{ // a table of
powers of two
System.out.println(i + ":
" + new Bignum(2).exp(i).toString());
}
System.out.println(new
Bignum(987).exp(23).toString()); // 987^23
}
Once you have coded and tested your Bignum class, use
it to compute and print out the exact value of 987 to the power
23. Include the answer in your email to the TA with your Bignum
jar
file.