Cryptography Reference
In-Depth Information
digits=new int[1];
digits[0]=0;
return;
}
//Negative int n-set negative to true, take absolute value of n
if (n<0) {
negative=true;
n=Math.abs(n);
}
int count=10;
//Divide by 10 until nothing left
while (n>0) {
//Remainder is the count-th digit in the array
temp[—count]=n%10;
n/=10;
}
//Remove any leading zeros-make new array and copy
digits=new int[temp.length-count];
for (int i=0;i<digits.length;i++) digits[i]=temp[count+i];
}
The constructor which produces a copy of an Int from another Int, and the Int() con-
structor which accepts no parameters, are easy to code. The Int() constructor should set the
Int to zero.
//This one produces an array of one int containing 0
public Int() {
negative=false;
digits=new int[1];
digits[0]=0;
}
public Int(Int n) {
negative=n.negative;
digits=new int[n.digits.length];
for (int i=0;i<digits.length;i++) digits[i]=n.digits[i];
}
We now develop a constructor that produces Int objects from strings. We can arrange it
so that it will parse strings to determine whether or not it can be interpreted as an Int, then
place the characters (converted to ints) in the array. In case the string cannot be parsed as
an Int, we can throw an IntException:
public class IntException extends Exception {
public IntException() {super();}
public IntException(String s) {super(s);}
}
Search WWH ::




Custom Search