Selenium Tutorial 7
Java User defined Methods:
Creating methods amongst returning value as well as without returning value.
Calling Internal as well as external methods
Method Overloading:
If a Class convey multiple methods amongst same name, only amongst unlike parameters, as well as thence it is known every bit Method overloading.
There are 2 ways to overload the method inward Java.
i) By changing publish of Arguments
Example: We convey 2 methods inward our cast amongst the cite of add.
a) int add(int a, int b)
b) int add(int a, int b, int c)
-----------------------------
ii) By changing information types.
a) int add together (int a, int b)
b) double add together (double a, double b)
--------------------------------------
Example:package Saturday;
public cast JavaMetods {
populace static void original (String [] args){
int x = add(5, 9);
int y = add together (5, 7, 9);
double z = add(5.234, 6.123);
System.out.println(x);
System.out.println(y);
System.out.println(z);
}
populace static int add together (int a, int b){
int result;
lawsuit = a + b;
supply result;
}
populace static int add together (int a, int b, int c){
int result;
lawsuit = a + b + c;
supply result;
}
populace static double add together (double a, double b){
double result;
lawsuit = a + b;
supply result;
}
}
-------------------------------------
Advantages of Method overloading
It increases the readability of the Program.
----------------------------------------------
Java Built inward Methods
Categories of Built inward 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 declaration as well as thence 0
if the integer is less than the declaration as well as thence -1
if the integer is greater than the declaration as well as thence 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) circular (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) 2d (Returns minimum value betwixt 2 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 betwixt 2 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 conditions 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 conditions 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 conditions the value is Upper representative or not?)
example:
System.out.println(Character.isUpperCase('C'));//true
System.out.println(Character.isDigit('z')); //false
-------------------------------------
4) isLowercase (Checks conditions the value is Lower representative 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 2 strings)
Example:
String str1 ="SELENIUM";
String str2 ="selenium";
String str3 ="seleniuma";
String str4 ="selenium";
int result;
lawsuit = str1.compareTo(str2);
System.out.println(result); //
lawsuit = str3.compareTo(str2);
System.out.println(result); //
lawsuit = str2.compareTo(str4);
System.out.println(result); //
-------------------------------------
2) charAt (character yesteryear position)
String str1 ="SELENIUM";
char lawsuit = 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 down (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 amongst 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 for sure 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 inward Java
> An Exception is an event, it occurs during execution of a program,
when normal execution of the plan is interrupted.
> Exception treatment is machinery to stimulate exceptions
--------------------------
Common Scenarios where exceptions may occur:------------------------------------------------
1) Scenario where ArithmeticException occurs
If nosotros separate whatsoever publish yesteryear Zero at that spot ArithmeticException occurs
Ex:
int a = 10/0;
----------------------------------------------
2) Scenario where NullPointerException occurs
if nosotros convey aught value inward whatsoever variable, performing whatsoever functioning yesteryear the variable,
ex:
String second = null;
System.out.println(s.length());//NullPointerException
---------------------------------------------------
3) Scenario where NumberFormatException occurs
The incorrect formatting of whatsoever value, may occur NumberFormatException
Ex:
String second = "abc";
int y = Integer.parseInt(s);
System.out.println(y);//NumberFormatException
--------------------------------------
4) Scenario where ArrayIndexOutOfBounds exception occurs
If nosotros are inserting whatsoever value inward the incorrect index
Ex:
int [] a = novel int [5];
a[10] = 100;
System.out.println(a[10]);//ArrayIndexOutOfBounds
--------------------------------------------------------------
Example:
int a =10;
int b = 0;
int result;
lawsuit = a/b;
System.out.println(result);
System.out.println("Hello Java");
System.out.println("Hello Selenium");
----------------------------------
Use crusade block:
Syntax:
--------
try {
Statements
------
-------
------
}
catch (exception e) {
Exception treatment code
}
-----------------------------------
With Exception handling
int a =10;
int b = 0;
int result;
try{
lawsuit = a/b;
System.out.println(result);
}
grab (ArithmeticException e){
System.out.println("Divided yesteryear Zero Error");
}
System.out.println("Hello Java");
System.out.println("Hello Selenium");
}
--------------------------------------
Multiple crusade blocks for treatment multiple exceptions
Example:
int a =10;
int b = 0;
int result;
int [] array1 = novel int [4];
try{
lawsuit = a/b;
System.out.println(result);
}
grab (ArithmeticException e){
System.out.println("Divided yesteryear Zero Error");
}
try{
array1[10]= 100;
System.out.println(array1[10]);
}
grab (ArrayIndexOutOfBoundsException e){
System.out.println("Array Out of Bound Error");
}
System.out.println("Hello Java");
System.out.println("Hello Selenium");
}
---------------------------------------
Java User defined Methods:
Creating methods amongst returning value as well as without returning value.
Calling Internal as well as external methods
Method Overloading:
If a Class convey multiple methods amongst same name, only amongst unlike parameters, as well as thence it is known every bit Method overloading.
There are 2 ways to overload the method inward Java.
i) By changing publish of Arguments
Example: We convey 2 methods inward our cast amongst the cite of add.
a) int add(int a, int b)
b) int add(int a, int b, int c)
-----------------------------
ii) By changing information types.
a) int add together (int a, int b)
b) double add together (double a, double b)
--------------------------------------
Example:package Saturday;
public cast JavaMetods {
populace static void original (String [] args){
int x = add(5, 9);
int y = add together (5, 7, 9);
double z = add(5.234, 6.123);
System.out.println(x);
System.out.println(y);
System.out.println(z);
}
populace static int add together (int a, int b){
int result;
lawsuit = a + b;
supply result;
}
populace static int add together (int a, int b, int c){
int result;
lawsuit = a + b + c;
supply result;
}
populace static double add together (double a, double b){
double result;
lawsuit = a + b;
supply result;
}
}
-------------------------------------
Advantages of Method overloading
It increases the readability of the Program.
----------------------------------------------
Java Built inward Methods
Categories of Built inward 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 declaration as well as thence 0
if the integer is less than the declaration as well as thence -1
if the integer is greater than the declaration as well as thence 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) circular (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) 2d (Returns minimum value betwixt 2 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 betwixt 2 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 conditions 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 conditions 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 conditions the value is Upper representative or not?)
example:
System.out.println(Character.isUpperCase('C'));//true
System.out.println(Character.isDigit('z')); //false
-------------------------------------
4) isLowercase (Checks conditions the value is Lower representative 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 2 strings)
Example:
String str1 ="SELENIUM";
String str2 ="selenium";
String str3 ="seleniuma";
String str4 ="selenium";
int result;
lawsuit = str1.compareTo(str2);
System.out.println(result); //
lawsuit = str3.compareTo(str2);
System.out.println(result); //
lawsuit = str2.compareTo(str4);
System.out.println(result); //
-------------------------------------
2) charAt (character yesteryear position)
String str1 ="SELENIUM";
char lawsuit = 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 down (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 amongst 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 for sure 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 inward Java
> An Exception is an event, it occurs during execution of a program,
when normal execution of the plan is interrupted.
> Exception treatment is machinery to stimulate exceptions
--------------------------
Common Scenarios where exceptions may occur:------------------------------------------------
1) Scenario where ArithmeticException occurs
If nosotros separate whatsoever publish yesteryear Zero at that spot ArithmeticException occurs
Ex:
int a = 10/0;
----------------------------------------------
2) Scenario where NullPointerException occurs
if nosotros convey aught value inward whatsoever variable, performing whatsoever functioning yesteryear the variable,
ex:
String second = null;
System.out.println(s.length());//NullPointerException
---------------------------------------------------
3) Scenario where NumberFormatException occurs
The incorrect formatting of whatsoever value, may occur NumberFormatException
Ex:
String second = "abc";
int y = Integer.parseInt(s);
System.out.println(y);//NumberFormatException
--------------------------------------
4) Scenario where ArrayIndexOutOfBounds exception occurs
If nosotros are inserting whatsoever value inward the incorrect index
Ex:
int [] a = novel int [5];
a[10] = 100;
System.out.println(a[10]);//ArrayIndexOutOfBounds
--------------------------------------------------------------
Example:
int a =10;
int b = 0;
int result;
lawsuit = a/b;
System.out.println(result);
System.out.println("Hello Java");
System.out.println("Hello Selenium");
----------------------------------
Use crusade block:
Syntax:
--------
try {
Statements
------
-------
------
}
catch (exception e) {
Exception treatment code
}
-----------------------------------
With Exception handling
int a =10;
int b = 0;
int result;
try{
lawsuit = a/b;
System.out.println(result);
}
grab (ArithmeticException e){
System.out.println("Divided yesteryear Zero Error");
}
System.out.println("Hello Java");
System.out.println("Hello Selenium");
}
--------------------------------------
Multiple crusade blocks for treatment multiple exceptions
Example:
int a =10;
int b = 0;
int result;
int [] array1 = novel int [4];
try{
lawsuit = a/b;
System.out.println(result);
}
grab (ArithmeticException e){
System.out.println("Divided yesteryear Zero Error");
}
try{
array1[10]= 100;
System.out.println(array1[10]);
}
grab (ArrayIndexOutOfBoundsException e){
System.out.println("Array Out of Bound Error");
}
System.out.println("Hello Java");
System.out.println("Hello Selenium");
}
---------------------------------------