Hi, Im having trouble with this java program. I have to compute cosine using the
ID: 3543478 • Letter: H
Question
Hi, Im having trouble with this java program.
I have to compute cosine using the infinite series expansion and not the math,cos function.
Here are the guidelines
1. Ask the user to inout a value for x.
2. Ask the user to input a value for the maximum exponet to compute up to 4 or 6, etc. The data type for this variable denoting the maximum expoent should be an even intereger. You will put in a do- while loop for data validation to ensure that it is a positive EVEN integer.
3. Compute and output the angle in both radiands and degrees using the formula below and not the pre- farbicrated Math library function. Also use a System.out.printf statement so that you will only display two decimal places for these values. Degrees = Radians * 180/Pi.
4. Then have your program display an approximate value for cos(x), using the cos (x) equation shown above. (Infinite series expansion).
Sample Run Testing with 60 Degrees
Please enter the input (9n radians) :
Please inout the exponet:
The angle is 1.05 radiands, or 60.00 degrees.
The approximated value for cosine is:
Thank You in advance.
Explanation / Answer
/*Updated code. I am printing all possible decimals in this version. You can simply limit the number of decimals you want by replacing %.2f by %.5f (in previos code) say if you wanted to print 5 decimals. Please let me know if you want anything else. Thanks!*/
import java.util.Scanner;
public class Cosine {
//Function to compute the factorial of a number.
//Used in series expansion of cosine
public static int fact(int n){
if(n<2) return 1;
else return n*fact(n-1);
}
public static void main(String[] args) {
//Create a scanner object to read from standard input
Scanner sc= new Scanner(System.in);
//Prompt for the value of x in radians
System.out.println("Please Enter the input(in radians) : ");
//Read the value of x
double x = sc.nextDouble();
//variable to hold the maximum exponent in series expansion
int exponent;
//Prompt for the value of a positive even exponent
System.out.println("Please Enter a positive even value for the maximum exponent to compute up to : ");
//Validation loop
while(true){
//Read the value of even exponent
exponent = sc.nextInt();
//If the exponent is negative or or odd, then show an error msg and prompt again for the exponent
if(exponent<=0 || exponent%2 == 1){
System.out.println("Sorry, you must enter an even postive integer. Please input the exponent: ");
}
else break;
}
//for loop variable
int n;
//variable to hold the approximated value of cos(x) using series expansion
double sum = 0;
//variable to hold the value of angle in degrees
double degrees = 0;
//Approximate the value of cos(x) using first "exponent" terms of series expansion of cos(x)
for(n=0;n<=exponent;n++){
sum+= Math.pow(-1, n)*Math.pow(x, 2*n)/fact(2*n);
}
//Calculate degrees from radians
degrees = x * 180 / Math.PI;
//Print the value of angle in radians as well as degrees
System.out.printf("The angle is %.2f radians, or %.2f degrees. ", x, degrees);
//Print the approximated value of cos(x)
System.out.println("The approximated value for cosine is: " + sum);
//close the scanner object
sc.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.