here is the problem:
-user enters "degree" and a "threshold" value
-degree is converted to radians(with a static method)
-computing the approximation as long as the absolute value of the series element is greater than the threshold
-Error is computed as absolute difference of approximation and the value of cos(x)
-//Write a static method that takes threshold value as a parameter and compute and return series approximation. Write another static method that take series approximated value as a parameter; compute and display error accordingly.

An example:
Enter 'x' in degrees: 60
Enter the threshold value: 0,1
A(x) : 0.45168864438392464
cos(x) : 0.5000000000000001
Error : 0.04831135561607547

I cannot get the A(x) value from that method and it returns 0.0 on the print screen

import java.util.Scanner;

public class Trial03 
	static double sum = 0;
	static double radian;
	//converting to radians method
	public static double toRadian(int degree)
		return (degree*Math.PI)/180;
	//factorial method
	public static int fact(int k)
		int fct = 1;
		for ( int i = 1; i<=k;i++)
			fct = fct * i;
		return fct;
	//threshold calculator method
	public static double thrCalc(double threshold,double x)
		double r1 = 1;
		double result = 0;
		for (int i = 0; r1<threshold; i++)
			//double result = 0;
			result = Math.pow(-1,i) * ((Math.pow(x,i*2))/fact(i*2));
			sum = sum + result;
			sum = r1;
		return sum;
	//main method
    public static void main( String[] args) 
    	Scanner scan = new Scanner(;

		int degree;
		double treshold;
		double error;
		System.out.print( "Enter 'x' in degrees: ");
		degree = scan.nextInt();
		System.out.print( "Enter the threshold value: ");
		treshold = scan.nextDouble();

		double sonuc = thrCalc(treshold);
		System.out.println("A(x)\t: " + sonuc);
		System.out.println("cos(x)\t: " + Math.cos(toRadian(degree)));
		error = sonuc - Math.cos(toRadian(degree));
		System.out.println("Error\t: " + Math.abs(error));

Recommended Answers

All 2 Replies

The first problem is this code does not compile.

The first problem is this code does not compile.

thanks i solved it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.