Chapter 1
Chapter 1
Chapter 1
Methods
method
public static int max(int num1, int num2) { int z = max(x, y);
header
int result;
method actual parameters
body parameter list (arguments)
if (num1 > num2)
result = num1;
else
result = num2;
return value
return result;
}
3
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
Introducing Methods, cont.
4
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
Introducing Methods, cont.
5
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
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;
}
6
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
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;
}
7
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
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;
}
8
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
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;
}
9
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
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;
}
10
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
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;
}
11
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
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;
}
12
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
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;
}
13
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
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
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
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;
}
15
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
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;
}
16
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
Passing Parameters
public static void nPrintln(String message, int n) {
for (int i = 0; i < n; i++)
System.out.println(message);
}
19
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
Scope of Local Variables, cont.
• A variable declared in the initial action part of a for loop
header has its scope in the entire loop.
• But a variable declared inside a for loop body has its scope
limited in the loop body from its declaration and to the end of
the block that contains the variable.
public static void method1() {
.
.
for (int i = 1; i < 10; i++) {
.
The scope of i .
int j;
.
The scope of j .
.
}
}
20
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
Scope of Local Variables, cont.
21
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
Scope of Local Variables, cont.
22
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
Overloading Methods
You can create more than methods in the same name if one of the
following conditions is met:
23
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
change datatype of parameters
24
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
Rearrange datatype of parameters
25
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
change number of parameters
26
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
Overloading Methods
Output: Output:
First method Second method
27
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
Ambiguous Invocation
28
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
Ambiguous Invocation
29
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
Overloading Methods
30
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
The Math Class
31
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
Create the following methods to calculate area of (Triangle and Rectangle):
– When user chooses one, prompt him to enter base and height for triangle
– When user chooses two, prompt him to enter length and width for rectangle
– When user chooses other numbers, display this message “Wrong Choice”
32
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
33
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز
Benefits of Methods
• Reduce complexity.
34
KING ABDULAZIZ UNIVERSITY جامعة الملك عبد العزيز