Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have posted this question before but now I have a different problem so I thoug

ID: 3930691 • Letter: I

Question

I have posted this question before but now I have a different problem so I thought it was best to ask a new question. Here is my original question(https://www.chegg.com/homework-help/questions-and-answers/tried-question-issues-source-code-errors-compiling-source-code-set-newestimate-completely--q15801445). My issue now is that my code is a never-ending loop and I am getting no output. Here is my new code:

import java.lang.Math;
public class GeneratingPi {
   public static void main(String[] args) {
       double estimate=3.14;
       double oldEstimate=0;
      
       double difference = estimate-oldEstimate;
       double counter = 0;
              
       while (difference > ((Math.E)-8)) {
           estimate = 4*(Math.pow(-1, counter))/(2*counter + 1);
           estimate += oldEstimate;//but if i do this before the difference, difference will equal 0.
           difference = Math.abs(oldEstimate-estimate);
           counter++;
       }//loop isn't ending.
      
       System.out.println("It took " + counter + " iterations for the difference to be less than 1E-8");
       System.out.println("The value of the estimate is " + oldEstimate + " The value of Pi is " + Math.PI);
      
   }
}  

Originally I had the estimate+=oldEstimate before the difference calculation but my professor told me I needed to do it before. I just don't know how else to manipulate it. Please help

Explanation / Answer

Program:

import java.lang.Math;
public class GeneratingPi {
public static void main(String[] args) {
double estimate=3.14;
double oldEstimate=0;
  
double difference = (double)(estimate-oldEstimate);
double counter = 0;

while (difference < ((Math.E)-8)) {

// If we use > value then it leads to infinite loop and loop doesnot end

estimate = 4*((Math.pow(-1, counter))/(2*counter + 1));
estimate += oldEstimate;//but if i do this before the difference, difference will equal 0.
difference = Math.abs(oldEstimate-estimate);
counter++;

}//loop isn't ending.
System.out.println("It took " + counter + " iterations for the difference to be less than 1E-8");
System.out.println("The value of the estimate is " + oldEstimate + " The value of Pi is " + Math.PI);
  
}
}

Output:

It took 0.0 iterations for the difference to be less than 1E-8

The value of the estimate is 0.0

The value of Pi is 3.141592653589793