| |
Due by 8:00 AM on Monday, October 25, 1998
In this assignment you will write methods using for and while loops
that compute some simple mathematical functions. This is a reimplementation
using methods of the calculations done for Homework 05 (with a change
to the way e is computed, as well).
Note: The purpose of this assignment is to do the computations using only
primitive arithmetic. Do not call any functions from the Math
package.
Note: The three parts described below each specify a method to be
written. These should all be combined into a single program named
NewApproxE and submitted as the solution to Problem 01.
- Write a method that takes as parameters a
double value x and an int
value n and returns the double
value of x raised to the power n.
The function should work for any integer value for
n, including negative values. (Be careful to
recall what it means to raise a number to a negative power!)
You should assume that any number (including 0)
raised to the power 0 is defined to be 1.
- Write a method that takes an
int
value n and computes and returns the
value of n! (the factorial of n).
Note that while the factorial will always be an integer, it can quickly
exceed the size of either of the integer types. Therefore,
it should be computed, stored, and returned as a double.
- The value of
e (the base of the natural logarithms)
raised to any number (floating point or integer) x can be
computed by the series:
1 2 3 4
x x x x x
e = 1 + ---- + ---- + ---- + ---- + ...
1! 2! 3! 4!
You are to write a method which uses this series to
approximate the value of e raised to a given power.
The method should take two double values as parameters:
the value of x and
an error amount called epsilon.
Whereas the last program we wrote to compute e to some power
computed a specified number of terms of the expansion,
this method should compute terms until it reaches a term whose
absolute value is smaller than the given error amount.
(Since you have already gone to
the trouble of computing it, that last term should be included in the
summation.)
Each term should be computed by making calls to the other two methods as
appropriate.
These methods should be put into a single program together with a
main method which prompts the user for a power of e to be
computed and an error amount and then prints the result of passing those
values to the third method above. The program should be named
NewApproxE.
|