**Unformatted text preview:**

NaturalNumberRoot.java Friday, March 4, 2022, 4:28 PM1 import components.naturalnumber.NaturalNumber;56 /**7 * Program with implementation of {@code NaturalNumber} secondary operation8 * {@code root} implemented as static method.9 *10 * @author Chime Nwaru11 *12 */13 public final class NaturalNumberRoot {1415 /**16 * Private constructor so this utility class cannot be instantiated.17 */18 private NaturalNumberRoot() {19 }2021 /**22 * Updates {@code n} to the {@code r}-th root of its incoming value.23 *24 * @param n25 * the number whose root to compute26 * @param r27 * root28 * @updates n29 * @requires r >= 230 * @ensures n ^ (r) <= #n < (n + 1) ^ (r)31 */32 public static void root(NaturalNumber n, int r) {33 assert n != null : "Violation of: n is not null";34 assert r >= 2 : "Violation of: r >= 2";3536 //Creates natural numbers for 0, 1, and 237 NaturalNumber two = new NaturalNumber2("2");38 NaturalNumber one = new NaturalNumber2("1");39 NaturalNumber zero = new NaturalNumber2("0");4041 //Creates a stable variable for the guessed number to refer back to42 NaturalNumber orig = new NaturalNumber2("1");43 orig.copyFrom(n);4445 //Creates a variable that'll be used for the guesses46 NaturalNumber roots = new NaturalNumber2("2");47 roots.copyFrom(n);48 roots.divide(two);4950 //Creates lower and upper bound for the guesses51 NaturalNumber low = new NaturalNumber2("0");5253 NaturalNumber high = new NaturalNumber2("2");54 high.copyFrom(n);5556 boolean check = false;5758 NaturalNumber exp = new NaturalNumber2(r);5960 //If the number is ever lower than the exponent of the root the answerPage 1NaturalNumberRoot.java Friday, March 4, 2022, 4:28 PM61 //will always be 162 if (n.compareTo(exp) <= 0) {63 roots.copyFrom(one);64 check = true;65 }66 //If the number is ever 0 the answer will always be 06768 if (n.compareTo(zero) == 0) {69 roots.copyFrom(zero);70 check = true;71 }7273 while (!check) {7475 orig.copyFrom(roots);7677 //Creates the variable for the power and the power of the78 //number+1 to check79 roots.add(one);80 roots.power(r);81 NaturalNumber npower = new NaturalNumber2("2");82 npower.copyFrom(roots);8384 roots.copyFrom(orig);8586 roots.power(r);87 NaturalNumber power = new NaturalNumber2("2");88 power.copyFrom(roots);89 roots.copyFrom(orig);9091 //Checks if the condition is met of the power of the guess is less92 //than or equal to the number while the power of the guess +193 //is greater than n 94 if (power.compareTo(n) <= 0 && npower.compareTo(n) > 0) {95 check = true;9697 } else {9899 roots.copyFrom(orig);100101 //if the power was more than the number it'll change the upper102 //bound and change the new guessing number103 if (power.compareTo(n) > 0) {104105 high.copyFrom(orig);106 roots.add(low);107 roots.divide(two);108109 } else {110 //if the power was less than the number it'll change the111 //lower bound and change the new guessing number112113 low.copyFrom(orig);114 roots.add(high);115 roots.divide(two);116 }117 }Page 2NaturalNumberRoot.java Friday, March 4, 2022, 4:28 PM118 }119 n.copyFrom(roots);120 }121122 /**123 * Main method.124 *125 * @param args126 * the command line arguments127 */128 public static void main(String[] args) {129 SimpleWriter out = new SimpleWriter1L();130131 final String[] numbers = { "0", "1", "13", "1024", "189943527", "0",132 "1", "13", "4096", "189943527", "0", "1", "13", "1024",133 "189943527", "82", "82", "82", "82", "82", "9", "27", "81",134 "243", "143489073", "2147483647", "2147483648",135 "9223372036854775807", "9223372036854775808",136 "618970019642690137449562111",137 "162259276829213363391578010288127",138 "170141183460469231731687303715884105727" };139 final int[] roots = { 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 15, 15, 15, 15, 15,140 2, 3, 4, 5, 15, 2, 3, 4, 5, 15, 2, 2, 3, 3, 4, 5, 6 };141 final String[] results = { "0", "1", "3", "32", "13782", "0", "1", "2",142 "16", "574", "0", "1", "1", "1", "3", "9", "4", "3", "2", "1",143 "3", "3", "3", "3", "3", "46340", "46340", "2097151", "2097152",144 "4987896", "2767208", "2353973" };145146 for (int i = 0; i < numbers.length; i++) {147 NaturalNumber n = new NaturalNumber2(numbers[i]);148 NaturalNumber r = new NaturalNumber2(results[i]);149 root(n, roots[i]);150 if (n.equals(r)) {151 out.println("Test " + (i + 1) + " passed: root(" + numbers[i]152 + ", " + roots[i] + ") = " + results[i]);153 } else {154 out.println("*** Test " + (i + 1) + " failed: root("155 + numbers[i] + ", " + roots[i] + ") expected <"156 + results[i] + "> but was <" + n + ">");157 }158 }159160 out.close();161 }162 }163Page

View Full Document