Advertisement
Guest User

Untitled

a guest
Dec 6th, 2010
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. Java has a "sine"-computing function: Math.sin(#)
  2. To use it, you replace # by some value specifying an angle measured in radians.
  3.  
  4. Programmers can use Java's Math.sin function when they need to compute
  5. a sine value. But, what if Java didn't have Math.sin? Or, what if
  6. you are the programmer who is asked by Sun Microsystems to write
  7. the program code for Math.sin? What would you do?
  8.  
  9. One way to compute sine, is to use the infinite series expansion
  10. which you may have seen in a mathematics class: to find the sine of
  11. x radians (e.g., maybe x=3 radians, or x=0.5 radians, or etc.) you can compute:
  12.  
  13. 3 5 7
  14. x x x
  15. sin(x) = x - --- + --- - --- + .......
  16. 3! 5! 7!
  17.  
  18. Note: In mathematics, "7!" is "7 factorial", which means 7*6*5*4*3*2*1 = 5040
  19.  
  20. Note: as the sin(x) equation shows, the true value of sine requires
  21. the computation of an infinite number of terms. But, since that's
  22. impossible to do, a programmer will only compute the sum to a finite
  23. number of terms, which will yield an APPROXIMATE value of the
  24. sine of x radians. The more terms you use in your sum, the more
  25. accurate your approximation will be. Typically, the larger the
  26. value of x, the larger is the number of terms you will need
  27. in order to get a reasonable approximation.
  28.  
  29.  
  30.  
  31. Here, now, is your task:
  32.  
  33. 1. Your HW7 class should have a "main" routine which performs as follows.
  34.  
  35. It asks the user to input a value (in radians) for x.
  36.  
  37. Hint: data type "double" is good for x, to allow the user
  38. to compute things like the sine of x=1.565
  39.  
  40. (You should ask the user to input his/her angle in radians.
  41. But if you want to, you can instead ask the user to input
  42. an angle in degrees, and then programmatically convert it into radians.)
  43.  
  44. Then, your main routine should ask the user to input a value for the
  45. maximum exponent to compute up to. E.g., 5 or 7 or etc.
  46. (You can use a variable maxe for this.)
  47.  
  48. Then, your main routine should call a function called calcSin,
  49. which computes and returns the approximate sine of x, by using the
  50. summation formula explained earlier in these Specs.
  51. Lastly, your main routine should output this result to the screen.
  52.  
  53.  
  54. 2. Your HW7 class should have a function, calcSin(x,maxe).
  55. You will have to code this yourself.
  56. It should compute and return the following value:
  57. the approximate value of sin(x), using the summation formula
  58. explained earlier in these Specs. Specifically, it should compute the value of
  59.  
  60. 3 5 7 maxe
  61. x x x x
  62. x - --- + --- - --- + ....... (+/-) ---
  63. 3! 5! 7! maxe!
  64.  
  65. However, to compute the factorials, it should use a function factorial(n).
  66.  
  67. 3. Your HW7 class should have a function, factorial(n),
  68. which computes and returns the value of "n!" (n factorial).
  69.  
  70. Caution: if you use "int" for the return value, only run your program
  71. with maxe at most 11, since factorials larger than 12! will cause overflow
  72. since they are too large to fit in an "int" data type.
  73.  
  74.  
  75.  
  76. -----------------------------------------------------------------------
  77.  
  78.  
  79. EXAMPLE:
  80.  
  81. If the user inputs x as 6.5, and inputs maxe as 5, then
  82. your program should compute and output a variable named sum, where
  83. sum is computed as:
  84.  
  85. 3 5
  86. 6.5 6.5
  87. sum = 6.5 - ---- + ----
  88. 3! 5!
  89.  
  90. Note: since the user inputted a value of 5 for maxe (maximum exponent),
  91. then the sum only computes up to the term whose exponent is 5,
  92. instead of computing all of the infinite terms in the infinite series.
  93.  
  94.  
  95. ---------------------------------------------------------------------------
  96.  
  97.  
  98. SAMPLE RUNS:
  99.  
  100. Here are some sample runs, to check if your program is outputting
  101. reasonably correct values (so you can tell if it's working correctly).
  102. Use the values of x and maxe below, to see if your program
  103. is working properly.
  104.  
  105. Note: depending on how your program does its calculations,
  106. its output value might not be exactly identical to the
  107. output values shown below. But, it should at least be very close to
  108. the output values below, e.g. it should be the same up to about
  109. four or five decimal places.
  110. If your program's output is less accurate than that, then you may want
  111. to look over your code to check for errors.
  112.  
  113.  
  114.  
  115. SAMPLE RUN:
  116.  
  117. Input x (radians): 1
  118. Input maxe: 3
  119. The approximated sine value is 0.8333333333333334
  120.  
  121.  
  122. SAMPLE RUN:
  123.  
  124. Input x (radians): 1
  125. Input maxe: 5
  126. The approximated sine value is 0.8416666666666667
  127.  
  128.  
  129. SAMPLE RUN:
  130.  
  131. Input x (radians): 1
  132. Input maxe: 9
  133. The approximated sine value is 0.8414710097001764
  134.  
  135.  
  136.  
  137. * THE TRUE SINE OF 1 RADIAN IS
  138. 0.8414709848...
  139. SO NOTICE THAT EVEN UP TO MAXE=9 (ONLY 5 TERMS IN THE SUM),
  140. THE APPROXIMATION IS QUITE GOOD!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement