1

This is the problem: Write a program that prompts for the lengths of the sides of a triangle and reports the three angles.

I have written the following code for it:

public static void main(String[] args) {
    
    Scanner console = new Scanner(System.in);
    
    System.out.print("Please input length of side A: ");
    int sideA = console.nextInt();
    System.out.print("Please input length of side B: ");
    int sideB = console.nextInt();
    System.out.print("Please input length of side C: ");
    int sideC = console.nextInt();
    System.out.println();
    System.out.println("The angle between A and B is: " + calculateAngle(sideA, sideB, sideC));
    System.out.println("The angle between B and C is: " + calculateAngle(sideB, sideC, sideA));
    System.out.println("The angle between C and A is: " + calculateAngle(sideC, sideA, sideB));     
}
    
    public static double calculateAngle(int a, int b, int c) {
        return Math.toDegrees(Math.acos((a * a + b * b - c * c) / (2 * a * b)));
    }

Here is a sample output from my code above:

Please input length of side A: 55
Please input length of side B: 22
Please input length of side C: 76

The angle between A and B is: 90.0
The angle between B and C is: 90.0
The angle between C and A is: 90.0

No matter what values I input for the sides, the only angles I ever get are 90 or 180 degrees, never the actual correct angle that can be calculated from the cosine rule. What is wrong with my code?

johnzenten
  • 15
  • 5
  • To clarify @Aplet123 comment - integer division means the `acos` in your calculation is always being given zero which is 90 degrees. Change the variables to `float` or `double`, or cast the result in the calculation. – stridecolossus Dec 13 '20 at 13:56

2 Answers2

1

Just cast the calculation in Math.acos to double :

return Math.toDegrees(Math.acos((double)(a * a + b * b - c * c) / (2 * a * b)));

As you could see in comments, when computing between multiple int types, the integer arithmetic is used and then casted to double.

Also it's worth noting, that int always round down, meaning:

int i = 0.9999999;  // i = 0
AP11
  • 598
  • 3
  • 10
0

According to documentation they are expecting a double value as their parameter for acos method JavaSE7 Math doc

so re-arrange your code like this

  public static void main(String[] args) {

        Scanner console = new Scanner(System.in);

        System.out.print("Please input length of side A: ");
        double sideA = console.nextDouble();
                System.out.print("Please input length of side B: ");
        double sideB = console.nextDouble();
        System.out.print("Please input length of side C: ");
        double sideC = console.nextDouble();
        System.out.println();
        System.out.println("The angle between A and B is: " + calculateAngle(sideA, sideB, sideC));
        System.out.println("The angle between B and C is: " + calculateAngle(sideB, sideC, sideA));
        System.out.println("The angle between C and A is: " + calculateAngle(sideC, sideA, sideB));
    }

    public static double calculateAngle(double a, double b, double c) {
        return Math.toDegrees(Math.acos((a * a + b * b - c * c) / (2 * a * b)));
    }