So far my professor has given us code and we basically "fill in the blanks" of the methods below the main code. I've done all the other methods, but am now stuck on the Taylor series portion. The code below is to calculate angles, and one of the methods called computes the sine of a given angle. Since the taylor series is infinite and seeing as I have little knowledge of Java, any help is appreciated. The code in question is on line 85.

/**
 * 
 */
package cecs174;
/**
 * 
 * @author Nicholas Colburn
 *
 */

public class AssignmentSeven 
{
	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		int[] angles = new int[16];
		angles[0] = 0; angles[1] = 90; angles[2] = 180; angles[3] = 270;
		angles[4] = 0 + 30; angles[5] = 90 + 60; angles[6] = 180 + 30; angles[7] = 270 + 60;
		angles[8] = 0 + 60; angles[9] = 90 + 30; angles[10] = 180 + 60; angles[11] = 270 + 30;
		angles[12] = 0 + 45; angles[13] = 90 + 45; angles[14] = 180 + 45; angles[15] = 270 + 45;
				
		for(int ii = 0; ii < 16; ++ii)
		{
			if(ii % 4 == 0) System.out.println("");
			showResults( angles[ii] );
		}
	}

	/**
	 * Print the results you see in the desired output.
	 * 
	 * @param degrees the number of degrees for processing
	 */
	private static void showResults(int degrees)
	{
	 	String quadrant = askQuadrant(degrees);	
		double radians = askRadians(degrees);
		double sine = askSine(degrees);
		double cosine = askCosine(degrees);
		
		System.out.printf("Degrees: %3d, Radians: %3.3f, Quadrant: %-6s,      Sine: %7.3f, Cosine: %7.3f\n",
						   degrees, radians, quadrant, sine + .00005, cosine + .00005);
		
	}

	/**
	 * Determines the axis and quadrant a degree is located in.
	 * @param degrees variable inputed from user used to find the axis and quadrant
	 * @return the axis and quadrant number
	 */
	private static String askQuadrant(int degrees)
	{	
		String quadrant = "";
		if(degrees == 0)quadrant += "X-axis";
		else if(degrees == 90)quadrant += "Y-axis";
		else if(degrees >= 270)quadrant += "Fourth";
		else if(degrees >= 180)quadrant += "Third";
		else if(degrees >= 90)quadrant += "Second";
		else quadrant += "First";
		return quadrant;
	}

	/**
	 * Finds the number of radians for inputed degrees.
	 * @param degrees 
	 * @return the number of radians
	 */
    	private static double askRadians(int degrees)
	{
    		double radians = PI / 180 * degrees;
    		return radians;	
	}

	/**
	 * 
	 * @param degrees
	 * @return
	 */
	private static double askSine(int degrees)
	{
		int d = degrees;
		double r = askRadians(degrees);
		double sin = r - (Math.pow(r, 3)) / factorial(3) + (Math.pow(r, 5)) / factorial(5) - (Math.pow(r, 7)) / factorial(7) + (Math.pow(r, 9)) / factorial(9); // incomplete
	}
	
	/**
	 * 
	 * @param degrees
	 * @return
	 */
	private static double askCosine(int degrees)
	{
		
		
	}
	
	/**
	 * Computes the factorial for the an inputed number.
	 * @param n the number to be computed
	 * @return the factorial of the number inputed
	 */
	private static int factorial(int n)
	{
		int factorial = 0;
		if(n == 0)return 1;
		for(int ii = 0; ii > 0; ++ii)
			factorial = n * (n - ii);
		return factorial;
	}
	
	/**
	 * Scales the inputed degrees to local quadrant values.
	 * @param degrees the inputed degrees being scaled down
	 * @return the scaled degrees
	 */
	private static int scale(int degrees)
	{
		int scale = 0;
		if(degrees == 0)scale = 0;
		else if(degrees == 90)scale = 90;
		else if(degrees >= 270)scale = degrees;
		else if(degrees >= 180)scale = 180 - degrees;
		else if(degrees >= 90)scale = degrees - 180;
		else scale = 360 - degrees;
		return scale;		
	}
	
	private static final double PI = 3.141592653589793238;
	private static final int NUM_TERMS = 10;
	
}

Edited 5 Years Ago by nickcolb: n/a

Have you got a specific precision you want to achieve? Otherwise just put it in a for-loop like

double tSeries = 0.0;
for(int i = 3; i < SOME_NBR_TO_ENSURE_PRECISION; i += 2){
    if(add){
        tSeries += Math.pow(r,i) / factorial(i);
    } else {
        tSeries -= Math.pow(r,i) / factorial(i);
    }
}

Wont give you all the code, but that should give you something to work with :) Good luck!

Edited 5 Years Ago by Slimmy: code looking bad

This article has been dead for over six months. Start a new discussion instead.