Complete the method, areaOfTriangle(). The method will take in the side lengths,
ID: 3753761 • Letter: C
Question
Complete the method, areaOfTriangle(). The method will take in the side lengths, a, b, and c, of a triangle as double values. The method should return the area of the triangle as a double value. Calculate the area using Heron's formula, where S is the semi-perimeter of the triangle:
You may assume the values of a, b, and c are valid, side-lengths of a triangle.
Starter Code:
public class Triangle {
public static double areaOfTriangle(double a, double b, double c) {
//TODO: complete this method
}
}
Explanation / Answer
public class Triangle { public static double areaOfTriangle(double a, double b, double c) { double s = (a + b + c) / 2; return Math.sqrt(s * (s-a) * (s-b) * (s-c)); } public static void main(String[] args) { System.out.println(areaOfTriangle(3, 4, 5)); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.