11_Methods -Part 2
11_Methods -Part 2
Modified from:
-W3Schools.com
-Introduction to Java Programming, Liang, 10 th Edition
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
1
Introduction
In this lecture, we will explore the fundamental
concepts and techniques related to methods.
Methods are an essential part of Java
programming, allowing us to organize and reuse
code effectively. Throughout this lecture, we will
delve into topics such as parameters and
arguments, method overloading, scope of
variables, and recursion. By the end of this
session, you will have a solid understanding of
how to create and utilize methods in Java.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
2
Outline
Parameters and Arguments
Method Overloading
Ambiguous Invocation
Scope of Variables
Java Recursion
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
3
Parameters and Arguments
Information can be passed to methods as parameter.
Parameters act as variables inside the method.
Parameters are specified after the method name,
inside the parentheses. You can add as many
parameters as you want, just separate them with a
comma.
The following example has a method that takes
a String called fname as parameter. When the
method is called, we pass along a first name, which
is used inside the method to print the full name.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
4
Parameters and Arguments (cont.)
public class Main {
static void myMethod(String fname) {
System.out.println(fname + " Refsnes");
}
public static void main(String[] args) {
myMethod("Liam");
myMethod("Jenny");
myMethod("Anja");
}
}
When a parameter is passed to the method, it is called an argument. So, from
the example above: fname is a parameter,
while Liam, Jenny and Anja are arguments.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
5
Multiple Parameters
You can have as many parameters as you like. Note that when you are
working with multiple parameters, the method call must have the same
number of arguments as there are parameters, and the arguments must be
passed in the same order.
Example
public class Main {
static void myMethod(String fname, int age) {
System.out.println(fname + " is " + age);
}
public static void main(String[] args) {
myMethod("Liam", 5);
myMethod("Jenny", 8);
myMethod("Anja", 31);
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
6
Multiple Parameters (cont.)
public static void nPrintln(String message, int n) {
for (int i = 0; i < n; i++)
System.out.println(message);
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
7
Example (3)
It is common to use if...else statements inside methods:
public class Main {
static void checkAge(int age) {
if (age < 18) {
System.out.println("Access denied - You are not old enough!");
} else {
System.out.println("Access granted - You are old enough!");
}
}
public static void main(String[] args) {
checkAge(20); // Call the checkAge method and pass along an age of 20
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
8
Benefits of Methods
• Write a method once and reuse it anywhere.
• Information hiding. Hide the implementation
from the user.
• Reduce complexity.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
9
Overloading Methods
With method overloading, multiple methods
can have the same name with different
parameters:
Example:
int myMethod(int x)
float myMethod(float x)
double myMethod(double x, double y)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
10
Overloading Methods
Consider the following example, which have two methods that add
numbers of different type:
static int plusMethodInt(int x, int y) {
return x + y;
}
static double plusMethodDouble(double x, double y) {
return x + y;
}
public static void main(String[] args) {
int myNum1 = plusMethodInt(8, 5);
double myNum2 = plusMethodDouble(4.3, 6.26);
System.out.println("int: " + myNum1);
System.out.println("double: " + myNum2);
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
11
Overloading Methods (cont.
Instead of defining two methods that should do the same thing, it is better to
overload one.
static int plusMethod(int x, int y) {
return x + y;
}
static double plusMethod(double x, double y) {
return x + y;
}
public static void main(String[] args) {
int myNum1 = plusMethod(8, 5);
double myNum2 = plusMethod(4.3, 6.26);
System.out.println("int: " + myNum1);
System.out.println("double: " + myNum2);
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
12
Ambiguous Invocation
Multiple methods can have the same name as
long as the number and/or type of parameters are
different..
Sometimes there may be two or more possible
matches for an invocation of a method, but the
compiler cannot determine the most specific
match. This is referred to as ambiguous
invocation. Ambiguous invocation is a compile
error.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
13
Ambiguous Invocation
public class AmbiguousOverloading {
public static void main(String[] args) {
System.out.println(max(1, 2));
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
15
Method Scope
Variables declared directly inside a method are available anywhere in the
method following the line of code in which they were declared: Example:
public class Main {
public static void main(String[] args) {
// Code here CANNOT use x
int x = 100;
// Code here can use x
System.out.println(x);
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
16
Block Scope
A block of code refers to all of the code between
curly braces {}. Variables declared inside blocks
of code are only accessible by the code between
the curly braces, which follows the line in which
the variable was declared.
A block of code may exist on its own or it can
belong to an if, while or for statement. In the case
of for statements, variables declared in the
statement itself are also available inside the
block's scope.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
17
Block Scope Example
public class Main {
public static void main(String[] args) {
// Code here CANNOT use x
{ // This is a block
// Code here CANNOT use x
int x = 100;
// Code here CAN use x
System.out.println(x);
} // The block ends here
// Code here CANNOT use x
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
18
Java Recursion
Recursion is the technique of making a
function call itself. This technique provides
a way to break complicated problems down
into simple problems which are easier to
solve.
Recursion may be a bit difficult to
understand. The best way to figure out how
it works is to experiment with it.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
19
Example
Use recursion to add all of the numbers up to 10.
public class Main {
public static void main(String[] args) {
int result = sum(10);
System.out.println(result);
}
public static int sum(int k) {
if (k > 0) {
return k + sum(k - 1);
} else {
return 0;
}
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
20
Example Explained
When the sum() function is called, it adds
parameter k to the sum of all numbers smaller
than k and returns the result. When k becomes 0, the
function just returns 0. When running, the program
follows these steps:
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
21
Halting Condition
Just as loops can run into the problem of infinite looping,
recursive functions can run into the problem of infinite
recursion. Infinite recursion is when the function never
stops calling itself. Every recursive function should have a
halting condition, which is the condition where the
function stops calling itself. In the previous example, the
halting condition is when the parameter k becomes 0.
It is helpful to see a variety of different examples to better
understand the concept. In this example, the function adds
a range of numbers between a start and an end. The halting
condition for this recursive function is when end is not
greater than start:
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
22
Example
Use recursion to add all of the numbers between 5 to 10.
public class Main {
public static void main(String[] args) {
int result = sum(5, 10);
System.out.println(result);
}
public static int sum(int start, int end) {
if (end > start) {
return end + sum(start, end - 1);
} else {
return end;
}
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
23
References
Y. Daniel Liang, 2019, Intro to Java
Programming, Comprehensive Version, Student
Value Edition 12th Edition. Pearson, ISBN-10 :
0136520154 ISBN-13: 978-0136520153.
Introduction to Java,
https://www.w3schools.com/java/java_intro.asp,
Last Updated 2024, Last Accessed March 2024
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
24