Professional Documents
Culture Documents
04 Slide
04 Slide
Introducing Methods
– Benefits of methods, Declaring Methods, and Calling Methods
Passing Parameters
– Pass by Value
Overloading Methods
– Ambiguous Invocation
Scope of Local Variables
Method Abstraction
The Math Class
Case Studies
Recursion (Optional)
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
Declaring Methods
TestMax Run
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.
public static int xMethod(int n) {
if (n > 0) return 1;
else if (n == 0) return 0;
else if (n < 0) return –1;
}
To fix this problem, delete if (n<0) in the code.
Passing Parameters
public static void nPrintln(String message,
int n) {
for (int i = 0; i < n; i++)
System.out.println(message);
}
Pass by Value
TestPassByValue Run
Pass by Value, cont.
Invoke swap The values of num1 and
num1 1
num2 are passed to n1 and
swap(num1, num2) n2. Executing swap does not
num2 2
affect num1 and num2.
Pass by value
n2 2 n2 1
temp 1
TestMethodOverloading Run
Ambiguous Invocation
Sometimes there may be two or more
possible matches for an invocation of a
method, but the compiler cannot determine
the most specific match. This is referred to
as ambiguous invocation. Ambiguous
invocation is a compilation error.
Ambiguous Invocation
public class AmbiguousOverloading {
public static void main(String[] args) {
System.out.println(max(1, 2));
}
Method Signature
Method body
Black Box
Benefits of Methods
xi
n
n ( xi ) 2
mean i 1 x 2
i i 1
n
n deviation i 1
n 1
ComputeMeanDeviation Run
Example 4.5 Obtaining Random
Characters
Write the methods for generating random
characters. The program uses these methods to
generate 175 random characters between ‘!' and ‘~'
and displays 25 characters per line. To find out the
characters between ‘!' and ‘~', see Appendix B,
“The ASCII Character Set.”
RandomCharacter Run
Example 4.5 Obtaining Random
Characters, cont.
Appendix B: ASCII Character Set
Case Studies
PrintCalendar Run
Design Diagram
printCalendar
(main)
readInput printMonth
getTotalNumOfDays getMonthName
getNumOfDaysInMonth
isLeapYear
Recursion (Optional)
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
ComputeFactorial Run
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 7 Space Required
for factorial(2) 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
Fibonacci Numbers, cont
ComputeFibonacci Run
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
TowersOfHanoi Run
Towers of Hanoi, cont.
A B C A B C
A B C A B C
A B C A B C
A B C A B C