Trouble with law of Cosine program

Hello all you Java programmers out there. I have a tiny problem. The code is not allowing me to use the cos(double x) and acos(double x) methods even though I imported the math class. So....Whats wrong with my code???

/*

* Andre Taylor

* 10/26/2010

* This program is a prototype that is designed to use the law of cosines to solve oblique triangles

* The user inputs the given values and the program computes and displays

* solved triangle

*/

package obliquetrianglescosine;

import java.lang.Math; //*Imported to use cosine and arccosin methods

//Its not working for some reason. it Says unused port even though I

//call for the cos and arccos method

import java.util.Scanner;

/**

*

* @author owner

*/

public class cosineLaw {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

double a; //Declare variables for values given

double b;

double c;

double A;

double B;

double C;

System.out.println("So what info was given to you? If nothing is input put 0 where value is."); //User inputs info given if nothing given user inputs 0

System.out.print("a:");

Scanner input = new Scanner (System.in);

a= input.nextDouble();

System.out.print("b:");

b= input.nextDouble();

System.out.print("c:");

c= input.nextDouble();

System.out.print("A:");

A= input.nextDouble();

System.out.print("B:");

B= input.nextDouble();

System.out.print("C:");

C= input.nextDouble();

computeLastSide( a, b, C, c);

computeSecondAngle( a, b, c,A);

computeLastAngle( A, B, C);

System.out.println("Side c= "+c);

System.out.println("Angle A= "+A);

System.out.println("Angle B= "+B);

}

public static double computeLastSide(double a,double b,double C, double c) //computes the last side of triangle

{

c =(a*a+b*b)-2*a*b*cos(C);

return c;

}

public static double computeSecondAngle(double a, double b, double c, double A) //computes second angle

{

A = acos(((b*b+c*c)-a*a)/2*b*c);

return A;

}

public static double computeLastAngle(double A, double B, double C) //computes last angle

{

B= 180-A-C;

return B;

}

}

Re: Trouble with law of Cosine program

Quote:

Originally Posted by

**Delstateprogramer**
Hello all you Java programmers out there. I have a tiny problem. The code is not allowing me to use the cos(double x) and acos(double x) methods even though I imported the math class. So....Whats wrong with my code???

/*

* Andre Taylor

* 10/26/2010

* This program is a prototype that is designed to use the law of cosines to solve oblique triangles

* The user inputs the given values and the program computes and displays

* solved triangle

*/

package obliquetrianglescosine;

import java.lang.Math; //*Imported to use cosine and arccosin methods

//Its not working for some reason. it Says unused port even though I

//call for the cos and arccos method

import java.util.Scanner;

/**

*

* @author owner

*/

public class cosineLaw {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

double a; //Declare variables for values given

double b;

double c;

double A;

double B;

double C;

System.out.println("So what info was given to you? If nothing is input put 0 where value is."); //User inputs info given if nothing given user inputs 0

System.out.print("a:");

Scanner input = new Scanner (System.in);

a= input.nextDouble();

System.out.print("b:");

b= input.nextDouble();

System.out.print("c:");

c= input.nextDouble();

System.out.print("A:");

A= input.nextDouble();

System.out.print("B:");

B= input.nextDouble();

System.out.print("C:");

C= input.nextDouble();

computeLastSide( a, b, C, c);

computeSecondAngle( a, b, c,A);

computeLastAngle( A, B, C);

System.out.println("Side c= "+c);

System.out.println("Angle A= "+A);

System.out.println("Angle B= "+B);

}

public static double computeLastSide(double a,double b,double C, double c) //computes the last side of triangle

{

c =(a*a+b*b)-2*a*b*cos(C);

return c;

}

public static double computeSecondAngle(double a, double b, double c, double A) //computes second angle

{

A = acos(((b*b+c*c)-a*a)/2*b*c);

return A;

}

public static double computeLastAngle(double A, double B, double C) //computes last angle

{

B= 180-A-C;

return B;

}

}

Use Math.acos. You can't just say acos() as that's not a method of **your** class.

Also, be careful to make sure when checking for sines and cosines that it's returning degrees or radians like you want it to.

Re: Trouble with law of Cosine program

thanks and how would I be sure that its returning degrees(I dont want Radians)?and what of the regular cos(double x) that one is giving me an error too?

Re: Trouble with law of Cosine program

Look at the javadoc for the math class: Math (Java Platform SE 6)

You should be able to figure out if it is using radians or degrees....... then if you need to convert it, you can use toDegrees, or toRadians, which are both part of the Math class