TheFaceHugger TheFaceHugger - 1 year ago 112
Java Question

Law of Sines in Java

So I am trying to write a program for my AP comp sci class in High School, and we are told to find the angle measures of a triangle with only the side lengths given, I have used the law of cosigns to find the first angle and am trying to use the law of sines to find the second, however it doesn't work. I have read all the other threads over this and have added the

line to help fix it, but it still doesn't work. Here is my code(plz excuse the mess it is still a work in progress):

double s1 = 0;
double s2 = 0;
double s3 = 0;
double angles1 = 0;
double angles2 = 0;
System.out.println("Enter side 1 of your triangle");
s1 = key.nextDouble();
System.out.println("Enter side 2 of your triangle");
s2 = key.nextDouble();
System.out.println("Enter side 3 of your triangle");
s3 = key.nextDouble();

angles1 = Math.acos((s3*s3-s2*s2-s1*s1)/(2*s1*s2)); // angle from side 3

angles2 = Math.asin(Math.sin(Math.toRadians(angles1)) * s2 / s3);

angles1 = angles1 * 57.3;
angles1 = Math.round(angles1);

angles2 = angles2 * 57.3;
angles2 = Math.round(angles2);

System.out.println(" Angle 1 " + angles1);
System.out.println(" Angle 2 " + angles2);

Answer Source

Math.acos returns a value already in radians, so just remove the Math.toRadians and it will work fine:

angles2 = Math.asin(Math.sin(angles1) * s2 / s3);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download