Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package local.itvoland;
- import java.io.*;
- import java.io.FileWriter;
- import java.util.Scanner;
- public class Main{
- public static void main(String[] args) throws IOException {
- try {
- FileReader input = new FileReader("input.txt");
- }catch(FileNotFoundException e){
- FileWriter fileInput = new FileWriter("input.txt");
- fileInput.write("10 20\n");
- fileInput.write("20 10\n");
- fileInput.write("20 20\n");
- fileInput.write("10 10\n");
- fileInput.close();
- }
- FileReader input = new FileReader("input.txt");
- FileWriter Writer = new FileWriter("output.txt");
- Scanner scan = new Scanner(input);
- boolean res;
- int x1 = 0, y1 = 0, x2 = 0, y2 = 0, x3 = 0, y3 = 0, x4 = 0, y4 = 0; // x(n), y(n) - точки квадрату (x, y)
- System.out.println("\nWelcome to a new math app!\n"); // інструктаж до роботи
- System.out.print("* Instruction to the app: *\n");
- System.out.print("1. Enter points x1 and y1 for point A\n");
- System.out.print("2. Enter points x2 and y2 for point B\n");
- System.out.print("3. Enter points x3 and y3 for point C\n");
- System.out.print("4. Enter points x4 and y4 for point D\n");
- System.out.print("________________________________________\n");
- System.out.print("Enter x1 & y1: "); // програма просить ввести дані x1, y1
- x1 = scan.nextInt();
- y1 = scan.nextInt();
- System.out.print("Enter x2 & y2: "); // програма просить ввести дані x2, y2
- x2 = scan.nextInt();
- y2 = scan.nextInt();
- System.out.print("Enter x3 & y3: "); // програма просить ввести дані x3, y3
- x3 = scan.nextInt();
- y3 = scan.nextInt();
- System.out.print("Enter x4 & y4: "); // програма просить ввести дані x4, y4
- x4 = scan.nextInt();
- y4 = scan.nextInt();
- res = isSquare(x1 ,y1 ,x2 ,y2 ,x3 ,y3 ,x4 ,y4);
- printSquare(x1 ,y1 ,x2 ,y2 ,x3 ,y3 ,x4 ,y4);
- // Writer.write(x1 ,y1 ,x2 ,y2 ,x3 ,y3 ,x4 ,y4);
- if (res) {
- Writer.write("Correct! Your result is SQUARE!\n");
- } else {
- Writer.write("Entered data is not correct. Try again\n");
- }
- Writer.close();
- }
- private static int distance(int x1, int y1, int x2, int y2){ // функція для розрахунку відстані
- return ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
- }
- private static boolean isSquare(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4){
- int AB = distance(x1, y1, x2, y2);
- int AC = distance(x1, y1, x3, y3);
- int AD = distance(x1, y1, x4, y4);
- if( AB == 0 || AC == 0 || AD == 0 )
- return false; // відображається повідомлення про некоректність введених даних;
- if( AB == AC && 2 * AB == AD && distance( x2, y2, x3, y3 ) == AD)
- return true;; // точки образують квадрат;
- if( AB == AD && 2 * AB == AC && distance( x2, y2, x4, y4 ) == AC)
- return true; // точки образують квадрат;
- if( AD == AC && 2 * AC == AB && distance( x4, y4, x3, y3 ) == AB)
- return true; // точки образують квадрат;
- return false;
- }
- private static void printSquare(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4){
- int max = x1;
- if(x1 < y1){
- max = y1;
- }else if(y1 < x2){
- max = x2;
- }else if(x2 < y2){
- max = y2;
- }else if(y2 < x3){
- max = x3;
- }else if(x3 < y3){
- max = y3;
- }else if(y3 < x4){
- max = x4;
- }else if(x4 < y4){
- max = y4;
- }
- String[][] square = new String[max+1][max+1];
- square[x1][y1] = "1";
- square[x2][y2] = "2";
- square[x3][y3] = "3";
- square[x4][y4] = "4";
- System.out.println("");
- for (String[] iArray: square) {
- for(String j: iArray){
- if(j == null) {
- System.out.print("_");
- }else{
- System.out.print(j);
- }
- }
- System.out.println("");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement