Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Java has a "sine"-computing function: Math.sin(#)
- To use it, you replace # by some value specifying an angle measured in radians.
- Programmers can use Java's Math.sin function when they need to compute
- a sine value. But, what if Java didn't have Math.sin? Or, what if
- you are the programmer who is asked by Sun Microsystems to write
- the program code for Math.sin? What would you do?
- One way to compute sine, is to use the infinite series expansion
- which you may have seen in a mathematics class: to find the sine of
- x radians (e.g., maybe x=3 radians, or x=0.5 radians, or etc.) you can compute:
- 3 5 7
- x x x
- sin(x) = x - --- + --- - --- + .......
- 3! 5! 7!
- Note: In mathematics, "7!" is "7 factorial", which means 7*6*5*4*3*2*1 = 5040
- Note: as the sin(x) equation shows, the true value of sine requires
- the computation of an infinite number of terms. But, since that's
- impossible to do, a programmer will only compute the sum to a finite
- number of terms, which will yield an APPROXIMATE value of the
- sine of x radians. The more terms you use in your sum, the more
- accurate your approximation will be. Typically, the larger the
- value of x, the larger is the number of terms you will need
- in order to get a reasonable approximation.
- Here, now, is your task:
- 1. Your HW7 class should have a "main" routine which performs as follows.
- It asks the user to input a value (in radians) for x.
- Hint: data type "double" is good for x, to allow the user
- to compute things like the sine of x=1.565
- (You should ask the user to input his/her angle in radians.
- But if you want to, you can instead ask the user to input
- an angle in degrees, and then programmatically convert it into radians.)
- Then, your main routine should ask the user to input a value for the
- maximum exponent to compute up to. E.g., 5 or 7 or etc.
- (You can use a variable maxe for this.)
- Then, your main routine should call a function called calcSin,
- which computes and returns the approximate sine of x, by using the
- summation formula explained earlier in these Specs.
- Lastly, your main routine should output this result to the screen.
- 2. Your HW7 class should have a function, calcSin(x,maxe).
- You will have to code this yourself.
- It should compute and return the following value:
- the approximate value of sin(x), using the summation formula
- explained earlier in these Specs. Specifically, it should compute the value of
- 3 5 7 maxe
- x x x x
- x - --- + --- - --- + ....... (+/-) ---
- 3! 5! 7! maxe!
- However, to compute the factorials, it should use a function factorial(n).
- 3. Your HW7 class should have a function, factorial(n),
- which computes and returns the value of "n!" (n factorial).
- Caution: if you use "int" for the return value, only run your program
- with maxe at most 11, since factorials larger than 12! will cause overflow
- since they are too large to fit in an "int" data type.
- -----------------------------------------------------------------------
- EXAMPLE:
- If the user inputs x as 6.5, and inputs maxe as 5, then
- your program should compute and output a variable named sum, where
- sum is computed as:
- 3 5
- 6.5 6.5
- sum = 6.5 - ---- + ----
- 3! 5!
- Note: since the user inputted a value of 5 for maxe (maximum exponent),
- then the sum only computes up to the term whose exponent is 5,
- instead of computing all of the infinite terms in the infinite series.
- ---------------------------------------------------------------------------
- SAMPLE RUNS:
- Here are some sample runs, to check if your program is outputting
- reasonably correct values (so you can tell if it's working correctly).
- Use the values of x and maxe below, to see if your program
- is working properly.
- Note: depending on how your program does its calculations,
- its output value might not be exactly identical to the
- output values shown below. But, it should at least be very close to
- the output values below, e.g. it should be the same up to about
- four or five decimal places.
- If your program's output is less accurate than that, then you may want
- to look over your code to check for errors.
- SAMPLE RUN:
- Input x (radians): 1
- Input maxe: 3
- The approximated sine value is 0.8333333333333334
- SAMPLE RUN:
- Input x (radians): 1
- Input maxe: 5
- The approximated sine value is 0.8416666666666667
- SAMPLE RUN:
- Input x (radians): 1
- Input maxe: 9
- The approximated sine value is 0.8414710097001764
- * THE TRUE SINE OF 1 RADIAN IS
- 0.8414709848...
- SO NOTICE THAT EVEN UP TO MAXE=9 (ONLY 5 TERMS IN THE SUM),
- THE APPROXIMATION IS QUITE GOOD!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement