Advertisement
nadiahristova

Prob2_TriangleArea

Jan 24th, 2015
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Prob2_TriangleArea {
  4.     public static void main(String[] args) {
  5.         Scanner input = new Scanner(System.in);
  6.        
  7.         int[][] points = new int[3][2];
  8.         int[] positions = new int[]{0,1,2,0,1};
  9.         for (int i = 0; i < 3; i++) {
  10.             System.out.print("Enter the coordinates of the " + (i+1) + " point: ");
  11.             points[i][0] = input.nextInt();
  12.             points[i][1] = input.nextInt();
  13.             System.out.println();
  14.         }
  15.        
  16.         float sum =0f;
  17.         for (int i = 0; i < points.length; i++) {
  18.             sum = sum + points[positions[i]][0] * (points[positions[i+1]][1]-points[positions[i+2]][1]);
  19.         }
  20.        
  21.         float area = Math.abs(sum/2);
  22.         System.out.printf("The area of the triangle is: %.2f", area);
  23.     }
  24.  
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement