Write a program that prompts the user to enter the lengths of the side of a tria
ID: 3809240 • Letter: W
Question
Write a program that prompts the user to enter the lengths of the side of a triangle and prints its three angles. The calculation of the angle values should take place in a separate method. You will pass the three side lengths as parameters to the method and it will return the angle as the result. You will have to call the method three times (once for each angle). Round your answers to two decimal places. If the lengths of all three sides of any triangle are known the three angles can be calculated as follows: lpha =rccos left({rac {b^{2}+c^{2}-a^{2}}{2bc}} ight) eta =rccos left({rac {a^{2}+c^{2}-b^{2}}{2ac}} ight) gamma =rccos left({rac {a^{2}+b^{2}-c^{2}}{2ab}} ight)
Explanation / Answer
package myProject;
import java.util.*;
//Class Angle definition
public class Angle
{
//Method calculates and returns the first angle
public static double angleA(double a, double b, double c)
{
return Math.acos((Math.pow(b, 2) + Math.pow(c, 2) - Math.pow(a, 2)) / (2 * b * c));
}//end of method
//Method calculates and returns the second angle
public static double angleB(double a, double b, double c)
{
return Math.acos((Math.pow(a, 2) + Math.pow(c, 2) - Math.pow(b, 2)) / (2 * a * c));
}//end of method
//Method calculates and returns the third angle
public static double angleC(double a, double b, double c)
{
return Math.acos((Math.pow(a, 2) + Math.pow(b, 2) - Math.pow(c, 2)) / (2 * a * b));
}//End of method
//Main method
public static void main(String[] args)
{
//Scanner class object created
Scanner sc = new Scanner(System.in);
//Accepts 3 side data
System.out.println("Enter the lengths of the side of a triangle: ");
System.out.println("Enter side A: ");
int a = sc.nextInt();
System.out.println("Enter side B: ");
int b = sc.nextInt();
System.out.println("Enter side C: ");
int c = sc.nextInt();
//Calls method and displays the result
System.out.printf(" First = %.2f", angleA(a, b, c));
System.out.printf(" Second = %.2f", angleB(a, b, c));
System.out.printf(" Third = %.2f", angleA(a, b, c));
}//End of main method
}//End of class
Sample Run 1:
Enter the lengths of the side of a triangle:
Enter side A: 10
Enter side B: 12
Enter side C: 14
First = 0.78
Second = 1.00
Third = 0.78
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.