Question: Objective Write a method that returns an estimate of pi Background reading ZyBooks Chapters 4 and 6, Loops and Methods Assignment The mathematical value of

Objective

Write a method that returns an estimate of pi

Background reading

ZyBooks Chapters 4 and 6, Loops and Methods

Assignment

The mathematical value of pi can be estimated by the following summation:

m(i) = 4( 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + + (Math.pow(-1,i+1)) / (2i - 1) )

Write a method estimatePi() that returns m(i) for a given i, and write a program that displays the following table:

i m(i) ----------------- 1 4.0000 101 3.1515 201 3.1466 301 3.1449 401 3.1441 501 3.1436 601 3.1433 701 3.1430 801 3.1428 901 3.1427 

Plan the approach

Our main method should:

Get a value for i which is the number of terms to sum in the series.

Repeatedly call estimatePi(), first with value 1 and up to the number of terms needed. For example, if we want to find m(401), we would print the result of estimatePi(1), then print the result of estimatePi(101), and so on until estimatePi(401). We will not concern ourselves with printing increments other than 100.

Our estimatePi() method should:

accept one parameter int value n

get a variable to store the series sum

Calculate the series sum by looping from i=1 to n,

each loop calculates a quotient term in the series by calculating (Math.pow(-1,i+1)) / (2i - 1).

add that value to the previous series sum

return 4 multiplied by the series sum

Optional, but helpful methods:

printHeader() method should print the table header and underscore.

printTableEntry() method should print a row in the table given parameters i and m(i)

/* To format the table, use printf. Something along the lines of: * System.out.printf("%3d %6.4f ", i, mi); */

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!