Question: import java.util.Arrays; class HugeInteger { static int hugeIntegerSize = 300; enum Sign { POS, NEG } private byte[] digits; private Sign sign; private int intvalue;
import java.util.Arrays;
class HugeInteger {
static int hugeIntegerSize = 300;
enum Sign { POS, NEG }
private byte[] digits; private Sign sign; private int intvalue;
HugeInteger(int length) {
this.sign = Sign.POS; this.digits = new byte[length]; intvalue = 0;
}
HugeInteger(String str) { intvalue = Math.abs(Integer.valueOf(str)); digits = new byte[hugeIntegerSize]; if (str.charAt(0) == '-') { sign = Sign.NEG; str = str.substring(1); } else { sign = Sign.POS; } int i = str.length() - 1; int j = 0; while (i >= 0) { digits[j++] = (byte) (str.charAt(i--) - '0'); } }
public int getLength() { return hugeIntegerSize; }
public byte getDigit(int index) { try { return this.digits[index]; } catch (IndexOutOfBoundsException e) { System.out.print("Index is out of bounds "); return -1; } }
public void print(HugeInteger i) { String pos0 = (i.sign == Sign.NEG) ? "-" : ""; String printable = new String(); printable += (pos0); for (int j = 0; j < i.digits.length; j++) { String stringed = String.valueOf(i.digits[j]); printable += stringed; } System.out.println(printable); }
public String toString() { String pos0 = (this.sign == Sign.NEG) ? "-" : ""; String printable = new String(); printable += (pos0); for (int j = 0; j < this.digits.length; j++) { String stringed = String.valueOf(this.digits[j]); printable += stringed; } return printable; }
public void assign(HugeInteger i) { this.sign = i.sign; this.digits = new byte[i.digits.length]; for (int j = 0; j < i.digits.length; j++) { this.digits[j] = i.digits[j]; } }
public boolean equal(HugeInteger i) { for (int j = 0; j < this.digits.length; j++) { if (this.digits[j] != i.digits[j] || this.sign != i.sign) { return false; } } return true; }
public boolean greaterThan(HugeInteger i) { // checking if they are equal if (i.equal(this)) { return false; }
// in case only one of them is negative if (i.sign != this.sign) { if (i.sign == Sign.NEG) { return true; } return false; }
// in case both are positive boolean bothPositive = (this.sign == Sign.POS) ? true : false; for (int j = this.digits.length - 1; j > -1; j--) { if (this.digits[j] < i.digits[j]) { return bothPositive ? false : true; } if (this.digits[j] > i.digits[j]) { return bothPositive ? true : false; } } // default case return false; }
public boolean lessThan(HugeInteger i) { return !this.greaterThan(i); }
public void add(HugeInteger i) {
if (this.sign != i.sign) { i.invertSign(); this.subtract(i); i.invertSign(); // Restore the sign of the argument return; }
byte carry = 0; byte[] result = new byte[i.digits.length]; int sum = 0; for (int j = 0; j < this.digits.length; j++) { sum = this.digits[j] + i.digits[j] + carry; if (sum > 9) { carry = 1; byte pos1 = (byte) Integer.toString(sum).charAt(1); byte insertable = (byte) (pos1 - 48); result[j] = insertable; } else { carry = 0; result[j] = (byte) sum; }
} this.digits = result; }
public boolean isZero() { for (int i = 0; i < hugeIntegerSize; i++) { if (digits[i] != 0) { return false; } } return true; }
public void subtractHelper(HugeInteger i) {
if (this.sign != i.sign) { i.invertSign(); this.add(i); i.invertSign(); // Restore the sign of the argument return; }
int borrow = 0; for (int j = 0; j < hugeIntegerSize; j++) { int difference = (digits[j] - borrow) - i.getDigit(j); if (difference < 0) { difference += 10; borrow = 1; } else { borrow = 0; } digits[j] = (byte) difference; }
if (isZero()) { sign = Sign.POS; } else if (this.lessThan(i)) { sign = i.sign; }
}
public void subtract(HugeInteger i) {
HugeInteger result = new HugeInteger(hugeIntegerSize);
Sign thisSignStore = this.sign; Sign iSignStorer = i.sign;
this.sign = Sign.POS; i.sign = Sign.POS;
boolean thisIsGreater = this.greaterThan(i) ? true : false;
this.sign = thisSignStore; i.sign = iSignStorer;
if (thisIsGreater) { this.subtractHelper(i); result.assign(this); result.sign = this.sign; } else { i.subtractHelper(this); this.invertSign(); result.assign(i); result.sign = this.sign;
} this.assign(result); }
void invertSign() { Sign newSign = (this.sign == Sign.NEG) ? Sign.POS : Sign.NEG; this.sign = newSign; }
public String brutalMultiply(HugeInteger i) { int x = this.intvalue * i.intvalue; return Integer.toString(x); }
public void mult(HugeInteger i) { Sign sin; if (this.sign == i.sign) { sin = Sign.POS; } else { sin = Sign.NEG; } HugeInteger result = new HugeInteger(this.brutalMultiply(i)); result.sign = sin; this.assign(result); } }
in this code,
a) Modify your HugeInteger class to implement the same interface as before, but use Javas ArrayList now so as to deal with HugeIntegers of any size. You should test your program exactly as before (and we will test it separately with really huge integers!).
b) Add one more method, public void multPerformance(hugeInter a, hugeInteger b), which, prints the product ab along with the time it took to compute it.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
