CSC 142 Non-linear Calculator Using Java code to implement approximations for si
ID: 672445 • Letter: C
Question
CSC 142
Non-linear Calculator
Using Java code to implement approximations for sin(x). Each of these functions can be approximated with Taylor Series, as follows:
Restrictions
This program cannot use the exp() or sin() method in the Math class, nor use the Math.E constant.
Do not use any if statements in your solution.
Calculation Specification:
To calculate sin(x), you must use a loop to calculate the sum above for the first 30 terms. Again, progressively compute each term.
Your design must use procedural decomposition.
Use class constants where appropriate.
The program must start with main().
Your file must have a program comment at the top. This description, at a minimum, is one to two lines explaining what the program does.
Include your name and date in the file comments.
Each method must include a block comment that describes
what the method does
a description of parameters (if there are any)
what is returned (if anything is returned)
Display
Explanation / Answer
// Import util class
import java.util.Scanner;
//Mian function
public class Sin {
public static final double PI = 3.14159265358979323846;
public static void main(String[] args) {
Scanner UserInput = new Scanner(System.in);
System.out.println("Please enter a number");
double x = UserInput.nextDouble();
System.out.println("Number is" + x + ".");
System.out.println(Numerator() / Factorial() );
}
}
// Create function for Numerator
public static double Numerator(double x)
{
double term = 1.0;
double sum = 0.0;
double Power = (x^term);
do{
sum = sum + Power;
}
while (term <= 21);
term = term + 2;
return sum;
}
//Create function for Denominator
public static double Denominator(){
double result = 1;
for(int i = 1; i <= x; ++2)
{
result *= i;
}
return result;
}
}