Unformatted text preview:

NaturalNumberRoot java Friday March 4 2022 4 28 PM 1 import components naturalnumber NaturalNumber 5 6 7 Program with implementation of code NaturalNumber secondary operation 8 code root implemented as static method 9 10 author Chime Nwaru 11 12 13 public final class NaturalNumberRoot 14 15 16 Private constructor so this utility class cannot be instantiated 17 18 private NaturalNumberRoot 19 20 21 22 Updates code n to the code r th root of its incoming value 23 24 param n 25 the number whose root to compute 26 param r 27 root 28 updates n 29 requires r 2 30 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 35 36 Creates natural numbers for 0 1 and 2 37 NaturalNumber two new NaturalNumber2 2 38 NaturalNumber one new NaturalNumber2 1 39 NaturalNumber zero new NaturalNumber2 0 40 41 Creates a stable variable for the guessed number to refer back to 42 NaturalNumber orig new NaturalNumber2 1 43 orig copyFrom n 44 45 Creates a variable that ll be used for the guesses 46 NaturalNumber roots new NaturalNumber2 2 47 roots copyFrom n 48 roots divide two 49 50 Creates lower and upper bound for the guesses 51 NaturalNumber low new NaturalNumber2 0 52 53 NaturalNumber high new NaturalNumber2 2 54 high copyFrom n 55 56 boolean check false 57 58 NaturalNumber exp new NaturalNumber2 r 59 60 If the number is ever lower than the exponent of the root the answer Page 1 NaturalNumberRoot java Friday March 4 2022 4 28 PM 61 will always be 1 62 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 0 67 68 if n compareTo zero 0 69 roots copyFrom zero 70 check true 71 72 73 while check 74 75 orig copyFrom roots 76 77 Creates the variable for the power and the power of the 78 number 1 to check 79 roots add one 80 roots power r 81 NaturalNumber npower new NaturalNumber2 2 82 npower copyFrom roots 83 84 roots copyFrom orig 85 86 roots power r 87 NaturalNumber power new NaturalNumber2 2 88 power copyFrom roots 89 roots copyFrom orig 90 91 Checks if the condition is met of the power of the guess is less 92 than or equal to the number while the power of the guess 1 93 is greater than n 94 if power compareTo n 0 npower compareTo n 0 95 check true 96 97 else 98 99 roots copyFrom orig 100 101 if the power was more than the number it ll change the upper 102 bound and change the new guessing number 103 if power compareTo n 0 104 105 high copyFrom orig 106 roots add low 107 roots divide two 108 109 else 110 if the power was less than the number it ll change the 111 lower bound and change the new guessing number 112 113 low copyFrom orig 114 roots add high 115 roots divide two 116 117 Page 2 NaturalNumberRoot java Friday March 4 2022 4 28 PM 118 119 n copyFrom roots 120 121 122 123 Main method 124 125 param args 126 the command line arguments 127 128 public static void main String args 129 SimpleWriter out new SimpleWriter1L 130 131 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 145 146 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 159 160 out close 161 162 163 Page 3


View Full Document

UIUC SOC 100 - Natural Number Root

Download Natural Number Root
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Natural Number Root and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Natural Number Root and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?