Programming
Languages
Section #9
Methods
Opening Problem
Find the sum of integers from 1 to 10, from 20 to 30, and
from 35 to 45, respectively.
Problem
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);
Solution
public static int sum(int i1, int i2) {
int sum = 0;
for (int i = i1; i <= i2; i++)
sum += i;
return sum;
}
public static void main(String[] args) {
System.out.println("Sum from 1 to 10 is " + sum(1, 10));
System.out.println("Sum from 20 to 30 is " + sum(20, 30));
System.out.println("Sum from 35 to 45 is " + sum(35, 45));
}
Defining Methods
A method is a collection of statements that are
grouped together to perform an operation.
Define a method Invoke a method
int z = max(x, y);
public static int max(int num1, int num2) {
actual parameters
int result; (arguments)
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Defining Methods
A method is a collection of statements that are
grouped together to perform an operation.
Define a method Invoke a method
return value method formal
modifier type name parameters
int z = max(x, y);
method
public static int max(int num1, int num2) {
header
actual parameters
int result; (arguments)
method
body parameter list
if (num1 > num2)
result = num1;
else
method
result = num2; signature
return result; return value
}
Method Signature
Method signature is the combination of the method name and the
parameter list.
Define a method Invoke a method
return value method formal
modifier type name parameters
int z = max(x, y);
method
public static int max(int num1, int num2) {
header
actual parameters
int result; (arguments)
method
body parameter list
if (num1 > num2)
result = num1;
else
method
result = num2; signature
return result; return value
}
Formal Parameters
The variables defined in the method header are known as
formal parameters.
Define a method Invoke a method
return value method formal
modifier type name parameters
int z = max(x, y);
method
public static int max(int num1, int num2) {
header
actual parameters
int result; (arguments)
method
body parameter list
if (num1 > num2)
result = num1;
else
method
result = num2; signature
return result; return value
}
Actual Parameters
When a method is invoked, you pass a value to the parameter. This
value is referred to as actual parameter or argument.
Define a method Invoke a method
return value method formal
modifier type name parameters
int z = max(x, y);
method
public static int max(int num1, int num2) {
header
actual parameters
int result; (arguments)
method
body parameter list
if (num1 > num2)
result = num1;
else
method
result = num2; signature
return result; return value
}
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. For example, the
returnValueType in the main method is void.
Define a method Invoke a method
return value method formal
modifier type name parameters
int z = max(x, y);
method
public static int max(int num1, int num2) {
header
actual parameters
int result; (arguments)
method
body parameter list
if (num1 > num2)
result = num1;
else
method
result = num2; signature
return result; return value
}
Calling Methods, cont.
pass the value of i
pass the value of 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;
}
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;
}
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;
}
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;
}
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 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
declare variable result
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
(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;
}
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;
}
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;
}
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;
}
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;
}
Example#1
Write a method that receives three integer parameters and returns the
maximum number between them.
Method Exercise
1- Write a Java method to check whether a year (integer) entered by the user is a leap year or not.
(the year is leap if the year is divisible by 4 but not 100 or is divisible by 400)
2- Write a Java method to compute the average of three numbers.
3- Write a Java method that accepts three integers and returns true if one of them is the middle point
between the other two integers, otherwise false. Go to the editor
– Expected Output:
– Input the first number: 2
– Input the second number: 4
– Input the third number: 6
– Check whether the three said numbers has a midpoint!
– true
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
24
Method Exercise #1 solution
1- Write a Java method to check whether a year (integer) entered by the user is a leap year or not. (the year
is leap if the year is divisible by 4 but not 100 or is divisible by 400)
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a year");
int year = scan.nextInt();
if (isLeap (year)) {
System.out.println("Specified year is a leap year");
} else {
System.out.println("Specified year is not a leap year");
}
}
public static boolean isLeap (int year) {
boolean leap = false;
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
leap = true;
}
return leap;
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
25
Method Exercise #2 Solution
2- Write a Java method to compute the average of three numbers.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter three numbers");
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
System.out.println (average (a, b, c));
}
public static double average (int numl, int num2,
int num3) {
return (num1+num2 + num3) / 3.0;
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
26