Question: In Java - Please complete TO DO section public class JavaBigInt { private List value; private boolean isNegative; // default constructor public JavaBigInt() { value

In Java - Please complete TO DO section

public class JavaBigInt {

private List value; private boolean isNegative;

// default constructor public JavaBigInt() { value = new ArrayList<>(); value.add((byte) 0); isNegative = false; }

// construct a big integer from a String of value public JavaBigInt(String strValue) { if (strValue == null || strValue.length() == 0) { throw new IllegalArgumentException(); }

int valueFirstIndex = 0; if (strValue.charAt(valueFirstIndex) == '-') { valueFirstIndex++; isNegative = true; } else { isNegative = false; }

value = new ArrayList<>();

for (int i = valueFirstIndex; i < strValue.length(); i++) { byte val = (byte) ((int) strValue.charAt(i) - '0');

if (val < 0 || val > 9) { throw new IllegalArgumentException(); } value.add(0, val); } }

// construct a big integer from an integer public JavaBigInt(int nValue) { if (nValue == 0) { value = new ArrayList<>(); value.add((byte) 0); return; }

value = new ArrayList<>();

while (nValue > 0) { byte d = (byte) (nValue % 10);

if (d < 0 || d > 9) { throw new IllegalArgumentException(); }

value.add(d); nValue /= 10; } }

// construct a big integer from a big integer public JavaBigInt(JavaBigInt srcBi) { value = new ArrayList<>();

for (Byte b : srcBi.value) { value.add(b); } }

// construct a big integer from a list of value // with no error checking private JavaBigInt(List srcValue) { if (srcValue == null) { throw new IllegalArgumentException(); } this.value = srcValue; }

// add two big integers and return a reference to the big integer sum public JavaBigInt add(JavaBigInt bigVal) { Iterator smallVal, largeVal; if (bigVal.value.size() > this.value.size()) { largeVal = bigVal.value.iterator(); smallVal = this.value.iterator(); } else { largeVal = this.value.iterator(); smallVal = bigVal.value.iterator(); } byte carry = 0, rValue; List result = new ArrayList<>(); while (smallVal.hasNext()) { rValue = (byte) (smallVal.next() + largeVal.next() + carry); if (rValue > 9) { rValue -= 10; carry = 1; } else { carry = 0; } result.add(rValue); } while (largeVal.hasNext()) { rValue = (byte) (largeVal.next() + carry);

if (rValue > 9) { rValue -= 10; carry = 1; } else { carry = 0; } result.add(rValue); } if (carry == 1) { result.add(carry); } return new JavaBigInt(result); }

///////////////////////////////////////////////////////////////////////// // public JavaBigInt multiply(JavaBigInt bigVal) { // TO DO: Multiply method // ///////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////// // public JavaBigInt subtract(JavaBigInt bigVal) { /// TO DO: Subtract method //////////////////////////////////////////////////////////////////////// // output a string representation of the big integer public String toString() { StringBuilder sb = new StringBuilder(); for (byte d : value) { sb.append(d); } return sb.reverse().toString(); }

public static void main(String[] args) { JavaBigInt a = new JavaBigInt("100"); System.out.println("a: " + a); JavaBigInt b = new JavaBigInt(10); System.out.println("b: " + b); JavaBigInt c; c = a.add(b); System.out.println("c: =" + c); JavaBigInt d = new JavaBigInt(c); System.out.println("d: =" + d); //To Do //Multiply & Subtraction } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!