## Transcribed Text

The assignment includes three projects.
You have to know what it means n factorial.
The factorial of a nonnegative integer n is denoted as n! (pronounced
“n factorial”) is defined as
n! = n * (n-1) * … * 1
for n greater than or equal to 1 and
n! = 1 for n = 0.
For example 4! = 4 * 3 * 2 * 1 which is 24.
Project a). Write an application that asks user to input a nonnegative
integer and computes and prints its factorial.
For next two projects I will give you some optional math information.
It is shown that every smooth function may be presented as a Taylor
series of infinite number of items sum of which is converged to this
function.
An important example of such a function is the exponential function
exp{x} that is e
x
where e is Euler number 2.71828183…, and x is
decimal number. Its Taylor series looks like
e
x = 1 + x /(1!) + x
2
/(2!) + x
3
/(3!) + … (1)
So to find exp{x} we need to find the sum of infinite number of items
in (1). We will use (1) to estimate the e
x
. But we can use only a finite
number n of items in (1).
e
x
1 + x /(1!) + x
2
/(2!) + … x
n
/(n!) (2)
Therefore we will calculate the e
x
with an error that is
Rn = e
x - [1 + x /(1!) + x
2
/(2!) + … +x
n
/(n!)] (3)
It may be shown that
2
Rn =
1
( 1)!
e n
x
n
(4)
The number ε is located between 0 and x and depends from x and n.
For whichever fixed value x the absolute error Rn in (3) converges to 0
when n converges to infinity:
R 0 n
when
n . So for whichever
fixed value x the function exp{x} may be evaluated as (2). There
may be found a number n for which the absolute error Rn will become
less then whichever positive given in advance small number.
If x = 1 we will estimate the value of Euler number e as
e 1 + 1/(1!) + 1/(2!) + … + 1/( n!) (5)
In this case the error of estimation is
Rn =
( 1)!
e
n
, where
0 1 1 3 e e e e .
So for Euler number e the absolute error of estimation e is
Rn
<
3
( 1)! n
(6)
If 0 < x < 1 we will use estimation (2) with absolute error
Rn
<
3
( 1)! n
(6a)
If x > 1 the error of approximate equation (2) will be bigger. Let the
first whole number bigger than x is N, N-1 < x <= N. Then from (4)
because
3
x N N e e e
and
n n 1 1 x N
we will obtain
Rn
<
1
3
( 1)!
N n N
n
(7)
3
And this error will converge to 0 when
n .
If x < 0 the series (2) is sign-changing, e
in (4) is less than 1. If
|x| < 1, then
Rn
<
1
( 1)! n
(8)
In this case error (8) of approximate equation (2) is the smallest one.
If x < 0 and |x| > 1 the
n 1 n 1
x N
where N-1 < |x| <= N. Then
the absolute error
Rn
<
1
( 1)!
n N
n
(9)
It is bigger then (8) but less then (7).
Project b). In math there are some very useful constants such as
(you are using it to find circumference and area of a circle), natural
number e, etc. Their values may be estimated using their Taylor’s
series. Use equation (5) to approximate the natural Euler number e.
The sum of the right side of (5) converges to the actual value of the
natural number e with increasing the number n of items in this sum.
If n = 0
e 1.
If n = 1
e 1 + 1/(1!) = 2.
If n = 2
e 1 + 1/(1!) + 1/(2!) = 2.5
If n = 3
e 1 + 1/(1!) + 1/(2!) + 1/(3!) 2.67.
Pay attention that the sum of any finite number n of item in (5) cannot
exceed the limiting value of e despite of the fact that every item in the
right part of (5) is positive and number n of items is not limited (try to
explain why?).
4
Write an GUI application that will help you to investigate how quick the
right part of (5) converges to its left part. Your code should print a
table with value of estimated e by using n = 0, n = 1, … , n = 10 items
in the right part of (5). Calculate the running error (6). Compare your
results with actual value of e that approximately can be represented
by Math.E. Here Math is the Java API class that is located in the
package java.lang. This package is automatically imported in every
Java class. The E is a static public final (constant) data member of the
class Math.
Advanced version of project b. Ask user with what precision (absolute
error) user want to calculate the value e, find the smallest number of
items n in (5) that will satisfy this demand. It means that running
error should be less then precision given by user. Show the results.
Project c). In math it is often used the expression e
x
, where x is a
decimal number and e is the natural number estimated in the Project
b).
The Taylor’s series for estimating e
x
is (1) and (2) approximates this
value.
The value of x may be positive or negative.
If x = 0 the value e
x
equals to a constant (guess what?).
The right side of (2) converges to its left side quicker when |x| < 1
and slowly when |x| > 1 (guess why?).
If x < 0 the right sight of (2) is a sign alternative series where the
items with x in odd degree are negative and items with x in even
degree are positive. Because of that the sum in the right side of (2)
may be and less and more than the value on the left side. But the
absolute value of the difference between the sum of members on the
right side and the value on the left side of (2) should decrease with
increasing the number of items n.
Write an application that will estimate e
x by using (2) with running
error (6a), or (7), or (8), or (9). Calculate e
x with n = 1, n = 2, … , n
= 10 items in (2).
Advanced Project c. Write an application that tells user that it will
calculate e
x
. Ask user the value of x and the precision with which e
x
5
should be calculated. Compare this precision with corresponding
running error to satisfy the user demand. In your output show what
are values of the Math.exp(x), e
x
, the user’s precision epcilon and
number of items n that satisfies the precision epcilon.
Use your code to calculate e
x
for -1 < x < 0, x < -1, x > 0, x = 1, x >
1 and x = 0.
Write a client class with GUI that will perform all tasks for Project a),
Project b), and Project c). Do repetition statement as simple and
effective as possible.

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction
of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice.
Unethical use is strictly forbidden.

import java.util.*;

/*

* Projects Class containing static methods

* implementing Math operations and user

* interactivity for Projects A, B, and C

*/

public class Projects {

// Method to recursively compute Factorial

public static long factorial(int n) {

if (n == 0) {

return 1;

} else {

return n*factorial(n-1);

}

}

// Project A execution and user interaction

public static void ProjectA() {

System.out.println("--- Factorial Calculator Program ---");

Scanner s = new Scanner(System.in);

while (true) {

try {

System.out.print("Please enter a nonnegative integer: ");

int n = Integer.parseInt(s.nextLine());

if (n < 0) {

System.out.println("ERROR: Invalid input");

continue;

}

System.out.println(n+"! = "+factorial(n));

break;

} catch (Exception e) {

System.out.println("ERROR: Invalid input");

}

}

}...