Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import static java.lang.Math.pow;
- public class Main {
- private static float yNext;
- private static int x;
- private static float eps;
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.println("This program finds square root of a number using Newton's method.");
- int inputMethodChoice = chooseInputMethod(scanner);
- float root;
- if (inputMethodChoice == 1) {
- inputDataManually(scanner);
- } else if (inputMethodChoice == 2) {
- String filePath = findFile(scanner);
- inputDataFromFile(filePath);
- }
- findSquareRoot(1);
- outputAnswer();
- writeToFile(scanner);
- scanner.close();
- }
- private static void outputAnswer() {
- System.out.println("Square root of x = " + yNext);
- }
- private static int chooseInputMethod(Scanner scanner) {
- int choice;
- do {
- System.out.println("Choose an option:");
- System.out.println("1. Input data manually");
- System.out.println("2. Read data from a text file");
- while (!scanner.hasNextInt()) {
- System.out.println("Invalid input. Please enter 1 or 2.");
- scanner.next();
- }
- choice = scanner.nextInt();
- scanner.nextLine();
- if (choice != 1 && choice != 2) {
- System.out.println("Error! You have to input either 1 or 2.");
- }
- } while (choice != 1 && choice != 2);
- return choice;
- }
- private static void inputDataManually(Scanner scanner) {
- do {
- System.out.println("Input the X value:");
- while (!scanner.hasNextInt()) {
- System.out.println("Invalid input. Please enter a number.");
- scanner.next();
- }
- x = scanner.nextInt();
- scanner.nextLine();
- if (x < 0 || x > 1000000) {
- System.out.println("Error! You have to input number that is bigger than 0 and smaller than 1 000 000.");
- }
- } while (x < 0 || x > 1000000);
- do {
- System.out.println("Input Epsilon value:");
- while (!scanner.hasNextFloat()) {
- System.out.println("Invalid input. Please enter a number.");
- scanner.next();
- }
- eps = scanner.nextFloat();
- scanner.nextLine();
- if (eps < 0 || eps >= 1) {
- System.out.println("Error! You have to input number that is bigger than 0 and smaller than 1.");
- }
- } while (eps < 0 || eps >= 1);
- }
- private static void findSquareRoot(float yPrev) {
- float temp;
- yNext = (float) (0.5 * (yPrev + x / yPrev));
- if (Math.abs(yNext - yPrev) > eps) {
- temp = yNext;
- findSquareRoot(temp);
- }
- }
- private static String findFile(Scanner scanner) {
- String filePath;
- boolean isCorrect;
- do {
- isCorrect = true;
- System.out.print("Enter path to the file: ");
- filePath = scanner.nextLine();
- File file = new File(filePath);
- if (!file.exists()) {
- isCorrect = false;
- System.out.println("File not found at the specified path.");
- } else if (!filePath.endsWith(".txt")) {
- isCorrect = false;
- System.out.println("Error, wrong file type! File should have .txt extension.");
- }
- } while (!isCorrect);
- System.out.println("File found.");
- return filePath;
- }
- private static void inputDataFromFile(String path) {
- float base;
- float power;
- double temp;
- try (Scanner fileScanner = new Scanner(new File(path))) {
- System.out.println("Reading file...");
- String line = fileScanner.nextLine();
- String[] parts = line.split(" ");
- base = Float.parseFloat(parts[0]);
- line = fileScanner.nextLine();
- parts = line.split(" ");
- power = Float.parseFloat(parts[0]);
- line = fileScanner.nextLine();
- parts = line.split(" ");
- x = Integer.parseInt(parts[0]);
- temp = base*Math.pow(10, -1*power);
- eps = (float) temp;
- } catch (IOException e) {
- System.out.println("Error reading file!");
- }
- }
- private static void writeToFile(Scanner scanner) {
- try {
- System.out.print("Enter file name: ");
- String fileName = scanner.nextLine();
- fileName += ".txt";
- FileWriter writer = new FileWriter(fileName);
- writer.write("Square root of x = " + yNext);
- writer.close();
- System.out.println("Results written to " + fileName);
- } catch (IOException e) {
- System.out.println("Error writing to file!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement