Java for Selenium Part 8

User defined Methods inwards Java

i) Introduction to Java Methods

ii) Types of Methods inwards Java

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

What is Method?

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

Methods are likewise known equally Functions

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

In Object Oriented Programming (Ex: Java Language) nosotros role Methods (Built-in, User defined).
--------------------------------------
When nosotros select Methods?

Whenever nosotros desire perform whatsoever functioning multiple times together with then nosotros select Methods.

All Programming concepts tin locomote used inwards Methods.

Advantages of Methods

Code Reusability, using methods nosotros tin trim down the projection code size.

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

1) Built in/Pre-defined Methods

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

Java has a library of classes together with methods organized inwards packages.

Ex:

import java.io.Console;

import java.io.*;

java.lang parcel is automatically imported inwards every Java program.

In guild to role pre-defined methods nosotros require to import packages/classes.

Using import keyword nosotros tin 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 whatsoever Value.

    a) Calling Methods past times invoking Object
    b) Calling Methods without invoking Object

2) Method amongst returning a Value.
    a) Calling Methods past times invoking Object
    b) Calling Methods without invoking Object

Calling External methods
Examples:

Writing/Creating Methods

We write Methods earlier the primary method

Calling Methods

We telephone yell upward methods later the primary method.
--------------------------------------
1) Method amongst returning a Value
a) Call Methods past times invoking Object

//Write Methods

Syntax:

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

ClassName objectName = novel ClassName();

//Call Methods

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

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

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

1) Method amongst 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 whatsoever value

a) Call Methods past times invoking object

b) Call Methods without invoking Object
-------------------------------------
a) Call Methods past times invoking object

Syntax for Create Method:

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

//Create Object

ClassName objectName = novel ClassName();

//Call Methods

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

public course of teaching 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 = novel Class1();

xyz.comparison(19, 17);
}
--------------------------------------
2) Method without returning whatsoever 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 course of teaching 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 role (Defining/Creating together with Calling Methods inside the same class)

b) External role (Calling methods from or thus other classes)
--------------------------------------
Example:

Class 1: (Internal role of methods)

public course of teaching Class1 {
//Create a Method amongst returning value together with telephone yell upward the method past times invoking Object
//Add outset two numbers together with multiply amongst tertiary number
public int addAndmultiply(int a, int b, int c){
int number = (a + b) * c;
return result;
}
//Create a Method amongst returning value together with telephone yell upward 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 whatsoever value together with telephone yell upward the method past times invoking Object
public void multiply (int a, int b, int c){
int number = a * b * c;
System.out.println(result);
}

//Create a Method without returning value together with telephone yell upward 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 = novel 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 role of Methods)

public static void main(String[] args) {
//Create object using Class1 inwards Class2
Class1 abc = novel Class1();
int second = 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 amongst returning value
        2) Method without returning value

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

Sumber http://www.gcreddy.com/
Post a Comment (0)
Previous Post Next Post