Advertisement
zed_com

Mary Task2

Oct 10th, 2022
922
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | Source Code | 0 0
  1. package local.itvoland;
  2.  
  3. import java.io.*;
  4. import java.io.FileWriter;
  5. import java.util.Scanner;
  6.  
  7. public class Main{
  8.     public static void main(String[] args) throws IOException {
  9.         try {
  10.             FileReader input = new FileReader("input.txt");
  11.         }catch(FileNotFoundException e){
  12.             FileWriter fileInput = new FileWriter("input.txt");
  13.             fileInput.write("10 20\n");
  14.             fileInput.write("20 10\n");
  15.             fileInput.write("20 20\n");
  16.             fileInput.write("10 10\n");
  17.             fileInput.close();
  18.         }
  19.  
  20.         FileReader input = new FileReader("input.txt");
  21.         FileWriter Writer = new FileWriter("output.txt");
  22.         Scanner scan = new Scanner(input);
  23.  
  24.         boolean res;
  25.         int x1 = 0, y1 = 0, x2 = 0, y2 = 0, x3 = 0, y3 = 0, x4 = 0, y4 = 0; // x(n), y(n) - точки квадрату (x, y)
  26.  
  27.         System.out.println("\nWelcome to a new math app!\n"); // інструктаж до роботи
  28.  
  29.         System.out.print("* Instruction to the app: *\n");
  30.         System.out.print("1. Enter points x1 and y1 for point A\n");
  31.         System.out.print("2. Enter points x2 and y2 for point B\n");
  32.         System.out.print("3. Enter points x3 and y3 for point C\n");
  33.         System.out.print("4. Enter points x4 and y4 for point D\n");
  34.         System.out.print("________________________________________\n");
  35.  
  36.  
  37.        System.out.print("Enter x1 & y1: "); // програма просить ввести дані x1, y1
  38.         x1 = scan.nextInt();
  39.         y1 = scan.nextInt();
  40.  
  41.         System.out.print("Enter x2 & y2: "); // програма просить ввести дані x2, y2
  42.         x2 = scan.nextInt();
  43.         y2 = scan.nextInt();
  44.  
  45.         System.out.print("Enter x3 & y3: "); // програма просить ввести дані x3, y3
  46.         x3 = scan.nextInt();
  47.         y3 = scan.nextInt();
  48.  
  49.         System.out.print("Enter x4 & y4: "); // програма просить ввести дані x4, y4
  50.         x4 = scan.nextInt();
  51.         y4 = scan.nextInt();
  52.  
  53.         res = isSquare(x1 ,y1 ,x2 ,y2 ,x3 ,y3 ,x4 ,y4);
  54.  
  55.         printSquare(x1 ,y1 ,x2 ,y2 ,x3 ,y3 ,x4 ,y4);
  56.  
  57. //        Writer.write(x1 ,y1 ,x2 ,y2 ,x3 ,y3 ,x4 ,y4);
  58.  
  59.         if (res) {
  60.             Writer.write("Correct! Your result is SQUARE!\n");
  61.         } else {
  62.             Writer.write("Entered data is not correct. Try again\n");
  63.         }
  64.         Writer.close();
  65.     }
  66.  
  67.     private static int distance(int x1, int y1, int x2, int y2){ // функція для розрахунку відстані
  68.         return ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
  69.     }
  70.  
  71.     private static boolean isSquare(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4){
  72.  
  73.         int AB = distance(x1, y1, x2, y2);
  74.         int AC = distance(x1, y1, x3, y3);
  75.         int AD = distance(x1, y1, x4, y4);
  76.  
  77.         if( AB == 0 || AC == 0 || AD == 0 )
  78.             return false; // відображається повідомлення про некоректність введених даних;
  79.  
  80.         if( AB == AC && 2 * AB == AD && distance( x2, y2, x3, y3 ) == AD)
  81.             return true;; // точки образують квадрат;
  82.  
  83.         if( AB == AD && 2 * AB == AC && distance( x2, y2, x4, y4 ) == AC)
  84.             return true; // точки образують квадрат;
  85.  
  86.         if( AD == AC && 2 * AC == AB && distance( x4, y4, x3, y3 ) == AB)
  87.             return true; // точки образують квадрат;
  88.         return false;
  89.     }
  90.  
  91.     private static void printSquare(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4){
  92.         int max = x1;
  93.         if(x1 < y1){
  94.             max = y1;
  95.         }else if(y1 < x2){
  96.             max = x2;
  97.         }else if(x2 < y2){
  98.             max = y2;
  99.         }else if(y2 < x3){
  100.             max = x3;
  101.         }else if(x3 < y3){
  102.             max = y3;
  103.         }else if(y3 < x4){
  104.             max = x4;
  105.         }else if(x4 < y4){
  106.             max = y4;
  107.         }
  108.  
  109.         String[][] square = new String[max+1][max+1];
  110.  
  111.         square[x1][y1] = "1";
  112.         square[x2][y2] = "2";
  113.         square[x3][y3] = "3";
  114.         square[x4][y4] = "4";
  115.  
  116.         System.out.println("");
  117.         for (String[] iArray: square) {
  118.             for(String j: iArray){
  119.                 if(j == null) {
  120.                     System.out.print("_");
  121.                 }else{
  122.                     System.out.print(j);
  123.                 }
  124.  
  125.             }
  126.             System.out.println("");
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement