Chapter 4 - Methods
Chapter 4 - Methods
[email protected]
Mantra:
Coding is fun!
Introducing Methods
Method Structure
A method is a
collection of
statements that are
grouped together
to perform an
operation.
Introducing Methods, cont.
•parameter profile refers to the type, order,
and number of the parameters of a method.
•method signature is the combination of the
method name and the parameter profiles.
•The parameters defined in the method header
are known as formal parameters.
•When a method is invoked, its formal
parameters are replaced by variables or data,
which are referred to as actual parameters.
Declaring Methods
public static int max(int num1, int num2) {
if (num1 > num2)
return num1;
else
return num2;
}
Calling Methods, cont.
pass i
pass j
public static void main(String[] args) { public static int max(int num1, int num2) {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}
Calling Methods, cont.
i: 5 num1: 5
pass 2
parameters
j: 2 num2: 2
k: 5 result: 5
CAUTION
A return statement is required for a nonvoid method. The following
method is logically correct, but it has a compilation error, because the
Java compiler thinks it possible that this method does not return any
value.
Pass by value
n2 2 n2 1
temp 1
// With no errors
public static void incorrectMethod() {
int x = 1;
int y = 1;
for (int i = 1; i < 10; i++) {
int x = 0;
x += i;
}
}
Method Abstraction
You can think of the method body as a black box that contains the
detailed implementation for the method.
Method Signature
Method body
Black Box
Benefits of Methods
• Class methods:
• Trigonometric Methods
• Exponent Methods
• Rounding Methods
• min, max, abs, and random Methods
Trigonometric Methods
• sin(double a)
• cos(double a)
• tan(double a)
• acos(double a)
• asin(double a)
• atan(double a)
Exponent Methods
• exp(double a)
Returns e raised to the power of a.
• log(double a)
Returns the natural logarithm of a.
• pow(double a, double b)
Returns a raised to the power of b.
• sqrt(double a)
Returns the square root of a.
Rounding Methods
• double ceil(double x)
x rounded up to its nearest integer. This
integer is returned as a double value.
• double floor(double x)
x is rounded down to its nearest integer. This
integer is returned as a double value.
• double rint(double x)
x is rounded to its nearest integer. If x is
equally close to two integers, the even one is
returned as a double.
• int round(float x)
Return (int)Math.floor(x+0.5).
• long round(double x)
Return (long)Math.floor(x+0.5).
min, max, abs, and random
• max(a, b)and min(a, b)
Returns the maximum or minimum of two parameters.
• abs(a)
Returns the absolute value of the parameter.
• random()
Returns a random double value
in the range [0.0, 1.0).
Excercise 4.4 Computing Mean and Standard
Deviation
Generate 10 random numbers and compute the mean and standard
deviation
n n
xi n ( xi ) 2
mean i 1 x 2
i i 1
n
n deviation i 1
n 1
Recursion (Optional)
Example 4.7 Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
Example 4.7 Computing Factorial, cont.
main method:
factorial(4)
Step 9: factorial(4) returns 24 (4*6)
factorial(4) is called in the main
factorial(4) = 4*factorial(3)
Step 8: factorial(3) returns 6 (3*2)
Step 1: factorial(4) calls factorial(3)
factorial(3) = 3*factorial(2)
Step 7: factorial(2) returns 2 (2*1)
Step 2: factorial(3) calls factorial(2)
factorial(2) = 2*factorial(1)
Step 6: factorial(1) returns 1 (1*1)
Step 3: factorial(2) calls factorial(1)
factorial(1) = 1*factorial(0)
Step 5: factorial(0) returns 1
Step 4: factorial(1) calls factorial(0)
factorial(0) = 1
Example 4.7 Computing Factorial, cont.
5 Space Required
for factorial(0)
4 Space Required
for factorial(1)
Space Required
for factorial(1)
3 Space Required
for factorial(2)
Space Required
for factorial(2)
Space Required
for factorial(2)
2 Space Required
for factorial(3)
Space Required
for factorial(3)
Space Required
for factorial(3)
Space Required
for factorial(3)
1 Space Required
for factorial(4)
Space Required
for factorial(4)
Space Required
for factorial(4)
Space Required
for factorial(4)
Space Required
for factorial(4)
6 Space Required
for factorial(1)
Space Required
for factorial(2)
7 Space Required
for factorial(2)
Space Required Space Required 8 Space Required
for factorial(3) for factorial(3) for factorial(3)
Space Required Space Required Space Required 9 Space Required
for factorial(4) for factorial(4) for factorial(4) for factorial(4)
Fibonacci Numbers
Example 4.8 Computing Finonacci Numbers
0 1 1 2 3 5 8 13 21 34 55 89…
f0 f1
fib(2) = fib(0) + fib(1);
fib(0) = 0;
fib(1) = 1;
fib(n) = fib(n-2) + fib(n-1); n>=2
Fibonnaci Numbers, cont.
1 fib(4)=
fib(3) + fib(2)
call fib(3)
return fib(3)
2 fib(3)=
7 fib(2)=
fib(2) + fib(1)
call fib(2) fib(1) + fib(0)
return fib(2)
return fib(1)
3 fib(2)=
6 fib(1)= 8 fib(1)= 9 fib(0)=
fib(1) + fib(0)
call fib(1) 1 1 1
return fib(1)
4 fib(1)= 5 fib(0)=
1 0
Towers of Hanoi
Example 4.9 Solving the Towers of Hanoi Problem
A B C A B C
A B C A B C
A B C A B C
A B C A B C