Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.Console;
  4.  
  5. public class Main
  6. {
  7. public static void main(String[] args)
  8. {
  9. if(args.length != 2)
  10. {
  11. System.err.println("Invalid number of arguments");
  12. System.exit(1);
  13. }
  14. else
  15. {
  16.  
  17.  
  18. double x;
  19. double k;
  20. String error = "Invalid argument! You entered: ";
  21.  
  22. try {
  23. x = Double.parseDouble(args[0]);
  24. }
  25. catch (IllegalArgumentException e) {
  26. System.err.println(error + args[0]);
  27. x = 1;
  28. }
  29.  
  30. try {
  31. k = Integer.parseInt(args[1]);
  32. }
  33. catch (IllegalArgumentException e) {
  34. System.err.println(error + args[1]);
  35. k = 2;
  36. }
  37.  
  38.  
  39. if ( k <= 1 )
  40. {
  41. System.err.println("Invalid argument: " + k);
  42. System.exit(1);
  43. }
  44.  
  45. double Eps = 1 / Math.pow(10, k+1);
  46. double result = 0;
  47. double step = 1;
  48. int n = 1;
  49.  
  50. while (Math.abs(step) >= Eps)
  51. {
  52. result += step;
  53. step = (step * x * x) / (n * (n + 1));
  54. n += 2;
  55. }
  56.  
  57. String fmt = "%10." + k + "f\n";
  58. System.out.print("My Taylor: ");
  59. System.out.printf( fmt, result );
  60. System.out.printf( fmt, Math.cosh(x));
  61. System.exit(0);
  62. }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement