In: Computer Science
Using Java, write a program named MyAngles that will prompt the user for the measure of the three sides of a triangle and then reports the measurement of each interior angle of the triangle and the area of the triangle.
To solve this question take input of the first three sides of the triangle. Make sure to input valid triangle sides. First to calculate angle we can use Law of cosine the cos() function to calculate the angle on each vertex. For the area just use the heron's formula to calculate the area.
I have included the necessary comments that will help you understand the solution.
import java.util.Scanner;
class MyAngles {
public static void main(String[] args) {
double a,b,c;
double area,resArea;
Scanner scan = new Scanner(System.in);
System.out.println("Provide valid triangle sides value");
System.out.print("Enter the three sides of triangle(use space seperated value): ");
// Input sides
a = scan.nextDouble();
b = scan.nextDouble();
c = scan.nextDouble();
// Angle calculation
double angleAtA = Math.acos((b*b + c*c - a*a)/(2*b*c));
double angleAtB = Math.acos((a*a + c*c - b*b)/(2*a*c));
double angleAtC = Math.acos((a*a + b*b - c*c)/(2*a*b));
angleAtA = Math.toDegrees(angleAtA);
angleAtB = Math.toDegrees(angleAtB);
angleAtC = Math.toDegrees(angleAtC);
System.out.println("The angle at A: " + Math.round(angleAtA *10)/10.0);
System.out.println("The angle at B: " + Math.round(angleAtB * 10)/10.0);
System.out.println("The angle at C: " + Math.round(angleAtC * 10)/10.0);
// Area calculation
area = (a+b+c)/2.0d;
resArea = Math.sqrt(area* (area - a) * (area - b) * (area - c));
System.out.println("Area of Triangle = " + resArea);
}
}
The output will as follows:
I hope you have understood
the solution. If you like the solution kindly upvote
it.