HugeInt Class
/**
* HugeInt
*
* Framework for developing HugeInt class
*
* Currently only equals(), toString(), and
* a String constructor have been written.
*/
public class HugeInt {
private static final int MAX_DIGITS = 40;
private int[] digits = new int[MAX_DIGITS];
private int digitCount = 0;
private boolean negative = false;
public HugeInt(String str) {
if (str.length() > 0) {
if (str.charAt(0) == '-') {
negative = true;
str = str.substring(1);
}
int strLen = str.length();
for (int i = 0 ; i < strLen; i++) {
char ch = str.charAt(i);
int digit = ch - '0';
digits[i] = digit;
}
digitCount = strLen;
}
}
public boolean equals(Object other) {
boolean result = false;
if (this == other) {
result = true;
}
else if (other != null && other instanceof HugeInt) {
HugeInt tmp = (HugeInt)other;
if (this.digitCount == tmp.digitCount &&
this.negative == tmp.negative) {
int i = 0;
boolean equal = true;
do {
equal = (this.digits[i] == tmp.digits[i]);
i++;
} while (equal && (i < digitCount));
result = equal;
}
}
return result;
}
public String toString() {
StringBuffer tmp = new StringBuffer();
if (negative) {
tmp.append("-");
}
for (int i = 0; i < digitCount; i++) {
tmp.append(digits[i]);
}
return tmp.toString();
}
//=======================================
// Test/Debug
//=======================================
public static void main(String[] args) {
HugeInt a = new HugeInt("-1234");
HugeInt b = new HugeInt("123");
HugeInt c = new HugeInt("-1234");
System.out.println(a);
System.out.println("true " + a.equals(a));
System.out.println("false " + a.equals(b));
System.out.println("true " + a.equals(c));
System.out.println("false " + a.equals(null));
System.out.println("false " + a.equals("Hello"));
}
}