Professional Documents
Culture Documents
Introducing Methods
A method is a collection of statements that are
grouped together to perform an operation.
Define a method Invoke a method
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;
}
Introducing Methods……
• Method signature is the combination of the method
name and the parameter list.
• The variables defined in the method header are known
as formal parameters or simply parameter.
• You need to declare a separate data type for each
parameter. For instance, int num1,num2 should be
replaced by int num1, int num2.
• When a method is invoked, you pass a value to the
parameter. This value is referred to as actual parameter
or argument.
Introducing Methods……
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;
}
Trace Method Invocation
i is now 5
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
Trace Method Invocation….
j is now 2
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
Trace Method Invocation….
invoke max(i, 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;
}
10
Trace Method Invocation….
invoke max(i, j)
Pass the value of i to num1
Pass the value of j to num2
public static void main(String[] args) public static int max(int num1, int num2) {
{ int result;
int i = 5;
int j = 2; if (num1 > num2)
int k = max(i, j); result = num1;
else
System.out.println( result = num2;
"The maximum between " + i +
" and " + j + " is " + k); return result;
} }
11
Trace Method Invocation….
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
animation
Trace Method Invocation….
Trace Method Invocation
(num1 > num2) is true since
num1 is 5 and num2 is 2
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
animation
Trace Method Invocation….
Trace Method Invocation
result is now 5
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
Trace Method Invocation….
Trace Method Invocation
return result, which is 5
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
animation
Trace Method Invocation….
Trace Method Invocation
return max(i, j) and assign
the return value to k
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
animation
Trace Method Invocation….
Trace Method Invocation
Execute the print
statement
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;
}
17
Reuse Methods from Other Classes
One of the benefits of methods is for reuse.
If you create a new class Test, you can invoke the
max method using ClassName.methodName(…)
(because it is declared static).
Example:
• Math.sqrt(900.0)
• TestMax.max
Reuse Methods from Other Classes
The main method The max method is The max method is The main method
is invoked. invoked. finished and the return is finished.
value is sent to k.
20
animation
return result;
}
21
animation
return result;
}
22
animation
Declare k
return result;
}
23
animation
Invoke max(i, j)
return result;
}
24
Trace Call Stack
25
animation
Trace Call Stack
26
animation
Trace Call Stack
27
animation
Trace Call Stack
28
animation
Trace Call Stack
29
animation
return result;
}
30
Passing Parameters
There are two ways to pass a variable to the called
function: Pass by value and pass by reference
Example (C++ code)
Passing Parameters…
Pass by value (how it
works)
•The contents of
memory of i and j
don't change
Class constants:
• PI
• E (base of natural
logarithm)
Class methods:
• Trigonometric Methods
• Exponent Methods
• Rounding Methods
• min, max, abs, and random Methods
Trigonometric Methods
public static double sin(double redians)
public static double cos(double redians)
public static double tan(double redians)
public static double asin(double redians)
public static double acos(double redians)
public static double atan(double redians)
public static double toRadians(double degree)
public static double toDegrees(double degree)
Trigonometric Methods …….
Examples:
Examples:
In general,
int[] num;
num = new int[10];
Creating Arrays….
myList reference
myList[0] 5.6
myList[1] 4.5
Array reference myList[2] 3.3
variable
myList[3] 13.2
myList[4] 4
Array element at
myList[5] 34.33 Element value
index 5
myList[6] 34
int sum = 0;
for (int i = 0; i < a.length; i++)
sum += a[i];
System.out.println(“ Total = “ + sum);
Accessing Array Element…
If we only need to reference the value of each of the elements, you
can use the somewhat simpler enhanced for loop (also known as
the foreach loop), which traverse the complete array sequentially
without using an index variable.
The foreach loop only gets the values, so it couldn't have been
used to set the values in the first loop above.
Example
list1 list1
Contents Contents
of list1 of list1
list2 list2
Contents Contents
of list2 of list2
Garbage
59
Copying Arrays …..
There are three ways to copy arrays:
Use a loop to copy individual elements on by one
Use the static arraycopy method in the System class
Use the clone method to copy arrays
Copying Arrays ….
Using a loop:
61
Copying Arrays ….
62
Copying Arrays ….
Example:
System.arraycopy(sourceArray, 0, targetArray, 0,
sourceArray.length);
Anonymous array
64
Anonymous Array
The statement
printArray(new int[]{3, 1, 2, 6, 4, 2});
65
Pass By Value
66
Pass By Value
67
Simple Example
public class Test {
public static void main(String[] args) {
int x = 1; // x represents an int value
int[] y = new int[10]; // y represents an array of int values
m(x, y); // Invoke m with arguments x and y
System.out.println("x is " + x);
System.out.println("y[0] is " + y[0]);
}
public static void m(int number, int[] numbers) {
number = 1001; // Assign a new value to number
numbers[0] = 5555; // Assign a new value to numbers[0]
}
}
68
Pass By Value
69
Returning an Array from a Method
public static int[] reverse(int[] list) {
int[] result = new int[list.length];
for (int i = 0, j = result.length - 1;
i < list.length; i++, j--) {
result[j] = list[i];
}
return result;
}
list
result
70
animation
list 1 2 3 4 5 6
result 0 0 0 0 0 0
71
animation
list 1 2 3 4 5 6
result 0 0 0 0 0 0
72
animation
list 1 2 3 4 5 6
result 0 0 0 0 0 0
73
animation
list 1 2 3 4 5 6
result 0 0 0 0 0 1
74
animation
list 1 2 3 4 5 6
result 0 0 0 0 0 1
75
animation
list 1 2 3 4 5 6
result 0 0 0 0 0 1
76
animation
list 1 2 3 4 5 6
result 0 0 0 0 2 1
77
animation
list 1 2 3 4 5 6
result 0 0 0 0 2 1
78
animation
list 1 2 3 4 5 6
result 0 0 0 0 2 1
79
animation
list 1 2 3 4 5 6
result 0 0 0 3 2 1
80
animation
list 1 2 3 4 5 6
result 0 0 0 3 2 1
81
animation
list 1 2 3 4 5 6
result 0 0 0 3 2 1
82
animation
list 1 2 3 4 5 6
result 0 0 4 3 2 1
83
animation
list 1 2 3 4 5 6
result 0 0 4 3 2 1
84
animation
list 1 2 3 4 5 6
result 0 0 4 3 2 1
85
animation
list 1 2 3 4 5 6
result 0 5 4 3 2 1
86
animation
list 1 2 3 4 5 6
result 0 5 4 3 2 1
87
animation
list 1 2 3 4 5 6
result 0 5 4 3 2 1
88
animation
list 1 2 3 4 5 6
result 6 5 4 3 2 1
89
animation
list 1 2 3 4 5 6
result 6 5 4 3 2 1
90
animation
list 1 2 3 4 5 6
result 6 5 4 3 2 1
91
animation
list 1 2 3 4 5 6
list2
result 6 5 4 3 2 1
92
Multi-Dimensional Array
Multidimensional arrays are implemented as arrays of arrays.
Multidimensional arrays are declared by appending the
appropriate number of bracket pairs after the array name.
Example
0 1 2 3 4 0 1 2 3 4 0 1 2
0 0 0 1 2 3
1 1 1 4 5 6
2 2 7 2 7 8 9
3 3 3 10 11 12
4 4 int[][] array = {
{1, 2, 3},
matrix = new int[5][5]; matrix[2][1] = 7; {4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
Multi-Dimensional Array…
To access an element in a multidimensional array is
just the same as accessing the elements in a one
dimensional array.
For example, to access the first element in the first row
of the array dogs, we write,
System.out.print( dogs[0][0] );
This will print the String "terry" on the screen.
Use nested loops to access each and every element of
multidimensional arrays
Jagged/Ragged Arrays
Multidimensional arrays with equal number of row
slots are called rectangular arrays
int[][] triangleArray = { 1 2 3 4 5
{1, 2, 3, 4, 5},
{2, 3, 4, 5}, 1 2 3 4
{3, 4, 5},
{4, 5}, 1 2 3
{5}
}; 1 2
1 2
98
Ragged Arrays, cont.
What is the output of the following code?