Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA: Need help with coding this assignment. IMPORTANT -- Please ensure that BOTH loops are in the same program, one FOR LOOP and one WHILE

JAVA: Need help with coding this assignment. IMPORTANT -- Please ensure that BOTH loops are in the same program, one FOR LOOP and one WHILE LOOP

Follow the directions below to submit Assignment 4:

Create a Java program.

The class name for the program should be 'CalculatePI'.

In the main method you should perform the following:

Create a for loop that will loop for 10,000,000 iterations that will be used to calculate the Mathematical value of PI.

You will need a variable to count the iterations.

You will need a variable that will be used in the mathematical calculation inside the loop.

The formula for PI is: PI = 4 * (1 - 1/(2*i-1) + 1/(2*i+1) . . .

Each 1000th iteration of the loop should print the iteration number and the currently calculation for PI.

At the end of the loop the program should display the value of PI as determined by the variable Math.PI and the difference between your calculated value and the value contained in Math.PI

You should then repeat the instructions above using a while( ) loop rather than a for( ) loop. **SAME JAVA PROGRAM** CALLING METHODS.

Pseudo flowchart for the assignment program:

Create a variable to contain the PI calculation. The initial value of this variable should be 1.

Create a for( ) loop that will loop for 10,000,000 iterations. You will need a loop variable to count the number iterations.

Create a second loop variable that will be used for i in the calculation. The initial value of this varaible should be 2.

For each iteration of the loop you will need to subtract the value of 1.0/(2*i-1) from the calculation and add the value of 1.0/(2*i+1).

At the end of the loop you will increment the iteration loop variable by 1 and increment the value of i used in the calculation by 2.

Every 1000th iteration of the loop you should display the iteration number and the calculated value for PI.

When you have completed the loop you should print the value of Math.PI and the difference between Math.PI and your calculated value of PI.

You will then repeat this process using a while( ) loop...WITHIN THE SAME PROGRAM USING THE SAME CLASS..

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions