Question: Ex.java This is an exercise in using repetition structures to calculate the mathematical values of sum of a series. Since methods have not yet been
Ex.java
This is an exercise in using repetition structures to calculate the mathematical values of sum of a series. Since methods have not yet been covered in detail, you may do the calculations in a class that contains just a main method. The computation itself requires looping structures that use of count-controlled loops.
-
This program computes an estimate of the value of the mathematical value ex, which is defined, for a given exponent x 0, and for n > 0 terms of a series, as:
As n is made greater, the estimate of e becomes more precise. If the computation is done with doublecomputations, then after some number of additional terms (some value of n), the precision of the doublerepresentation will be reached, and no further improvement will be seen. However, for large values of x, it may not be possible to obtain an estimate.
The value computed should be a Java floating-point value (please use double). The program should first ask the user for an integer value n 0, which indicates the number of terms to be evaluated. If the value entered is less than zero, a message must be given the user, and the user allowed to try again. If the value is 0, the program should just exit.
If the number of terms value is greater than 0, then the user should be asked to enter a value for the exponent x. The exponent may be any positive double value, zero or fractional. If a negative value of x is entered, the program should allow the user to start over by requesting the number of terms again. (The series works for negative values, but requires some additional precautions that will not be explored here).
As before, make the program continue to loop until the user asks for a zero number of terms to be used.
Some values of the exponent will prevent the program getting correct results. If the correct value cannot be computed, tell the user that is the problem, and keep asking for another number of terms and an exponent value until an acceptable number (or 0) is entered. (Note that if the exponent, x == 1, then the value obtained should be for just the value of e itself. It should always be possible to obtain an estimate for this value, for any number of terms.)
Print out your estimates of the value of e raised to the given power to at least 15 significant places.
Do not use the Math.pow or Math.exp methods for this exercise (other than for debugging or comparison purposes); the intent is to evaluate the given series in a manner similar to what the Math.exp method itself would have to use internally to compute the same values.
-
Example of Input and Output for the program Ex.java
Here are some examples of input and output for the program Ex.java. The number of terms shown in these examples are close to the minimum needed to achieve double type accuracy for the given exponentsyou should attempt to replicate these results:
Enter a positive number of terms (or 0 to quit): 16 Enter an exponent (positive, zero or fractional): 0.5 After 16 terms estimate of e to the power 0.500000 is 1.64872127070013 Enter a positive number of terms (or 0 to quit): 18 Enter an exponent: 1 After 18 terms estimate of e to the power 1 is 2.71828182845905 Enter a positive number of terms (or 0 to quit): 67 Enter an exponent: 20 After 67 terms estimate of e to the power 20 is 485165195.409790 Enter a positive number of terms (or 0 to quit): 190 Enter an exponent: 100 After 190 terms estimate of e to the power 100 is 2.68811714181614e+43 Enter a positive number of terms (or 0 to quit): 936 Enter an exponent: 709 After 936 terms estimate of e to the power 100 is 8.21840746155499e+307 Enter a positive number of terms (or 0 to quit): 347 Enter an exponent: 1000 That value is too large to be able to compute that exponent of e; please try again.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
