0

I have been trying to code a program whereby the user inputs an angle, assuming it is degrees, it gets converted from degrees to radians and then calculates its sine or cosine. I tried to do this in a way which is convenient for the user but it turns out I have some issues trying to calculate both of these(cosine and sine) and outputting correct results. I appreciate your help. Also assuming constant number of terms which is 30.

import java.util.Scanner;

public class Question8 {
    public static double radians(double angle)
    {
        double pi = 3.141592653589793238462643;
        return angle / 180.0 * pi; //conversion from degrees to radians
    }

    public static void main( String [] args)
    {
        Scanner sc = new Scanner(System.in);
        int sw = 0, n, j=1, m=-1;
        double sine, i=0, r=0, cosine, c=0, rad; //initialising variables of type double and int

        do{
            System.out.println("Please input either 1 or 2 to calculate Sine or Cosine respectively.");
            sw = sc.nextInt();
            switch(sw) { //implementing a switch to differentiate between sine and cosine

                case 1:{ //this calculates the sine
                    System.out.println("Please input Angle and n (number of terms) to calculate sine");
                    sine = sc.nextDouble();
                    n = sc.nextInt();
                    rad = radians(sine);
                    i = rad;
                    for(int k = 3; k < n; k = k+2) {
                        double o = Math.pow(rad,k);
                        j = j*(k-1)*k;
                        r = o/j;
                        i=i+m*r;
                        m=m*(-1);

                    }
                    System.out.println("Sine " + sine + " = " + i);


                }
                break;

                case 2:{ //this calculates cosine
                    System.out.println("Please input angle and n to calculate cosine");
                    cosine = sc.nextDouble();
                    n = sc.nextInt();
                    rad = radians(cosine);
                    c = 1.0;
                    for(int k = 2; k < n; k = k+2) {
                        double o = Math.pow(rad,k);
                        j = j*(k-1)*k;
                        r = o/j;
                        c=c+m*r;
                        m = m*(-1);
                    }
                    System.out.println("Cosine " + cosine + " = " + c);

                }
                break;

                default: {
                    System.out.println("Invalid choice"); //user selects invalid numbers
                }
                break;
            }
        } while(sw != 0);
    }
}
Redent
  • 33
  • 7
  • Is there a reason that you are not using `Math.sin()` and `Math.cos()`? – Code-Apprentice Apr 02 '18 at 20:19
  • Yes, the project requests that I code those functions myself, I cannot use those – Redent Apr 02 '18 at 20:20
  • What happens when you run your program? What do you want it to do differently? – Code-Apprentice Apr 02 '18 at 20:20
  • I also suggest that you put the calculations for sine and cosine in separate methods. This will make your code much easier to read and to debug. – Code-Apprentice Apr 02 '18 at 20:21
  • My issue is that either sine or cosine have wrong answers. For example, I run the code as it is and it gives me all the answers for sine correct whereas for cosine (60) for example, it gives me 1.04 which is impossible – Redent Apr 02 '18 at 20:41

1 Answers1

1

Your algorithm for calculating sin and cos is correct. You should reinitialize variables m,j and r after each calculus. You can use CORDIC algorithm or chebyshev polynomial as more accurate substitute for Taylor series.

Sagar Pudi
  • 4,634
  • 3
  • 32
  • 51
biiyamn
  • 476
  • 3
  • 9
  • Thank you for the heads up! So basically I should initialize each one of those variable in every case of the switch? – Redent Apr 02 '18 at 21:49
  • 1
    yes, or put sin and cos in separate methods as suggested by Code-Apprentice – biiyamn Apr 02 '18 at 21:58
  • I did what you initially said, it worked fine thanks a lot! I did not try to separate them in methods because it would get too complicated for me to do but good to know it is another good approach – Redent Apr 02 '18 at 23:09
  • @Redent writing a separate method for cos() and for sin() would help you avoid this bug because each calculation will have its own variables. Also learning how to write methods is very important as a java programmer. – Code-Apprentice Apr 03 '18 at 00:20
  • @Code-Apprentice Thank you, I appreciate a ton! – Redent Apr 03 '18 at 20:14