Question: Part A: Estimating Pi There are many formulas that can be used to approximate pi. The following formula was proposed by Leibniz: =4(1131+5171+) Write a





Part A: Estimating Pi There are many formulas that can be used to approximate pi. The following formula was proposed by Leibniz: =4(1131+5171+) Write a single Java class called EstimatingPi that estimates pi using that formula. You may place all of your code in the main method of this class. Here are some hints: - The calculation involves a series of terms": 4/1,4/3,4/5,4/7,4/9, and so forth. - These terms involve fractions, so use variables of type double for your calculations. This means the terms are really 4.0/1.0,4.0/3.0,4.0/5.0, and so forth. - We don't want your program to keep going until infinity, so use a while loop that continues as long as the term is larger than 0.00000001 - Notice that the denominators in the terms are 1.0,3.0, 5.0, 7.0, and so forth. You can accomplish this by having a variable (of type double) for your denominator. Initialize this value to 1.0. Increase it by 2.0 each time through your loop. - That takes care of calculating the denominator values, so calculating the term each time through the loop is simply 4.0 divided by the denominator. - You need another variable to store your estimate of pi. This value will change each time through the loop. The change is either to add or subtract the term. As the formula above shows, the first term is added, the second term is subtracted. and so forth, alternating each time. - There are several good ways to keep track of whether to add or subtract the term. For instance, you could use a boolean variable that you alternate between true and false each time through the loop. Or you could use a double variable that alternates between 1.0 and 1.0 each time through the loop - the advantage of this one is you can multiply by that variable instead of using an if statement to decide between adding and subtracting. Or (since you have to count the number of loop iterations - see below) you can determine whether your counter is odd or even using an if statement and the \% operator. Count how many loop iterations it takes you to calculate a final estimate of pi. IMPORTANT: I want you to use this as an opportunity to practice incremental programming. This approach increases how quickly and easily programming tasks can be completed. The following pages provide some tips for doing this in an effective way with this assignment. Let's start with where you want to end up. Here is sample output from my version of this program. Your program should produce the same type of output: $ java EstimatingPi The number of iterations wast 200000001 The estimated pi 1s: 3.1415926585894076 Math.PI: 3.141592653589793 The 20 lines of output are included to help you understand how the calculation progresses each time through your while loop. You will need an if statement inside your loop so you only print for the first 20 iterations, not all 200 million! (1 Notice that once your calculation of pi is complete, you are to print the following three values (as I have done above): 1. The number of iterations it took to complete your calculation. (Are you surprised how many iterations were required? And did you notice if your program took a second or so to complete its execution?) 2. Your final estimate of pi 3. The value Math.PI Okay, so let's talk about the incremental steps you should use as you develop this program. Start by writing a main method with a while loop that prints the numbers from 1.0 to 9.0, counting by 2.0. That's it. In other words, write a while loop that produces the following output, nothing more: 1.03.05.07.09.0 What have you just accomplished? You've figured out how to write a while loop that produces the required series of denominator values (well, the first few of them anyway). ONLY AFTER you have that compiled and working, update the program so it now prints two values per line - the same output as above, as well as 4.0 divided by each of those numbers. So you will produce output similar to this: 1.03.05.07.09.04.01.3333333333333330.80.5714285714285710.444444444444444 The second column is the required series of term values. What should the next incremental step be? Maybe add a counter and the alternating adding \& subtracting to start calculating your pi value. (At this point you're still only working with denominator values from 1.0 to 9.0, but that's okay - we'll extend the number of loop iterations in a subsequent version of the program.) The final step might be to change your while loop condition. Don't stop at a denominator of 9.0 - instead, keep looping while your term is larger than 0.00000001. Be sure to add an if statement inside the loop so you only print the first 20 values (see the code below). Each of those steps is complete only once you've compiled, tested, and the partial program works as expected. I promise this approach will help you develop more effective programming habits. You will complete your programming tasks more quickly. with fewer debugging problems, and with a better understanding of how the code works which will translate into better marks on your final exam. Your final version of the program should produce output similar to the sample output shown on the previous page. To align the numbers as I have done, use a printf statement similar to this at the appropriate place inside your while loop: if (n
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
