Create a new project Lab06a, and write a program that computes an approximation
ID: 3634112 • Letter: C
Question
Create a new project Lab06a, and write a program that computes an approximation of a series. You program must take a threshold value and compute the approximation as long as the value of the series element is greater than the threshold. Error is computed as absolute difference of approximation and actual value. Your program should include two static methods. First one should take threshold value as a parameter and compute and return series approximation. Second one should take series approximated value as a parameter; compute and display error accordingly.
1/1 + 1/4 + 1/9 + 1/16 + 1/25 +...=*/6
Below are example outputs from the program:
Enter the threshold value: 0,1
Result : 1.3611111111111112
PI^2/6 : 1.6449340668482264
Error : 0.28382295573711525
Enter the threshold value: 0,01
Result : 1.5497677311665408
PI^2/6 : 1.6449340668482264
Error : 0.09516633568168564
Explanation / Answer
please rate - thanks
import java.util.*;
public class main
{public static void main(String [] args)
{Scanner in=new Scanner(System.in);
double eps,result,error;
System.out.print("Enter the threshold value: ");
eps=in.nextDouble();
result=series(eps);
System.out.println("Result : "+result);
displayError(result);
}
public static void displayError(double result)
{double act=(Math.PI*Math.PI)/6.;
double error=Math.abs(act-result);
System.out.println("PI^2/6 : "+act);
System.out.println("Error : "+error);
}
public static double series(double eps)
{double val=0, sum=0;
int den=1;
do
{val=1./(den*den);
sum+=val;
den++;
}while(val>eps);
return sum;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.