Lecture 7 Methods in Java
Lecture 7 Methods in Java
Fundamentals of
Programming (Java)
Instructor
Dr. Muhammad Waqar
[email protected]
2
Methods
• Methods can be used to define reusable code and organize
and simplify coding. Tests
• A method is a collection of statements grouped together to
perform an operation.
• Previously you have used predefined methods such as
System.out.println, Math.pow, and Math.random. These
methods are defined in the Java library.
3
Defining Methods
• A method definition consists of its method name,
parameters, return value type, and body.
Tests
• The syntax for defining a method is as follows:
Example of a method
Tests
6
Calling a method
• Calling a method executes the code in the method.
• In a method definition, you define what the method is to do.
Tests
• To execute the method, you have to call or invoke it.
• There are two ways to call a method, depending on whether
the method
returns a value or not.
• If a method returns a value, a call to the method is usually
treated as a value. For example,
int larger = max(3, 4);
calls max(3, 4) and assigns the result of the method to the
variable larger
• If a method returns void, a call to the method must be a
statement. For example, the method println returns void.
7
Calling a method
• When a program calls a method, program control is
transferred to the called method.
• A called method returns control Tests
to the caller when its return
statement is executed or when its method ending closing
brace is reached.
• Methods enable code sharing and reuse. The max method
can be invoked from any class.
• If you call a method in the same class where it has been
defined, it can be used directly by name e.g. max(i,j)
• If you create a new class, you can invoke the max method
using ClassName.methodName (i.e., TestMax.max(i,j)).
8
Example
public class TestMax {
public static void main(String[] args) {
int i = 5;
Tests
int j = 2;
int k = max(i, j);
System.out.println("The maximum of " + i +
" and " + j + " is " + k);
}
Practice Problem
• Write a program which defines two methods, max and min.
Method Overloading
Method Overloading
public static double max(double num1, double num2) {
if (num1 > num2)
return num1; Tests
else
return num2;}
• If you call max with int parameters, the max method that
expects int parameters will be invoked;
• if you call max with double parameters, the max method
that expects double parameters will be invoked.
• This is referred to as method overloading; that is, two
methods have the same name but different parameter lists
within one class.
• The Java compiler determines which method to use based on
the method signature.
19
Practice Problem I
Write a program which defines a method to calculate
multiplication of two numbers e.g. multiplyInteger.
Tests
Call this method in the main method and calculate
multiplication of two numbers.
22
Practice Problem II
Write a program which defines three overloaded methods to
calculate multiplication of numbers.
Tests
First method should accept two values and should return
multiplication of two numbers.
Second and third method should have three and four numbers
and should give the multiplication result at output respectively.
23