Method
Method
Introduction to Methods
2
Types of Methods
1. Pre-defined/ built-in method:
These methods are already defined in java libraries
e.g: .length(), .equals(), .compareTo(), .toLowerCase() etc.
2. User-defined method:
These are the methods written by the programmer.
3
User-Defined Methods
There are mainly 2 parts of using a user-defined method:
1. Method declaration
2. Method calling
Method Declaration can be further broken down into more parts.
4
Static Method
● A static method belongs to the class and not to the instance of a class.
● As it belongs to the class, it can be called without creating any object.
● We use the keyword “static” to define a method as static.
● We can call a static method from inside any other static method.
5
Method Declaration
➢ A method in java is written outside the main() method but inside the class.
➢ The following convention is followed when declaring a method:
6
Method Declaration Cont.
Declaring a method that adds two numbers received as parameters.
7
Method Body
● Any set of code lines written inside the method.
● Any variable declared inside the scope of a method is not accessible from
outside including the parameter variables.
● No line is executed after the “return” statement.
8
Return Type
9
1 void return 2 int return 3 boolean return
static void print_sum( int a, int b) { static int sum( int a, int b) { static boolean check( int a) {
10
Method Call
11
Method Call Cont.
Calling the previously written add method.
>
12
Method Arguments & Parameters
The sequence of the arguments passed corresponds to the variables receiving
the parameters.
static void subtract( int a, int b) { subtract(2, 3); Welcome to DrJava. Working directory is E:\
Java
System.out.println(diff); -1
value. >
Here in case 1: subtract(2,3), the values 2 and 3 will be received as int a = 2 and int b = 3. Therefore, the difference is (2-3) = -1.
14
Method Example 1
Writing a method that prints “Hello!”
sayHello(); Hello!
No arguments
} >
and no
} parameters
received
15
Method Example 2
Writing a method that checks if a number divisible by three or not.
public class func1{
public static String divThree(int num){
if(num%3==0){
Output
return “Divisible by 3”;
} Welcome to DrJava. Working directory is E:\
Java
else{
> run func1
return “Not divisible by 3”;
Not divisible by 3
} 13 will be received
This value will be >
by num. And num
} returned to the line of
= 13 will be
method call
public static void main(String []args){ accessible inside
the function.
String answer = divThree(13);
System.out.println(answer);
This line will invoke a
}
method call.
}
16
Method Example 3
Writing a method that prints the largest number out of two numbers.
public class func1{
public static void findLargest(int num1, int num2){
if(num1>num2){ Output
System.out.println(num1);
Welcome to DrJava. Working directory is E:\
} Java
System.out.println(num2); 13
} >
}
public static void main(String []args){
findLargest(13, -10);
num1 = 13 num2 = -10
void return
}
since no return
}
statement.
17
To do
Write a method named evenPositive to find even and positive
numbers from an array of numbers.
18
Calling a Method from another Method
● We can call a method from inside another public class Methods {
public static void main(String[] args) {
method. method1();
1. method1() called from the main() method. }
static void method1(){
2. method2() called from method1(). System.out.print("CSE");
method2();
}
static void method2(){
System.out.print("110");
}
}
19
To Do public class MethodCalling {
public static void main(String[] args) {
show_triangle(4);
Now, can you print a triangular palindrome like the System.out.println();
show_triangle(5);
following using a method called show_triangle()?
}
Note that you have to take the help of static void show_spaces(int a){
show_spaces() and show_palindrome() method. for (int i = 0; i<a; i++){
System.out.print(" ");
1 }
121 }
12321 static void show_palindrome(int b) {
1234321 for (int i = 1; i <= b; i++) {
123454321 System.out.print(i);
}
● show_spaces() method prints a number of for (int i = b - 1; i >= 1; i—-)
{
spaces passed to its parameter System.out.print(i);
● show_palindrome() prints a palindrome. }
}
These methods are already done for you. static void show_triangle(int a){
//To Do
}
} 20
Practice Problem
● Write a Java method to find the smallest number among three numbers.
● Write a Java method to count all vowels in a string.
● Write a Java method to count all the words in a string.
● Write a Java method that accepts three integers and checks whether they are consecutive or
not. Returns true or false.
● Write a Java method for extracting the first digit from a positive or negative integer.
● [Hard] A nonnegative integer is called a palindrome if it reads forward and backward in the
same way. For example, the numbers 5, 121, 3443, and 123454321 are palindromes. Write
a method that takes as input a nonnegative integer and returns true if the number is a
palindrome; otherwise, it returns false. Also write a program to test your method.