Skip to content

Commit 41269e6

Browse files
ch07p2
1 parent c3bbb61 commit 41269e6

3 files changed

Lines changed: 83 additions & 5 deletions

File tree

‎ch05/GuessStarterRecursive.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static void main(String[] args) {
2525
}
2626

2727
/**
28-
* This method prompt the user for his guess,
28+
* This method prompts the user for his guess,
2929
* gets the value and tells him if it was too high or too low.
3030
* @param random the random generated number to guess
3131
*/

‎ch06/Exercise6.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static int oddSum(int n){
6666
}
6767

6868
/**
69-
* This method calculate the value of the
69+
* This method calculates the value of the
7070
* Ackermann function given two non-negative integers
7171
* @param m the first non-negative integer
7272
* @param n the second non-negative integer
@@ -86,7 +86,7 @@ public static int ack(int m, int n){
8686
}
8787

8888
/**
89-
* This recursive method calculate
89+
* This recursive method calculates
9090
* the value of the power x^n.
9191
* @param x the base of the power
9292
* @param n the exponent of the power

‎ch07/Exercise7.java‎

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ public static void main(String[] args) {
77
loop(10);
88
System.out.println(squareRoot(9.0));
99
System.out.println(power(2.0,8));
10+
System.out.println(factorial(8));
11+
System.out.println(myexp(8.0,100));
12+
check(1);
13+
for(double i=0.1; i<=100.0; i*=10){
14+
check(i);
15+
}
16+
System.out.println("The values of exponential e^x for x=-0.1,-1.0,-10.0,-100.0 are:");
17+
for(double i=-0.1; i>=-100.0; i*=10){
18+
System.out.println(myexp(i,100));
19+
}
20+
System.out.println(gauss(2.0,100));
1021
}
1122

1223
public static void loop(int n) {
@@ -22,7 +33,7 @@ public static void loop(int n) {
2233
}
2334

2435
/**
25-
* This method calculate an approximation
36+
* This method calculates an approximation
2637
* of the square root of a number
2738
* @param a the number whose root will be found
2839
* @return the approximated result of the square root
@@ -56,7 +67,7 @@ public static double squareRoot(double a){
5667
}
5768

5869
/**
59-
* This method calculate
70+
* This method calculates
6071
* the value of the power x^n.
6172
* @param x the base of the power
6273
* @param n the exponent of the power
@@ -80,4 +91,71 @@ public static double power(double x, int n) {
8091
return power;
8192
}
8293

94+
/**
95+
* This method computes the factorial function
96+
* of a given integer n.
97+
* @param n the argument of the factorial function
98+
* @return the value of the factorial function
99+
*/
100+
public static int factorial(int n){
101+
int result = 1;
102+
for(int i=n;i > 1; i--){
103+
result *= i;
104+
}
105+
return result;
106+
}
107+
108+
/**
109+
* This method calculates e^x
110+
* using an infinite series expansion,
111+
* whose i^th term is: x^i/i!.
112+
* @param x the exponent of the exponential
113+
* @param n the number of terms of the series
114+
* we want to add
115+
* @return the value of the exponential
116+
*/
117+
public static double myexp(double x, int n){
118+
double result = 1;
119+
double temp = 1;
120+
for(int i=1; i <= n; i++){
121+
temp *= x/i;
122+
result += temp;
123+
}
124+
return result;
125+
}
126+
127+
/**
128+
* This method compares the values of the exponential e^x
129+
* computed ith this class' method 'myexp' and
130+
* the ones computed with Math.exp(), showing them
131+
* in different columns.
132+
* @param x the exponent of the function to calculates
133+
*/
134+
public static void check(double x){
135+
System.out.print(x);
136+
System.out.print("\t");
137+
System.out.print(myexp(x,100));
138+
System.out.print("\t");
139+
System.out.print(Math.exp(x));
140+
System.out.println();
141+
}
142+
143+
/**
144+
* This method calculates exp(-x^2),
145+
* given an integer x, using an
146+
* infinite series expansion of Gauss.
147+
* @param x the argument of the exponential
148+
* @param n the number of terms of the series
149+
* @return the value of the exponential
150+
*/
151+
public static double gauss(double x, int n){
152+
double result = 1;
153+
double temp = 1;
154+
for(int i=1; i<=n; i++){
155+
temp *= -x*x/i;
156+
result += temp;
157+
}
158+
return result;
159+
}
160+
83161
}

0 commit comments

Comments
 (0)