Advertisement
Sauka1337

4.2

May 19th, 2024
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.17 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5.  
  6. import static java.lang.Math.pow;
  7.  
  8. public class Main {
  9.     private static float yNext;
  10.     private static int x;
  11.     private static float eps;
  12.  
  13.     public static void main(String[] args) {
  14.  
  15.         Scanner scanner = new Scanner(System.in);
  16.  
  17.         System.out.println("This program finds square root of a number using Newton's method.");
  18.         int inputMethodChoice = chooseInputMethod(scanner);
  19.         float root;
  20.  
  21.         if (inputMethodChoice == 1) {
  22.             inputDataManually(scanner);
  23.         } else if (inputMethodChoice == 2) {
  24.             String filePath = findFile(scanner);
  25.             inputDataFromFile(filePath);
  26.         }
  27.         findSquareRoot(1);
  28.         outputAnswer();
  29.         writeToFile(scanner);
  30.  
  31.         scanner.close();
  32.  
  33.     }
  34.  
  35.     private static void outputAnswer() {
  36.  
  37.         System.out.println("Square root of x = " + yNext);
  38.     }
  39.     private static int chooseInputMethod(Scanner scanner) {
  40.         int choice;
  41.         do {
  42.             System.out.println("Choose an option:");
  43.             System.out.println("1. Input data manually");
  44.             System.out.println("2. Read data from a text file");
  45.  
  46.             while (!scanner.hasNextInt()) {
  47.                 System.out.println("Invalid input. Please enter 1 or 2.");
  48.                 scanner.next();
  49.             }
  50.             choice = scanner.nextInt();
  51.             scanner.nextLine();
  52.             if (choice != 1 && choice != 2) {
  53.                 System.out.println("Error! You have to input either 1 or 2.");
  54.             }
  55.         } while (choice != 1 && choice != 2);
  56.         return choice;
  57.     }
  58.  
  59.     private static void inputDataManually(Scanner scanner) {
  60.         do {
  61.             System.out.println("Input the X value:");
  62.  
  63.             while (!scanner.hasNextInt()) {
  64.                 System.out.println("Invalid input. Please enter a number.");
  65.                 scanner.next();
  66.             }
  67.             x = scanner.nextInt();
  68.             scanner.nextLine();
  69.             if (x < 0 || x > 1000000) {
  70.                 System.out.println("Error! You have to input number that is bigger than 0 and smaller than 1 000 000.");
  71.             }
  72.         } while (x < 0 || x > 1000000);
  73.  
  74.         do {
  75.             System.out.println("Input Epsilon value:");
  76.  
  77.             while (!scanner.hasNextFloat()) {
  78.                 System.out.println("Invalid input. Please enter a number.");
  79.                 scanner.next();
  80.             }
  81.             eps = scanner.nextFloat();
  82.             scanner.nextLine();
  83.             if (eps < 0 || eps >= 1) {
  84.                 System.out.println("Error! You have to input number that is bigger than 0 and smaller than 1.");
  85.             }
  86.         } while (eps < 0 || eps >= 1);
  87.     }
  88.  
  89.     private static void findSquareRoot(float yPrev) {
  90.         float temp;
  91.         yNext = (float) (0.5 * (yPrev + x / yPrev));
  92.         if (Math.abs(yNext - yPrev) > eps) {
  93.             temp = yNext;
  94.             findSquareRoot(temp);
  95.         }
  96.     }
  97.  
  98.     private static String findFile(Scanner scanner) {
  99.         String filePath;
  100.         boolean isCorrect;
  101.         do {
  102.             isCorrect = true;
  103.             System.out.print("Enter path to the file: ");
  104.             filePath = scanner.nextLine();
  105.             File file = new File(filePath);
  106.             if (!file.exists()) {
  107.                 isCorrect = false;
  108.                 System.out.println("File not found at the specified path.");
  109.             } else if (!filePath.endsWith(".txt")) {
  110.                 isCorrect = false;
  111.                 System.out.println("Error, wrong file type! File should have .txt extension.");
  112.             }
  113.         } while (!isCorrect);
  114.         System.out.println("File found.");
  115.         return filePath;
  116.     }
  117.     private static void inputDataFromFile(String path) {
  118.         float base;
  119.         float power;
  120.         double temp;
  121.  
  122.         try (Scanner fileScanner = new Scanner(new File(path))) {
  123.             System.out.println("Reading file...");
  124.  
  125.             String line = fileScanner.nextLine();
  126.             String[] parts = line.split(" ");
  127.  
  128.             base = Float.parseFloat(parts[0]);
  129.  
  130.             line = fileScanner.nextLine();
  131.             parts = line.split(" ");
  132.  
  133.             power = Float.parseFloat(parts[0]);
  134.  
  135.             line = fileScanner.nextLine();
  136.             parts = line.split(" ");
  137.  
  138.             x = Integer.parseInt(parts[0]);
  139.  
  140.             temp = base*Math.pow(10, -1*power);
  141.             eps = (float) temp;
  142.  
  143.         } catch (IOException e) {
  144.             System.out.println("Error reading file!");
  145.         }
  146.     }
  147.     private static void writeToFile(Scanner scanner) {
  148.         try {
  149.             System.out.print("Enter file name: ");
  150.             String fileName = scanner.nextLine();
  151.             fileName += ".txt";
  152.             FileWriter writer = new FileWriter(fileName);
  153.  
  154.             writer.write("Square root of x = " + yNext);
  155.  
  156.             writer.close();
  157.  
  158.             System.out.println("Results written to " + fileName);
  159.         } catch (IOException e) {
  160.             System.out.println("Error writing to file!");
  161.         }
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement