Sine function, can't get factorial to work
I've been having issues with trying to get this program to work. The program's purpose is to compute the sine function without using the Math.sin(). At first I had trouble with the for loop but I finally understood how it worked, so it's not much of an issue anymore. My next problem is trying to get this factorial to work. I've look at tutorials and I think I'm doing the right thing but when I compile I get several errors (stated in code).
Original Code
Code java:
/* This program is suppose to compute the Sine function without using Math.sin */
import java.io.*;
public class x
{
public static void main(String args[]) throws IOException
{
BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));
double Rad, Sine;
int MaxE;
Sine = 0.0;
System.out.println("Enter Radians: ");
Rad = Double.parseDouble(keybd.readLine());
for (int a = 1; a <= 11; a++)
{
Sine += Math.pow(-1,a)*Math.pow(Rad,2*a+1)/factorial(2*a+1);
}
System.out.println("The value of Sine is " + Sine);
}
}
Errors
Code java:
x.java:20: cannot find symbol
symbol : method factorial(int)
location: class x
Sine += Math.pow(-1,a)*Math.pow(Rad,2*a+1)/factorial(2*a+1);
^
1 error
I think I'm on the right track but I'm not sure how to troubleshoot these errors yet since I'm still pretty new. Any help is appreciated!
Re: Sine function, can't get factorial to work
Hello Actinistia.
The issue is that factorial(2 * a + 1) is seen as a method and obviousally there isn't this method within your code.
factorial isn't even a variable within your code so what are you attempting to do here please?
Re: Sine function, can't get factorial to work
Quote:
Originally Posted by
JavaPF
Hello Actinistia.
The issue is that factorial(2 * a + 1) is seen as a method and obviousally there isn't this method within your code.
factorial isn't even a variable within your code so what are you attempting to do here please?
Well, I was looking at some code from a different forum, although I wasn't able to get it to work either, and it seemed to make some sense. I tried to incorporate it within my own code but wasn't able to do it successfully. Here's the code I'm referring to:
Code java:
public double sin(int n){
double sum = 0.0;
for(int i = 1; i <= n; i++){
//if i is even, -1 becomes 1
//else, -1 stays negative
sum += Math.pow(-1,i)/factorial(2*i+1);
}
return sum;
}
public double factorial(int i){
if(i <= 1) return 1;
double product = 1;
for(int j = i; j > 1; j--) product *= j;
return product;
}
It's probably due to my own inexperience since I've only been working with java for a few weeks or so. I did have the
Code java:
public double factorial(int i){
if(i <= 1) return 1;
double product = 1;
for(int j = i; j > 1; j--) product *= j;
return product;
}
in there before but I was getting errors so I just decided to delete it since I didn't know what to do with it.
Re: Sine function, can't get factorial to work
just this
import javax.swing.*;
class fak {
public static void main(String[]args) {
int hasil=1;
int f=Integer.parseInt(JOptionPane.showInputDialog("Ma sukkan Angka : "));
for(int i=1;i<=f;i++)
{
hasil=hasil*i;
if(f !=i)
System.out.print(i+"x");
else
System.out.print(i+" = ");
}
JOptionPane.showMessageDialog(null,"Hasil dari "+f+"! adalah "+hasil);
System.exit(0);
}
}
Re: Sine function, can't get factorial to work
Quote:
Originally Posted by
erdy_rezki
just this
What is that? And why resurrect a year old post, especially when the contribution is gibberish.