lecture_6_Method.ppt
lecture_6_Method.ppt
1
Opening Problem
2
A Solution
int sum = 0;
for (int i = 1; i <= 10; i++)
sum = sum + i;
System.out.println("Sum from 1 to 10 is " + sum);
sum = 0;
for (int i = 20; i <= 30; i++)
sum = sum + i;
System.out.println("Sum from 20 to 30 is " + sum);
sum = 0;
for (int i = 35; i <= 45; i++)
sum = sum + i;
System.out.println("Sum from 35 to 45 is " + sum);
3
Repeated Code
int sum = 0;
for (int i = 1; i <= 10; i++)
sum = sum + i;
System.out.println("Sum from 1 to 10 is " + sum);
sum = 0;
for (int i = 20; i <= 30; i++)
sum = sum + i;
System.out.println("Sum from 20 to 30 is " + sum);
sum = 0;
for (int i = 35; i <= 45; i++)
sum = sum + i;
System.out.println("Sum from 35 to 45 is " + sum);
4
Solution Using Method sum
public class sumMethod {
public static void main(String[] args)
{
int result = sum(1, 10);
System.out.println("Sum from 1 to 10 is:\t" + result);
6
Benefits of Methods
• Write a method once and reuse it anywhere
• Promotes Information hiding (hide the
implementation from the user)
• Facilitate modularity (break the code into
manageable modules)
• Reduce code complexity (better maintenance)
7
Defining Methods
A method has a header and a body.
=> The header is the method declaration.
=> The body is a a collection of statements grouped together to
perform an operation.
8
Method Signature
Method signature is the combination of the method name
and the parameter list.
9
Formal Parameters
The variables defined in the method header are known as
formal parameters.
10
Actual Parameters
When a method is invoked, you pass a value to the
parameter. This value is referred to as actual parameter or
argument.
11
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.
12
Calling Methods
Testing method max
This program demonstrates calling method max to
return the largest of two int values.
13
animation
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;
}
14
animation
15
animation
16
animation
17
animation
18
animation
19
animation
20
animation
21
animation
22
animation
23
animation
24
Program TestMax
//class TestMax
public class TestMax
{
public static void main(String[] args) // main method
{
int i = 5;
int j = 2;
int k = max(i, j);
System.out.println("The maximum of " + i + " and " + j + " is " + k);
}
return result;
}
}
25
CAUTION
A return statement is required for a value-returning method. The
method shown below in (a) is logically correct, but it has a
compilation error because the Java compiler thinks it is possible
that this method does not return any value.
To fix this problem, delete if (n < 0) in (a), so that the compiler will
see a return statement to be reached regardless of how the if
statement is evaluated.
26
Reuse Methods from Other Classes
27
Another Example
// illustration of methods in java
import java.util.*;
public class TestMethods {
public static void main (String[] arge)
{ int a = 10, b = 20;
int addResult = Add(a,b); //method call Add
System.out.println("Sum of a and b is " + addResult);
// method definition
public static int Add(int x, int y)
{ return (x+y); }
// method definition
public static void printMessage(String message)
{
for (int i = 1; i <= 5; i++)
System.out.println(message);
}
}
28
Runtime Stack
A runtime stack is a structure used to keep track of
active (currently running) methods in the program,
and order of method calls.
Each active method has "activation record" on the
stack. The record is the memory space for all local
variables in the method.
The top activation record on the stack represents the
currently running (active) method in the program.
The bottom activation record represents the main
method often program.
Once a method is no longer active, it is removed from
the stack (always the top record is removed).
29
animation
30
animation
31
animation
32
animation
33
animation
34
animation
35
animation
36
animation
37
animation
38
animation
39
Call Stacks
40
void Method
This type of method does not return a value. The method
performs some actions.
43
Pass by Value Example
public class TestPassByValue {
public static void main (String[] arge){
int num1 = 1;
int num2 = 2;
System.out.println("Before calling Swap: num1 = " + num1 +
" num2 = " + num1 + "\n");
swap(num1, num2);
System.out.println("After calling Swap: num1 = " + num1 +
" num2 = " + num2 + "\n");
}
44
Pass by Value Runtime Stack
45
Modularizing Code
Modularization is software design concept that calls for
writing code in modules.
48
Ambiguous Invocation
public class AmbiguousOverloading {
public static void main(String[] args) {
System.out.println(max(1,2)); //Error
}
50
Scope of Local Variables, cont.
51
Scope of Local Variables, cont.
52
Scope of Local Variables, cont.
// Homework: code with errors, can you find them?
public static void incorrectMethod()
{
int x = 1;
int y = 1;
for (int i = 1; i < 10; i++)
{
int x = 0;
int t = 0;
x = x + i;
}
i = i + 10;
y = y + 10;
t = t + 10;
}
53