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){
import
java.io.InputStreamReader;
import
java.io.IOException;
throws
IOException added to the main heading.
BufferedReader
console = new BufferedReader( new
InputStreamReader(System.in));
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
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