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:
  1. double power(double x, int n)  uses a for loop to compute x to the power n.  Be sure to properly handle the case when n=0.
  2. double nRootBisection(double y, int n) computes the n-th root of y via bisection. 
Bisection is a numerical algorithm for iteratively converging on a solution of a function f(x) known to lie inside some interval [a,b]. The algorithm evaluates the function f(x) at the midpoint of the interval [a,b],  x = (a+b)/2.  and then tests to see in which of the subintervals [a,(a+b)/2] or [(a+b)/2,b] the solution lies.   This procedure is then repeated with the new interval, as often as needed to locate the solution within the desired accuracy.  Usually, there is a maximum number of iterations enforced -- to prevent infinite loops.

Finding the n-th root via the bisection algorithm

For 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 Checking

Your implementation of the method power should detect the following error cases:
  • the value of n is negative
Your implementation of the method myRootBisection should detect the following error cases:
  • the value of y is negative
  • the value of n is not positive
  • the function failed to converge after maximum allowed iterations
In all error cases, your method should return the value -1.

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.

Debugging mode

The class MyMath includes a method debug(boolean debugState). By invoking the method with the value true, debugging messages may be printed.   Your implementation should only print out information if debugState is set to true.  For an example, see the method nRootNewton.

Testing

You are responsible for testing your methods.  To help you in testing, a few example tests are provided in the file Main.java.  Other tests will be conducted in grading -- so test your methods carefully before submitting your code.  Be sure to test that your methods work in all the error cases identified above.
     

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