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
// 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
// add two big integers and return a reference to the big integer sum public JavaBigInt add(JavaBigInt bigVal) { Iterator
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
Get step-by-step solutions from verified subject matter experts
