Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class DistanceFormula {
- public static void main(String[] args) {
- String orderedP1, orderedP2;
- int x1, y1, x2, y2, tBase, tHeight;
- double tArea, distance;
- Scanner scan = new Scanner(System.in);
- System.out.print("Enter an ordered pair: x,y: ");
- orderedP1 = scan.nextLine();
- x1 = Integer.parseInt(orderedP1.substring(0,1));
- y1 = Integer.parseInt(orderedP1.substring(2));
- System.out.print("Enter an ordered pair: x,y: ");
- orderedP2 = scan.nextLine();
- x2 = Integer.parseInt(orderedP2.substring(0,1));
- y2 = Integer.parseInt(orderedP2.substring(2));
- distance = Math.sqrt((Math.pow(Math.abs(y2-y1),2)) + (Math.pow(Math.abs(x2-x1),2)));
- System.out.println("The distance between the ordered pairs " + orderedP1 + " and " + orderedP2 + " is: " + distance);
- // Using printf()
- System.out.printf("The distance between (%s) and (%s) is %.2f\n", orderedP1, orderedP2,
- distance);
- // Form a triangle ot of 2 ordered pairs
- tBase = Math.abs(x1-x2);
- tHeight = Math.abs(y1-y2);
- tArea = (0.5) * (tBase * tHeight);
- System.out.printf("The triangle formed with (%s) and (%s) has an area of %,.2f\n",
- orderedP1, orderedP2, tArea);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment