CS 1063  Introduction to Computer Programming  Project 3

The Series Class

 

Objectives

Hand-in Requirements

The project will be submitted electronically through WebCTPlace your Word document in your project directory.  Zip up your entire project directory to submit as the source. (Right click on the project folder, follow the Send To link, and select “Compressed (zipped) Folder.) Once the compression process is done, you will have a zipped file with the same name as your project folder and an extension of zip. This zipped file will be uploaded to the WebCT system during project submission. The project directory should include the following files:

Details

Calculate pi and ex using a series. 

    The series for   pi/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - ... and so on.  Output the estimate of pi after computing 100, 1000, 10000, 100000 terms of the series.  Let the user input the number of terms to be added and compare your answer with the Java constant Math.PI.

     Compute ex where   ex  = 1 + x1/1! + x2/2! + x3/3! + .....  ... + xn/n! + ....  Let the user input the value of x, of type double, to use.  Continue adding terms until the difference of successive terms is less than a constant
     double EPSILON = .000001   Compare your answer with the Java method Math.exp(x)


Constant

                      static double EPSILON = 0.000001;

 Instance Field

    none

Public Interface

/*  Constructor  */

  public Series()

 


/*  pi/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - ...
    Use a for loop to compute the sum for numTerms.  The method returns sum * 4
      Hint:  Generate the + - sign change by using the following
                      initialize       int sign = -1;  (starting with the second term)
                      in loop
                                          sum = sum + (your fraction) * sign; 
                sign = sign * -1;  
(reverses the sign for next multiplication )
*/

  public static double pi(int numTerms){

 


/*   ex  = 1 + x1/1! + x2/2! + x3/3! + .....  ... + xn/n! + ....
      Continue adding terms using a while loop until the difference of successive terms is less than a constant   
            
while ( sum - previous > EPSILON)

      This method must call fact(double x) below to calculate the denominator of the fraction.
       Remember that the
Math class has a pow function.    Example:  Math.pow(2,3)   2*2*2 = 8    See lecture week 8.

*/

  public static double e(double x){

 


/* calculates and returns the factorial of x
    Example  3!  = 3 * 2 * 1 = 6
   Use the method developed in class

 */

  public static double fact(double x){


                    2.  Testing your Series class

                    Assume the following:

 import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.io.IOException;


throws IOException added to the main heading.
BufferedReader console = new BufferedReader( new InputStreamReader(System.in));

OR
import java.util.Scanner;
Scanner in = new Scanner( System.in);

Use a do-while loop to ask the user if they would like to continue entering numbers.  In this loop test your method using 100, 1000, 10000, 100000

1.  Ask the user to enter the number of terms.  
2.  Print the value for pi using Series.pi(numTerms)
3.  Print the value for pi using Math.PI

Use a do-while loop to ask the user if they would like to continue entering numbers.  In this loop test your method using 4,5,6 and 7

1.  Ask the user to enter the value for x.
2.  Print the value for ex using Series.e(x)
3.  Print the value for ex using Math.exp(x) method