Write a method that returns the area of a triangle using the following header: p
ID: 3781399 • Letter: W
Question
Write a method that returns the area of a triangle using the following header: public static double getTriangleArea(double[][] points) The points are stored in a 3-by-2 two-dimensional array points with points [0] [0] and points [0] [1] for (xl, y1). The triangle area can be computed using the formula in Programming Exercise 2.19. The method returns 0 if the three points are on the same line. Write a program that prompts the user to enter three points of a triangle and displays the triangle's area. Here is a sample run of the program: Enter x1, y1, x2, y2, x3, y3: 2.5 2 5 -1.0 4.0 2.0 The area of the triangle is 2.25 Enter x1, y1, x2, y2, x3, y3: 2 2 4.5 4.5 6 6 The three points are on the same lineExplanation / Answer
import java.util.Scanner;
public class getTriangleArea {
public static void main(String[] args) {
double[] c = new double[6];
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter three points for a triangle: ");
String[] input = keyboard.nextLine().split(" ");
for(int i = 0; i < c.length; i++)
c[i] = Double.parseDouble(input[i]);
if((c[0]==c[1])&&(c[2]==c[3])&&(c[4]==c[5]))
{
System.out.printf("points lie in same line");
}
else
{
double s1 = Math.sqrt(Math.pow(c[0] - c[2], 2) + Math.pow(c[1] - c[3], 2));
double s2 = Math.sqrt(Math.pow(c[2] - c[4], 2) + Math.pow(c[3] - c[5], 2));
double s3 = Math.sqrt(Math.pow(c[4] - c[0], 2) + Math.pow(c[5] - c[1], 2));
double s = (s1 + s2 + s3) / 2;
double area = Math.sqrt(s * (s - s1) * (s - s2) * (s - s3));
System.out.printf("The area of the triangle is %.1f.", area);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.