Cryptography Reference
In-Depth Information
FIGURE 2.1
1
0
1
4
9
8
Digits
3
2
Negative
True
7
000723894101 as in Figure 2.1. Note that
we disregard any leading zeros. The exception to this will be when we represent zero; the
array will have the single digit 0.
So, for example, we would store the integer
2.1
CONSTRUCTORS
For constructors, we should have the following:
1.
An Int constructor which can convert an atomic type int to an Int,
2.
One which can convert a string into an Int,
3.
One which copies an Int to a new Int, and
4.
The default constructor (one which accepts no parameters). This one will set the Int to
zero.
For the first constructor listed, we consider how this will be done. Suppose we want to
convert the int
562 into an Int. We could set aside enough space for ten digits, since
the largest int is around 2 billion and would require no more than ten decimal digits.
We first take note of the sign of the number, and then set the boolean negative field to
either true or false. We could then successively divide
=
n
by ten, keeping the remainder each
time and placing it in the array starting at the rear. If we have any unfilled slots in the array
when we are finished, we can move forward the elements at the rear. The code to accom-
plish this follows:
n
public Int(int n) {
//Produce the array-an int can not have more than 10 decimal digits
int[] temp=new int[10];
//zero is a special case
if (n==0) {
negative=false;
 
Search WWH ::




Custom Search