Programming Languages Lab 8
Programming Languages Lab 8
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;
}
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
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)
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
26