Project: NaturalNumber Roots
Objectives
- Familiarity with using
NaturalNumberobjects and methods. - Familiarity with using interval halving to invert a function.
The Problem
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
ProjectTemplateor a previous project you have created, naming the new projectNaturalNumberRoot. - Open the
srcfolder of this project and then open(default package). As a starting point you can use any of the Java files. Rename itNaturalNumberRootand 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 theNaturalNumberRoot.javafile in Eclipse and paste the code to replace the file contents. Save the file.
Method
-
Edit
NaturalNumberRoot.javato implement therootstatic 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
NaturalNumbermethods except forNaturalNumber's own instance methodroot. Run theNaturalNumberRootprogram to test your implementation ofroot. -
Select your Eclipse project
NaturalNumberRoot(not just some of the files, but the whole project), create a zip archive of it, and submit the zip archive to the Carmen dropbox for this project, as described in Submitting a Project.
Additional Activities
Here are some possible additional activities related to this project. Any extra work is strictly optional, for your own benefit, and will not directly affect your grade.
- In Eclipse, copy
NaturalNumberRoot.javatoNaturalNumberRoot2.java. Write another implementation of therootmethod using interval halving with a smaller starting interval. It turns out that you can start not with the interval [0, #n + 1) but with the (generally much smaller) interval [10k, 10k+1), where k is the greatest integer less than or equal to (|#n|-1)/r. Here, |#n| denotes the discrete log (base 10) of #n (the discrete log (base 10) of a positive natural number is equal to the number of digits in the base 10 representation of the number; the discrete log of 0 is 0). Compare performance of the two implementations.