Programming Assignment
2
Due February 8, before 9:30am
|
||
Program Description |
||
In this
assignment, you will use basic concepts of working with floating-point numbers
and flow-of-control. You will develop a method that will raise a number x to the power n, and another method that will find
the n-th root of a number y. |
||
What You Do
|
||
It is assumed that you are using NetBeans to develop
your code for this assignment. Create a new NetBeans project that is a "General"
"Java Application". Name your NetBeans project program2. Download the source for the Main.java and MyMath.java class, and store these in the program2/src/program2 directory. Using NetBeans, you are to implement two methods for the class MyMath:
Finding the n-th root via the bisection algorithmFor the problem of finding the n-th root of y, we want to solve for the value x such that f(x) = xn - y = 0. The following pseudo-code gives an outline of the basic algorithm for this case. Note that you will need to adapt this for Java and your own implementation of the method nRootBisection.given a value y, a positive integer n, and numerical tolerance set a = 0 and set b = y if y > 1 otherwise set b = 1 // an is less than y and bn > is greater than or equal to y while number of iterations is less than maximum allowable { set mid = the midpoint of the interval [a,b] if midn - y is greater than 0 set b = mid otherwise set a = mid if (b-a) is less than numerical tolerance return a } // Error: Maximum number of iterations exceeded handle the error Error CheckingYour implementation of the method power should detect the following error cases:
Example
provided: Newton-Raphson algorithm
Another numerical algorithm
for converging on a numerical solution of f(x) = xn - y = 0 is the Newton-Raphson method. An example
implementation is shown in the method nRootNewton. This algorithm
starts with an initial guess for x and
then at each iteration updates the estimated x using the equation: x = (x - f(x)) / f'(x), where f'(x) is the first derivative. This process
is repeated until the change in x is
within the numerical tolerance interval around zero. |
||
What You Submit
|
||
Submit
your file called MyMath.java in a
directory named 02 via
gsubmit
on csa2.bu.edu. Under no circumstances will late assignments be accepted. |
||
Grading Criteria
|
||
20%
Submitted code compiles without errors 20% power correctly computes x to the power n, even when n = 0 20% nRootBisection correctly finds roots for the cases given in Main.java 20% nRootBisection correctly finds roots for other cases given at testing time 20% Properly handle error cases as described above |
||