klasscho

Untitled

Dec 18th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. package com.company;
  2. import java.io.IOException;
  3. import java.util.Scanner;
  4. import java.io.FileWriter;
  5. import java.io.BufferedReader;
  6. import java.io.InputStreamReader;
  7. public class Main {
  8. public static double Epsilon(double LimitEps) {
  9. boolean isInCorrect = false;
  10. double Eps;
  11. do {
  12. try {
  13. Scanner num = new Scanner(System.in);
  14. Eps = num.nextInt();
  15. if ((Eps < LimitEps) && (Eps > 1)) {
  16. System.out.format("Your number must belong to the range of [%f..%d]!\n", LimitEps, 0);
  17. } else
  18. isInCorrect = true;
  19. } catch (Exception e) {
  20. System.out.println("Enter a correct value!");
  21. Eps = 0.0f;
  22. isInCorrect = true;
  23. }
  24. }
  25. while (isInCorrect);
  26. return Eps;
  27. }
  28.  
  29. public static double FunctionArgument(double limitX) {
  30. boolean isInCorrect = true;
  31. double x = 0.0f;
  32. do {
  33. try {
  34. Scanner num = new Scanner(System.in);
  35. x = num.nextInt();
  36. if ((x > 0) && (x < limitX)) {
  37. System.out.format("Your number must belong to the range of [%d..%d]!\n", 0, limitX);
  38. } else
  39. isInCorrect = false;
  40. } catch (Exception e) {
  41. System.out.println("Enter a correct value!");
  42. }
  43. }
  44. while (isInCorrect);
  45. return x;
  46. }
  47.  
  48.  
  49. public static double MaclaurinSeries(double Eps, double x) {
  50. double y = 0.0, value = 0.0;
  51. double factorial = 1.0, power = x, sign = 1.0;
  52. int n = 1;
  53. do {
  54. value = power / factorial;
  55. y += sign * value;
  56. n++;
  57. sign *= -1.0;
  58. power *= x * x;
  59. factorial *= (double) (2 * n - 1) * (2 * n - 2);
  60. } while (value > Eps);
  61. return y;
  62. }
  63.  
  64. public static void Input(double Eps, double x, double result) {
  65. System.out.printf("%.4f ", Eps, x, result);
  66. }
  67.  
  68.  
  69. public static void WriteToFile(double y, String FName) throws Exception {
  70. FileWriter FileOut = new FileWriter(FName);
  71. FileOut.write(y + " ");
  72. FileOut.close();
  73. System.out.println("Saved to file: " + FName);
  74. }
  75.  
  76. public static void main(String[] args) {
  77. final double LimitEps = 0.000001;
  78. final double LimitX = 124;
  79. String FileNameOut;
  80. double y;
  81. double Eps, x;
  82. BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  83. System.out.println("This program calculates the value of a function y = sin(x) with precision e by expanding the function in a Maclaurin series.");
  84. System.out.println("Enter the precision value:");
  85. Eps = Epsilon(LimitEps);
  86. System.out.println("Enter the value of the function argument:");
  87. x = FunctionArgument(LimitX);
  88. y = MaclaurinSeries(Eps, x);
  89. Input(Eps, x, y);
  90. try {
  91. FileNameOut = in.readLine();
  92. }catch (IOException e){
  93. FileNameOut = "";
  94. }
  95. try {
  96. WriteToFile(y, FileNameOut);
  97. }catch (Exception e){
  98.  
  99. }
  100.  
  101.  
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment