Reputation: 23
very beginner programmer here (Started 2 weeks ago), I'm currently having problems with Math.atan. I currently use the Eclipse IDE and I have been trying to find out how to do tan^-1 for a triangle but it currently isn't working. I inputted 10 and 5 into the console but it comes out with 0.4636476090008061 when the actual answer is something like 63.43494882. I tried converting side1Two and side2Two into degrees, but it didn't work.
import java.util.Scanner;
public class triangleParts {
public static void main(String[] args) {
/*
* |\
* |A\
* | \
* Side 1 | \
* |_ \
* |_|__B\
* Side 2
*/
Scanner side1 = new Scanner(System.in);
System.out.println("Input side 1 here:");
double side1Two = side1.nextDouble();
Scanner side2 = new Scanner (System.in);
System.out.println("Input side 2 here:");
double side2Two = side2.nextDouble();
//Hypotenuse//
double hypotenuse = (Math.sqrt((side1Two * side1Two) + (side2Two * side2Two)));
System.out.println("Hypotenuse");
System.out.println(hypotenuse);
//Angle A//
double angleA = (Math.atan(side2Two/side1Two));
System.out.println("Angle A");
System.out.println(angleA);
//Angle B//
double angleB = (Math.atan(side1Two/side2Two));
System.out.println("Angle B");
System.out.println(angleB);
}
}
Upvotes: 1
Views: 570
Reputation: 22275
Here is your code fixed to use only one scanner, and converting to degrees since the output of Math.atan is in radians:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
/*
* |\
* |A\
* | \
* Side 1 | \
* |_ \
* |_|__B\
* Side 2
*/
System.out.println("53000's Simple Java Trigonometry Program");
System.out.println("========================================");
Scanner sc = new Scanner(System.in); //Declare Scanner
System.out.print("Input the length of side 1 here:");
double side1 = sc.nextDouble();
System.out.print("Input the length of side 2 here:");
double side2 = sc.nextDouble();
//Hypotenuse
double hypotenuse = (Math.sqrt((side1 * side1) + (side2 * side2)));
System.out.println("The length of the Hypotenuse is: " + hypotenuse);
//Angle A
double angleA = Math.toDegrees((Math.atan(side2/side1)));
System.out.println("The size of Angle A is: " + angleA);
//Angle B
double angleB = Math.toDegrees((Math.atan(side1/side2)));
System.out.println("The size of Angle B is: " + angleB);
}
}
Try it here!
Upvotes: 0
Reputation: 54148
//Angle A//
double angleA = Math.toDegrees((Math.atan(side2Two/side1Two)));
System.out.println("Angle A");
System.out.println(angleA);
//Angle B//
double angleB = Math.toDegrees((Math.atan(side1Two/side2Two)));
System.out.println("Angle B");
System.out.println(angleB);
This, is working ;)
Upvotes: 0
Reputation: 389
As Hovercraft Full Of Eels mentioned, the method returns a radian value, not a degree value. You can use the following built-in method to make it into degrees:
double getDegrees = Math.toDegrees((Math.atan(side2Two/side1Two)))
Upvotes: 1