klasscho

Untitled

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