Selenium Tutorial 7


Selenium Tutorial 7

Java User defined Methods:
Creating methods with returning value and without returning value.

Calling Internal and external methods

Method Overloading:
If a Class have multiple methods with same name, but with different parameters, then it is known as Method overloading.

There are two ways to overload the method in Java.

i) By changing number of Arguments
Example: We have two methods in our class with the name of add.

a) int add(int a, int b)

b) int add(int a, int b, int c)
-----------------------------
ii) By changing data types.
a) int add (int a, int b)

b) double add (double a, double b)
--------------------------------------
Example:package Saturday;

public class JavaMetods {
    public static void main (String [] args){
        int x = add(5, 9);
        int y = add (5, 7, 9);
        double z = add(5.234, 6.123);
        System.out.println(x);
        System.out.println(y);
        System.out.println(z);
    }
    public static int add (int a, int b){
        int result;
        result = a + b;
        return result;
    }
    public static int add (int a, int b, int c){
        int result;
        result = a + b + c;
        return result;
    }
    public static double add (double a, double b){
        double result;
        result = a + b;
        return result;
    }
}
-------------------------------------
Advantages of Method overloading
It increases the readability of the Program.
----------------------------------------------
Java Built in Methods
Categories of Built in methods:
a) Number methods
b) Character methods
c) String methods
d) Array methods etc...
------------------------------
a) Number methods
1) compareTo() method

Example:
Integer a = 5;
        System.out.println(a.compareTo(8)); //-1
        System.out.println(a.compareTo(5));//0
        System.out.println(a.compareTo(2));//1

Result Criteria:

if the integer is equal to the argument then 0
if the integer is less than the argument then -1
if the integer is greater than the argument then 1
-----------------------------------
2) equals() method

Integer a = 5;
        Integer b = 10;
        Integer c = 5;
        Short d = 5;
        System.out.println(a.equals(b));//false
        System.out.println(a.equals(c)); //true
        System.out.println(a.equals(d)); //false
--------------------------------------
3) abs (Returns Absolute value)

Example:

Integer a = -5;
        double b = -10.234;
       
        System.out.println(Math.abs(a));// 5
        System.out.println(Math.abs(b));// 10.234

----------------------------------------------
4) round (Rounds the value nearest Integer)

Example:
double a = 10.575;
        double b = 10.498;
       
        System.out.println(Math.round(a));// 11
        System.out.println(Math.round(b));// 10

-----------------------------------------
5) min (Returns minimum value between two numbers)

Example:

int a =10, b =20;
        double c = 1.234, d = 3.567;
        System.out.println(Math.min(a, b)); // 10
        System.out.println(Math.min(c, d)); // 1.234
        System.out.println(Math.min(123, 124)); // 123
        System.out.println(Math.min(10.345, 10.3451)); // 10.345
        System.out.println(Math.min(1, 1)); // 1
-----------------------------------------
6) max (Returns maximum value between two numbers)

Example:
int a =10, b =20;
        double c = 1.234, d = 3.567;
        System.out.println(Math.max(a, b)); // 20
        System.out.println(Math.max(c, d)); // 3.567
        System.out.println(Math.max(123, 124)); // 124
        System.out.println(Math.max(10.345, 10.3451)); // 10.3451
        System.out.println(Math.max(1, 1)); // 1
-------------------------------
7) random (Generates Random Number)

Example:
System.out.println(Math.random()); //
System.out.println(Math.random()); //
---------------------------------------------
b) Character methods
1) isLetter (Checks weather the value is Alfa byte or not?)

Example:
        char a = '1';
        System.out.println(Character.isLetter(a)); //false
        System.out.println(Character.isLetter('A'));//true
        System.out.println(Character.isLetter('a'));//true
        System.out.println(Character.isLetter('*'));//false
--------------------------------
2) isDigit (It returns weather the value is Number or not?)

        char a = '1';
        System.out.println(Character.isDigit(a)); //true
        System.out.println(Character.isDigit('A'));//false
        System.out.println(Character.isDigit('a'));//false
        System.out.println(Character.isDigit('*'));//false
        System.out.println(Character.isDigit('7')); //true
-----------------------------------------------
3) isUppercase (Checks weather the value is Upper case or not?)

example:
System.out.println(Character.isUpperCase('C'));//true
System.out.println(Character.isDigit('z')); //false
-------------------------------------
4) isLowercase (Checks weather the value is Lower case or not?)

Example:
System.out.println(Character.isLowerCase('C'));//false
System.out.println(Character.isLowerCase('z')); //true
------------------------------
5) toUppercase (Converts the value to Upper case)

Example:
System.out.println(Character.toUpperCase('a'));//A
System.out.println(Character.toUpperCase('A')); //A
-----------------------------------
6) toLowercase (Converts the value to Lower case)

Example:
System.out.println(Character.toLowerCase('a'));//a
System.out.println(Character.toLowerCase('A')); //a
-------------------------------------------------------
c) String methods
1) compareTo() method (It compares two strings)

Example:
String str1 ="SELENIUM";
        String str2 ="selenium";
        String str3 ="seleniuma";
        String str4 ="selenium";
        int result;
        result = str1.compareTo(str2);
        System.out.println(result); //
       
        result = str3.compareTo(str2);
        System.out.println(result); //
       
        result = str2.compareTo(str4);
        System.out.println(result); //
-------------------------------------
2) charAt (character by position)

String str1 ="SELENIUM";
        char result = str1.charAt(0);
        System.out.println(result); //S
               
-------------------------
3) concat (String concatanation)

String str1 ="Selenium";
        String str2 = " Java";
        str1 = str1.concat(str2);
        System.out.println(str1);

-----------------------------
4) equals (String equals)

Example:
String str1 ="Selenium";
        String str2 = "UFT";
        String str3 ="Selenium";
       
        System.out.println(str1.equals(str2)); //false
        System.out.println(str1.equals(str3));     //true   
-----------------------------
5) equalsIgnorecase

Examples:
String str1 ="selenium";
        String str2 = "UFT";
        String str3 ="SELENIUM";
        String str4 ="SELENIUM";
       
        System.out.println(str3.equalsIgnoreCase(str4)); //true
        System.out.println(str1.equalsIgnoreCase(str3)); //true   
        System.out.println(str1.equalsIgnoreCase(str2)); //false
-----------------------------------------------
6) toUppercase (Converts values To Upper case)

Example:
String str1 ="selenium";
        String str2 ="SELEnium";
        String str3 ="SELENIUM";
       
        System.out.println(str1.toUpperCase());
        System.out.println(str2.toUpperCase());
        System.out.println(str3.toUpperCase());
-------------------------------------
7) toLowercase (Converts values To Lower case)

String str1 ="selenium";
        String str2 ="SELEnium";
        String str3 ="SELENIUM";
       
        System.out.println(str1.toLowerCase()); //selenium
        System.out.println(str2.toLowerCase()); //selenium
        System.out.println(str3.toLowerCase()); //selenium
---------------------------------------------
8) trim (removes spaces from both sides of a String)

Example:

String str1 ="           Selenium              ";
        String str2 ="               SELEnium";
        String str3 ="SELENIUM                ";
       
        System.out.println(str1);
        System.out.println(str1.trim());
        System.out.println(str2.trim());
        System.out.println(str3.trim());
---------------------------------------------
9) substring (Returns sub string)

Example:
String str1 ="Welcome to Selenium Testing";
       
        System.out.println(str1.substring(10)); // Selenium Testing
        System.out.println(str1.substring(19)); //Testing
        System.out.println(str1.substring(10, 18)); //Selenium
------------------------------------------------
10) endsWith (ends with specified suffix)

Example:
-----------
String str1 = "Selenium Testing";
       
        System.out.println(str1.endsWith("Testing"));//true
        System.out.println(str1.endsWith("ing"));//true
        System.out.println(str1.endsWith("Selenium"));//false
--------------------------------------------------
d) Array methods
1) length method

int [] array1 = {10, 20, 30, 40, 50};
System.out.println(array1.length);//5
-------------------------------------
2) toString() (print Array)

String [] arr1 ={"UFT", "Selenium", "RFT", "SilkTest"};
        String str = Arrays.toString(arr1);
        System.out.println(str);

--------------------------------------
3) contains (Checks if the Array contains certain value or not?)

String [] arr1 ={"UFT", "Selenium", "RFT", "SilkTest"};
        boolean a = Arrays.asList(arr1).contains("UFT");
        boolean b = Arrays.asList(arr1).contains("uft");
        System.out.println(a);// true
        System.out.println(b);// false
----------------------------------------------------------
Exception Handling in Java
> An Exception is an event, it occurs during execution of a program,
when normal execution of the program is interrupted.

> Exception handling is mechanism to handle exceptions
--------------------------
Common Scenarios where exceptions may occur:------------------------------------------------
1) Scenario where ArithmeticException occurs
If we divide any number by Zero there ArithmeticException occurs

Ex:

int a = 10/0;
----------------------------------------------
2) Scenario where NullPointerException occurs
if we have null value in any variable, performing any operation by the variable,

ex:

String s = null;
System.out.println(s.length());//NullPointerException
---------------------------------------------------
3) Scenario where NumberFormatException occurs
The wrong formatting of any value, may occur  NumberFormatException

Ex:
String s = "abc";
        int y = Integer.parseInt(s);
        System.out.println(y);//NumberFormatException
--------------------------------------
4) Scenario where ArrayIndexOutOfBounds exception occurs
If we are inserting any value in the wrong index

Ex:
int [] a = new int [5];
        a[10] = 100;
        System.out.println(a[10]);//ArrayIndexOutOfBounds
--------------------------------------------------------------
Example:
int a =10;
        int b = 0;
        int result;
        result = a/b;
        System.out.println(result);
        System.out.println("Hello Java");
        System.out.println("Hello Selenium");
----------------------------------
Use try block:
Syntax:
--------
try {
Statements
------
-------
------
}
catch (exception e) {
Exception handling code
}
-----------------------------------
With Exception handling

int a =10;
        int b = 0;
        int result;
        try{
        result = a/b;
        System.out.println(result);
        }
        catch (ArithmeticException e){
        System.out.println("Divided by Zero Error");   
        }
        System.out.println("Hello Java");
        System.out.println("Hello Selenium");
        }
--------------------------------------
Multiple try blocks for handling multiple exceptions
Example:
int a =10;
        int b = 0;
        int result;
        int [] array1 = new int [4];
        try{
        result = a/b;
        System.out.println(result);
        }
        catch (ArithmeticException e){
        System.out.println("Divided by Zero Error");   
        }
       
        try{
            array1[10]= 100;
            System.out.println(array1[10]);
        }
        catch (ArrayIndexOutOfBoundsException e){
            System.out.println("Array Out of Bound Error");
        }
        System.out.println("Hello Java");
        System.out.println("Hello Selenium");
        }
---------------------------------------

0 comments:

Post a Comment