Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help filling in the body of the static method naturalNumberRoot so that it

ID: 3925084 • Letter: N

Question

Need help filling in the body of the static method naturalNumberRoot so that it computes the root of a natural number

import components.naturalnumber.NaturalNumber;
import components.naturalnumber.NaturalNumber2;
import components.simplewriter.SimpleWriter;
import components.simplewriter.SimpleWriter1L;

/**
* Program with implementation of {@code NaturalNumber} secondary operation
* {@code root} implemented as static method.
*
* @author Put your name here
*
*/
public final class NaturalNumberRoot {

/**
* Private constructor so this utility class cannot be instantiated.
*/
private NaturalNumberRoot() {
}

/**

   * Updates {@code n} to the {@code r}-th root of its incoming value.

   *

   * @param n

   * the number whose root to compute

   * @param r

   * root

   * @updates n

   * @requires r >= 2

   * @ensures n ^ (r) <= #n < (n + 1) ^ (r)

   */

public static void root(NaturalNumber n, int r) {

assert n != null : "Violation of: n is not null";

assert r >= 2 : "Violation of: r >= 2";

  

  

}

/**

   * Main method.

   *

   * @param args

   * the command line arguments

   */

public static void main(String[] args) {

SimpleWriter out = new SimpleWriter1L();

final String[] numbers = { "0", "1", "13", "1024", "189943527", "0",

"1", "13", "4096", "189943527", "0", "1", "13", "1024",

"189943527", "82", "82", "82", "82", "82", "9", "27", "81",

"243", "143489073", "2147483647", "2147483648",

"9223372036854775807", "9223372036854775808",

"618970019642690137449562111",

"162259276829213363391578010288127",

"170141183460469231731687303715884105727" };

final int[] roots = { 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 15, 15, 15, 15, 15,

2, 3, 4, 5, 15, 2, 3, 4, 5, 15, 2, 2, 3, 3, 4, 5, 6 };

final String[] results = { "0", "1", "3", "32", "13782", "0", "1", "2",

"16", "574", "0", "1", "1", "1", "3", "9", "4", "3", "2", "1",

"3", "3", "3", "3", "3", "46340", "46340", "2097151", "2097152",

"4987896", "2767208", "2353973" };

for (int i = 0; i < numbers.length; i++) {

NaturalNumber n = new NaturalNumber2(numbers[i]);

NaturalNumber r = new NaturalNumber2(results[i]);

root(n, roots[i]);

if (n.equals(r)) {

out.println("Test " + (i + 1) + " passed: root(" + numbers[i]

+ ", " + roots[i] + ") = " + results[i]);

} else {

out.println("*** Test " + (i + 1) + " failed: root("

+ numbers[i] + ", " + roots[i] + ") expected <"

+ results[i] + "> but was <" + n + ">");

}

}

out.close();

}

}

Explanation / Answer

import components.naturalnumber.NaturalNumber;
import components.naturalnumber.NaturalNumber2;
import components.simplewriter.SimpleWriter;
import components.simplewriter.SimpleWriter1L;

public final class NaturalNumberRoot {

    /**
     * Private constructor so this utility class cannot be instantiated.
     */
    private NaturalNumberRoot() {
    }

    private static NaturalNumber NaturalNumber2(1);
    private static NaturalNumber TWO = new NaturalNumber2(2);

    /**
     * Updates {@code n} to the {@code r}-th root of its incoming value.
     *
     */
    public static void root(NaturalNumber n, int r) {
        assert n != null : "Violation of: n is not null";
        assert r >= 2 : "Violation of: r >= 2";

        NaturalNumber lowEnough = n.newInstance();

        NaturalNumber tooHigh = n.newInstance();
        tooHigh.copyFrom(n);
        tooHigh.increment();

        NaturalNumber guess = n.newInstance();
        guess.copyFrom(n);
        guess.divide(TWO);

        NaturalNumber diff = n.newInstance();
        diff.copyFrom(tooHigh);
        diff.subtract(lowEnough);

        while (diff.compareTo(ONE) > 0) {
            NaturalNumber guessPower = n.newInstance();
            guessPower.copyFrom(guess);
            guessPower.power(r);

            if (n.compareTo(guessPower) < 0) {
                tooHigh.transferFrom(guess);
            } else {
                lowEnough.transferFrom(guess);
            }
            //the next four lines diff is just acting as a temp variable
            diff.copyFrom(tooHigh);
            diff.subtract(lowEnough);
            diff.divide(TWO);
            diff.add(lowEnough);
            guess.transferFrom(diff);

            diff.copyFrom(tooHigh);
            diff.subtract(lowEnough);
        }

        n.transferFrom(lowEnough);
    }

    /**
     * Main method.
     */
    public static void main(String[] args) {
        SimpleWriter out = new SimpleWriter1L();

        final int[] numbers = { 0, 1, 13, 1024, 189943527, 0, 1, 13, 4096,
                189943527, 0, 1, 13, 1024, 189943527, 82, 82, 82, 82, 82, 9,
                27, 81, 243, 143489073 };
        final int[] roots = { 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 15, 15, 15, 15, 15,
                2, 3, 4, 5, 15, 2, 3, 4, 5, 15 };
        final int[] results = { 0, 1, 3, 32, 13782, 0, 1, 2, 16, 574, 0, 1, 1,
                1, 3, 9, 4, 3, 2, 1, 3, 3, 3, 3, 3 };

        for (int i = 0; i < numbers.length; i++) {
            NaturalNumber n = new NaturalNumber2(numbers[i]);
            NaturalNumber r = new NaturalNumber2(results[i]);
            root(n, roots[i]);
            if (n.equals(r)) {
                out.println("Test " + (i + 1) + " passed: root(" + numbers[i]
                        + ", " + roots[i] + ") = " + results[i]);
            } else {
                out.println("*** Test " + (i + 1) + " failed: root("
                        + numbers[i] + ", " + roots[i] + ") expected <"
                        + results[i] + "> but was <" + n + ">");
            }
        }

        out.close();
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote