Java for Selenium Part 8

User defined Methods in Java

i) Introduction to Java Methods

ii) Types of Methods in Java

iii) User defined Methods
--------------------------------------
i) Introduction to Java Methods

What is Method?

A Java method is a set of statements that are grouped together to perform an operation.

Methods are also known as Functions

In Structured Programming (Ex: C Language) we use Functions (Built-in, User defined).

In Object Oriented Programming (Ex: Java Language) we use Methods (Built-in, User defined).
--------------------------------------
When we choose Methods?

Whenever we want perform any operation multiple times then we choose Methods.

All Programming concepts can be used in Methods.

Advantages of Methods

Code Reusability, using methods we can reduce the project code size.

Code Maintenance is easy.
--------------------------------------
ii) Types of Methods in Java

1) Built in/Pre-defined Methods

2) User defined Methods
--------------------------------------
1) Built in/Pre-defined Methods

Java has a library of classes and methods organized in packages.

Ex:

import java.io.Console;

import java.io.*;

java.lang package is automatically imported in every Java program.

In order to use pre-defined methods we need to import packages/classes.

Using import keyword we can import packages/classes.
--------------------------------------
Categories of Built-in/Pre-defined Methods

    1) String Methods
    2) Array Methods
    3) Number Methods
    4) Character Methods
    etc...
------------------------------------------
iii) User Defined Methods
Two types of User defined Methods

1) Method without returning any Value.

    a) Calling Methods by invoking Object
    b) Calling Methods without invoking Object

2) Method with returning a Value.
    a) Calling Methods by invoking Object
    b) Calling Methods without invoking Object

Calling External methods
Examples:

Writing/Creating Methods

We write Methods before the main method

Calling Methods

We call methods after the main method.
--------------------------------------
1) Method with returning a Value
a) Call Methods by invoking Object

//Write Methods

Syntax:

accessModifier returnType methodName(Parameters){
Statements
------------
-----------
---------
return statement
}
//Create Object

ClassName objectName = new ClassName();

//Call Methods

dataType variableName = objectName.method(values for Parameters);
--------------------------------------
Examples:

public class Class1 {
//User defined Methods
public int multiply(int a, int b, int c){
int result = a * b * c;
return result;
}
public static void main(String[] args) {
//Create Object
Class1 abc = new Class1();
   
//Call Methods
int x = abc.multiply(10, 5, 7);
System.out.println(x);

System.out.println(abc.multiply(10, 5, 7));
}

1) Method with returning a Value

b) Call Methods Without invoking Object

//Create Methods

Syntax:

accessModifier nonAccessModifier returnType methodName(Parameters){
Statements
-------------
------------
-----------
}

//Call Methods

dataType variableName = ClassName.methodName(values for parameters);

Example:

public static int add(int a, int b){
int    result;
result= a + b;
return result;
}

public static void main(String[] args) {

int y = Class1.add(123, 456);
System.out.println(y);   

System.out.println(Class1.add(123, 456));

int z = add(12, 45);
System.out.println(z);

System.out.println(add(12, 45));
}
}
--------------------------------------
2) Method without returning any value

a) Call Methods by invoking object

b) Call Methods without invoking Object
-------------------------------------
a) Call Methods by invoking object

Syntax for Create Method:

accessModifier returnTypeNothing methodName(parameters){
Statements
---------------
---------------
-------------
}

//Create Object

ClassName objectName = new ClassName();

//Call Methods

objectName.methodName(values for parameters);
--------------------------------------
Example:

public class Class1 {
public void comparison(int a, int b){
if (a > b){
System.out.println("A is a Big Number");
}
else
{
System.out.println("B is a Big Number");   
}
}

public static void main(String[] args) {
Class1 xyz = new Class1();

xyz.comparison(19, 17);
}
--------------------------------------
2) Method without returning any value

b) Call Methods without invoking Object

//Syntax for Creating Method

accessModifier nonAccessModifier returnTypeNothing methodName(parameters){
Statements
-------------
-------------
}

Syntax for Calling Method
ClassName.methodName(values for Parameters);

Example:
public class Class1 {
public static void comparison(int a, int b){
if (a > b){
System.out.println("A is a Big Number");
}
else
{
System.out.println("B is a Big Number");   
}
}

public static void main(String[] args) {

Class1.comparison(1234, 234);
comparison(4, 6);
}
--------------------------------------
Usage of Methods

a) Internal use (Defining/Creating and Calling Methods within the same class)

b) External use (Calling methods from another classes)
--------------------------------------
Example:

Class 1: (Internal use of methods)

public class Class1 {
//Create a Method with returning value and call the method by invoking Object
//Add first 2 numbers and multiply with 3rd number
public int addAndmultiply(int a, int b, int c){
int result = (a + b) * c;
return result;
}
//Create a Method with returning value and call the method without invoking object
public static int grade(int marks){
int result;   
if (marks >= 600){
result =1;
}
else if ((marks >= 500) && (marks <600)){
result =2;   
}
else
{
result =3;   
}
return result;
}

//Create a Method without returning any value and call the method by invoking Object
public void multiply (int a, int b, int c){
int result = a * b * c;
System.out.println(result);
}

//Create a Method without returning value and call the method without invoking Object
public static void comparison(int a, int b){
if (a > b){
System.out.println("A is a Big Number");
}
else {
System.out.println("B is a Big Number");   
}
}

public static void main(String[] args) {

Class1 obj = new Class1();
int x = obj.addAndmultiply(10, 5, 2);
System.out.println(x);
int y = Class1.grade(450);
System.out.println(y);

obj.multiply(4, 5, 7);
Class1.comparison(10, 20);
}
}
--------------------------------------
Class 2: (External use of Methods)

public static void main(String[] args) {
//Create object using Class1 in Class2
Class1 abc = new Class1();
int s = abc.addAndmultiply(2, 4, 9);
System.out.println(s);

abc.multiply(3, 5, 6);

int g = Class1.grade(700);
System.out.println(g);

Class1.comparison(20, 10);
}
}
--------------------------------------
Other Topics:

i) Method OverLoading

ii) Method Overriding
--------------------------------------

Conclusion

Java Methods
    i) Built-in/Pre-defined Methods
    ii) User defined Methods
        1) Method with returning value
        2) Method without returning value

Usage of Methods
1) Calling methods by invoking Object
2) Calling methods without invoking object
3) Calling External methods
--------------------------------------

0 comments:

Post a Comment