Question
Your job is to implement the root static method for NaturalNumber using the interval halving root algorithm you developed in an earlier homework and lab
Your job is to implement the root static method for NaturalNumber using the interval halving root algorithm you developed in an earlier homework and lab for integer roots.
Setup
Create a new Eclipse project by copying ProjectTemplate or a previous project you have created, naming the new project NaturalNumberRoot.
Open the src folder of this project and then open (default package). As a starting point you can use any of the Java files. Rename it NaturalNumberRoot and delete the other files from the project.
Follow the link to NaturalNumberRoot.java, select all the code on that page and copy it to the clipboard; then open the NaturalNumberRoot.java file in Eclipse and paste the code to replace the file contents. Save the file.
Method
Edit NaturalNumberRoot.java to implement the root static method with the interval halving algorithm. Here is the contract:
/** * 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) {...} |
For this method you can use any NaturalNumber methods except for NaturalNumber's own instance method root. Run the NaturalNumberRoot program to test your implementation of root.
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";
// TODO - fill in body
}
/**
* 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();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started