Java class name PiEstimatorXXXXX where XXXXX is your student ID. In the 14th cen
ID: 3567706 • Letter: J
Question
Java class name PiEstimatorXXXXX where XXXXX is your student ID.
In the 14th century, the value of PI was approximated by calculating the series
See http://en.wikipedia.org/wiki/Approximations_of_%CF%80
Using N terms in this series to estimate the PI value and display it. The higher the N
value is, the more accurate the estimate will be.
Define this method:
static ArrayList<Double> estimatePI (int count);
The loop must be inside this function. The loop must run exactly count time to
estimate the PI values with count terms of the series. Each iteration of the loop
generates a new estimate. Each estimate of the series is stored in an array list. Then
the function returns the array list to main(). Do not print any thing in the function !
In main() method, ask user to enter an input value of N. Then main() will call the
method estimatePI() with the argument N and get back an array list of N estimates.
Then main() will print the list of estimated PI values with 14 decimal digits in the
fractions.
In main(), you should also display the value of the PI constant in the Java Math
library with 14 decimal fraction digits.
Sample program run
Enter a count N to estimate the value of PI by looping for a series calculation:
25
Estimate PI values are:
3.46410161513775
3.07920143567800
3.15618147156995
3.13785289159568
3.14260474566308
3.14130878546288
3.14167431269884
3.14156871594178
3.14159977381151
3.14159051093808
3.14159330450308
3.14159245428765
3.14159271502038
3.14159263454731
3.14159265952171
3.14159265173400
3.14159265417257
3.14159265340616
3.14159265364783
3.14159265357140
3.14159265359563
3.14159265358793
3.14159265359039
3.14159265358960
3.14159265358985
PI in the Math library is 3.14159265358979
Explanation / Answer
This ones is fully correct.....I realy need to read the question clearly from next time......
I'm really really sorry for this but please ignore all the above solutions....
Final solution
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;
public class PiEstimator
{
//to round to 14 decimals
static DecimalFormat round = new DecimalFormat("#.00000000000000");
static ArrayList<Double> estimatePI (int count)
{
//initializing an ArrayList variable
ArrayList<Double> list=new ArrayList<Double>();
Double sum=0.0;
for(int i=0;i<count;i++)
{
//using the formula given to calculate the value of PI
sum+=Math.sqrt(12)*Math.pow(-1, i)*((double)1/(((2*i+1))*Math.pow(3, i)));
//adding it to the list after rounding it to 14 decimals
list.add(Double.parseDouble(round.format(sum)));
}
//returning the list
return list;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a count N to estimate the value of PI by looping for a series calculation: ");
int n=sc.nextInt();
ArrayList<Double> list=new ArrayList<Double>();
list=estimatePI(n);
for(int i=0;i<list.size();i++)
System.out.println(list.get(i));
System.out.println("PI in the Math library is: "+round.format(Math.PI));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.