Академический Документы
Профессиональный Документы
Культура Документы
1
Opening Problem
Find multiple sums of integers:
- from 1 to 10,
- from 20 to 30,
- from 35 to 45,
...
2
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Opening Problem
Repeat code:
int sum = 0;
for (int i = 1; i <= 10; i++)
sum += i;
System.out.println("Sum from 1 to 10 is " + sum);
sum = 0;
for (int i = 20; i <= 30; i++)
sum += i;
System.out.println("Sum from 20 to 30 is " + sum);
sum = 0;
for (int i = 35; i <= 45; i++)
sum += i;
System.out.println("Sum from 35 to 45 is " + sum);
3
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Opening Problem Solution
Use a method!
4
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Why write methods?
To shorten your programs
avoid writing identical code twice or more
To modularize your programs
fully tested methods can be trusted
To make your programs more:
readable
reusable
testable
debuggable
extensible
adaptable
5
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Rule of thumb
If you have to perform some
operation in more than one place
inside your program, make a new
method to implement this
operation and have other parts of
the program use it.
6
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Defining Methods
• A method is a collection of statements that are
grouped together to perform an operation.
Define a method Invoke a method
7
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Method Signature
• Method signature is the combination of the
method name and the parameter list.
Define a method Invoke a method
8
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Formal Parameters
• The variables defined in the method header are
known as formal parameters.
Define a method Invoke a method
9
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Actual Parameters
• When a method is invoked, you pass a value to
the parameter: actual parameter or argument.
Define a method Invoke a method
10
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Return Value Type
• A method may return a value.
• The returnValueType is the data type of the value the method returns.
If the method does not return a value, the returnValueType is the keyword void.
Define a method Invoke a method
11
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Calling Methods
pass the value of i
pass the value of j
12
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Trace Method Invocation
i is now 5
13
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Trace Method Invocation
j is now 2
14
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Trace Method Invocation
invoke max(i, j)
15
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Trace Method Invocation
invoke max(i, j)
Pass the value of i to num1
Pass the value of j to num2
16
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Trace Method Invocation
declare variable result
17
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Trace Method Invocation
(num1 > num2) is true since num1
is 5 and num2 is 2
18
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Trace Method Invocation
result is now 5
19
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Trace Method Invocation
return result, which is 5
20
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Trace Method Invocation
return max(i, j) and assign the
return value to k
21
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Trace Method Invocation
Execute the print statement
22
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Benefits of Methods
Write a method once and reuse it anywhere.
Information hiding:
Hide the implementation from the user.
Reduces complexity.
23
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Method Abstraction
API = the method body is a black box that contains the
detailed implementation for the method.
Optional arguments Optional return
for Input value
Method Header
Black Box
Method body
24
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Call Stacks
Methods are executed using a stack data structure
Space required for Space required for
the max method the max method
result: 5
num2: 2 num2: 2
num1: 5 num1: 5
Space required for Space required for Space required for Space required for
the main method the main method the main method the main method
k: k: k: k: 5 Stack is empty
j: 2 j: 2 j: 2 j: 2
i: 5 i: 5 i: 5 i: 5
(a) The main (b) The max (c) The max method (d) The max method is (e) The main
method is invoked. method is invoked. is being executed. finished and the return method is finished.
value is sent to k.
26
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Trace Call Stack
i: 5
27
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Trace Call Stack
j is declared and initialized
j: 2
i: 5
28
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Trace Call Stack
Declare k
29
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Trace Call Stack
Invoke max(i, j)
30
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Trace Call Stack
pass the values of i and j to num1
and num2
num2: 2
num1: 5
Space required for the
main method
k:
j: 2
i: 5
31
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Trace Call Stack
result:
num2: 2
num1: 5
Space required for the
main method
k:
j: 2
i: 5
32
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Trace Call Stack
result:
num2: 2
num1: 5
Space required for the
main method
k:
j: 2
i: 5
33
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Trace Call Stack
34
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Trace Call Stack
35
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Trace Call Stack
36
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Call-by-value
Method formal arguments are copies of the original
data.
Consequence?
methods cannot assign ( “=” ) new values to
primitive type formal arguments and affect the
original passed variables.
Why?
changing argument values changes the copy, not the
original.
37
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Call-by-value
The values of num1 and num2 are
passed to n1 and n2. Executing swap
does not affect num1 and num2.
The main method The swap method The swap method The main method
is invoked is invoked is finished is finished
38
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Overloading
Method overloading is the ability to create multiple methods of the
same name with different implementations.
// Overload the name max for different invocations
public static int max(int x, int y){
return (x>y) ? x : y;
}
39
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Overloading & Ambiguous Invocation
42
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)
Scope of Local Variables
• A nested block cannot redefine a local variable:
public static void correctMethod() {
int x = 1;
int y = 1;
for (int i = 1; i < 10; i++) {
int x = 0; // error
x += i;
}
}
43
(c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook)