RGAlex

O3. Longer Line / Methods - More Exercise

Feb 10th, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. package MethodsMoreExercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class O3LongerLine {
  6.     public static void main(String[] args) {
  7.  
  8.         Scanner scanner = new Scanner(System.in);
  9.         int x1 = Integer.parseInt(scanner.nextLine());
  10.         int y1 = Integer.parseInt(scanner.nextLine());
  11.         int x2 = Integer.parseInt(scanner.nextLine());
  12.         int y2 = Integer.parseInt(scanner.nextLine());
  13.  
  14.         int x3 = Integer.parseInt(scanner.nextLine());
  15.         int y3 = Integer.parseInt(scanner.nextLine());
  16.         int x4 = Integer.parseInt(scanner.nextLine());
  17.         int y4 = Integer.parseInt(scanner.nextLine());
  18.  
  19.         printLongestLine(x1, y1, x2, y2, x3, y3, x4, y4);
  20.     }
  21.  
  22.     static void printLongestLine(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
  23.         double result1 = Math.sqrt((Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2)));
  24.         double result2 = Math.sqrt((Math.pow((x3 - x4), 2) + Math.pow((y3 - y4), 2)));
  25.         if (result1 >= result2) {
  26.             closestToZero(x1, y1, x2, y2);
  27.         } else {
  28.             closestToZero(x3, y3, x4, y4);
  29.         }
  30.     }
  31.  
  32.     private static void closestToZero(int x1, int y1, int x2, int y2) {
  33.         double result1 = Math.sqrt(Math.pow(x1, 2) + Math.pow(y1, 2));
  34.         double result2 = Math.sqrt(Math.pow(x2, 2) + Math.pow(y2, 2));
  35.         if (result1 <= result2) {
  36.             System.out.printf("(%d, %d)(%d, %d)", x1, y1, x2, y2);
  37.         } else {
  38.             System.out.printf("(%d, %d)(%d, %d)", x2, y2, x1, y1);
  39.         }
  40.     }
  41. }
  42.  
  43.  
Advertisement
Add Comment
Please, Sign In to add comment