Advertisement
binibiningtinamoran

DistanceFormula

Oct 23rd, 2019
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class DistanceFormula {
  4.  
  5.     public static void main(String[] args) {
  6.         String orderedP1, orderedP2;
  7.         int x1, y1, x2, y2, tBase, tHeight;
  8.         double tArea, distance;
  9.  
  10.         Scanner scan = new Scanner(System.in);
  11.  
  12.        System.out.print("Enter an ordered pair: x,y: ");
  13.        orderedP1 = scan.nextLine();
  14.        x1 = Integer.parseInt(orderedP1.substring(0,1));
  15.        y1 = Integer.parseInt(orderedP1.substring(2));
  16.  
  17.         System.out.print("Enter an ordered pair: x,y: ");
  18.         orderedP2 = scan.nextLine();
  19.         x2 = Integer.parseInt(orderedP2.substring(0,1));
  20.         y2 = Integer.parseInt(orderedP2.substring(2));
  21.  
  22.         distance = Math.sqrt((Math.pow(Math.abs(y2-y1),2)) + (Math.pow(Math.abs(x2-x1),2)));
  23.         System.out.println("The distance between the ordered pairs " + orderedP1 + " and " + orderedP2 + " is: " + distance);
  24.         // Using printf()
  25.         System.out.printf("The distance between (%s) and (%s) is %.2f\n", orderedP1, orderedP2,
  26.                 distance);
  27.  
  28.         // Form a triangle ot of 2 ordered pairs
  29.         tBase = Math.abs(x1-x2);
  30.         tHeight = Math.abs(y1-y2);
  31.         tArea = (0.5) * (tBase * tHeight);
  32.  
  33.         System.out.printf("The triangle formed with (%s) and (%s) has an area of %,.2f\n",
  34.                 orderedP1, orderedP2, tArea);
  35.  
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement