main calls Math.cos and myCos twice.....

Task: a program that calls Math.cos and myCos twice, once passing Math.PI another passing Math.PI/6.0

myCos should compute and return the value of

1.0 - x^2/(2!) + x^4/(4!) - x^6/(6!) + ... + x^96/(96!) - x^98/(98!)

Code Java:

import java.util.Scanner;
class Cos
{
double x;
double sum = 0.0;
double count= 2.0;
double term= 1.0;
double sign = 1.0;
Scanner kb = new Scanner(System.in);
System.out.println("Enter number of term to sum:");
while (count <= 98)
{
sum = sum + sign* 1.0/term;
sign = -sign;
sum= count*(count-1)*sum;
term = term*x*x;
count ++;
double y= Math.pow(X4);
}
System.out.println("Math.cos =" +Math.cos);
System.out.println("myCos=" +sum)
}

Re: main calls Math.cos and myCos twice.....

You don't have your code inside of a method.

Re: main calls Math.cos and myCos twice.....

Oh, yes thank you! I believe my formula is not correct and that's my main problem. I don't understand how to formulate all the things that is asked for in this code.

Re: main calls Math.cos and myCos twice.....

I need help!!!!! I spent the whole day working on this, that's how bad I am.

Re: main calls Math.cos and myCos twice.....

Hard to help you with code when we can't see the code.

If you post any code please explain what your problems are.

Re: main calls Math.cos and myCos twice.....

Code Java:

public class Cos
{
public static void main(String[] args)
{
// your code goes here
}
}

If you are unsure of the Java syntax, I would recommend reading __The Java Tutorials__, starting with Getting Started and the Learning the Java Language sections.

Re: main calls Math.cos and myCos twice.....

Thank you for your help. Now I have this and I still have a problem with the factorial.....

Code Java:

import java.math.BigInteger;
import java.util.Scanner;
class Cos
{
public static void main(String[] args)
{
double sum = 0.0;
int f = 2, count = 1, sign = 1, n=0, numberTerms;
BigInteger factorial = BigInteger.ONE;
for (double i=1.0; i<=98; i++)
{
factorial=factorial.multiply(BigInteger.valueOf(long));
}
Scanner kb = new Scanner(System.in);
System.out.println("Enter number of terms to sum:");
numberTerms.kb.nextInt();
while (count <= numberTerms)
{
sum = sum + sign * Math.pow(kb,n)/factorial;
sign = -sign;
factorial = factorial+2;
n = n+2;
count++;
}
System.out.println("Mycos = " +sum);
System.out.println("Math.cos =" +Math.cos);
}
}

when trying to compile, it says class is expected in

Code Java:

factorial=factorial.multiply(BigInteger.valueOf(long));

Re: main calls Math.cos and myCos twice.....

Quote:

it says class is expected in

Please post the full text of the error message.

"long" is a keyword not a variable. Create and Use a variable for the valueOf argument.

Re: main calls Math.cos and myCos twice.....

O.K. So, now it compiles.... but it's not right. I believe it's my equation. =_= I don't know what to do.... I can't believe I have been doing this problem for a week now.... I need a book... Please help me >.<

Code Java:

import java.util.Scanner;
class Cos
{
public static void main(String[] args)
{
double sum = 0.0;
double count = 1, sign = 1, d=0, numberTerms=98;
double n = 3;
double f = n;
for(double p = (n - 1); p > 1; p--)
{
f = f * p;
}
Scanner input = new Scanner(System.in);
System.out.println("Enter number of terms to sum:");
double kb = input.nextDouble();
while (count <= numberTerms)
{
double t= Math.pow(kb,d);
sum = (sum + sign * t)/f;
sign = -sign;
d = d+2;
count++;
}
System.out.println("Mycos = " +sum);
System.out.println("Math.cos =" +Math.PI/6.0);
System.out.println("Math.cos =" +Math.PI);
}
}

Re: main calls Math.cos and myCos twice.....

Re: main calls Math.cos and myCos twice.....

Re: main calls Math.cos and myCos twice.....

Nope, I believe my factorial is wrong but I don't know how to correct it....

Re: main calls Math.cos and myCos twice.....

Quote:

my factorial is wrong

Can you do the steps manually and get the correct answer and then compare that with what the program does when you step thru it statement by statement.

Can you write a small test program that does just the factorial function and get that to work?