// Subtract 'num' from me in my base, returning the new value. // Precondition: my number >= 'num'. // ------------------------------------------------------------------ // STUDENTS WILL WRITE THIS METHOD. private NIB subtract (NIB num) { // Start the result as the number being subtracted from. NIB res = new NIB(toString(), getBase()); // The index of the current characters being subtracted. int i = 0; // While there are digits to subtract... while (i < res.getNumDigits()) { // If a(i) < b(i), we need to perform a carry. if (res.digits[i] < num.digits[i]) { res.digits[i] += res.getBase(); res.digits[i+1] --; } res.digits[i] -= num.digits[i]; i++; } // Deal with any leading 0's and update the size of the number. while (i > -1 && res.digits[i] == 0) { i--; } res.setNumDigits(i+1); return res; } // Return a new NumberInBase in base 'b' whose value is equivalent to // mine. // ------------------------------------------------------------------ // STUDENTS WILL WRITE THIS METHOD. public NIB convertTo (int b) { // For the result. NIB numberInNewBase = new NIB("",b); NIB workspace = new NIB(toString(),getBase()); NIB divisor = new NIB(Integer.toString(b,getBase()),getBase()); int count = 0; while (Integer.parseInt(workspace.toString(), workspace.getBase()) != 0) { int rem = workspace.remainder(divisor); numberInNewBase.putDigit(rem,count); workspace = workspace.divide(divisor); count ++; } return numberInNewBase; }