Selenium Tutorial 6
Java Loop Statements
a) for..Loop
b) while...Loop
c) produce while...Loop
d) enhanced for...Loop
--------------------------
a) for..Loop
It repeats a block of statements for a specified set out of times
Syntax:
for (stratValue; endvalue; increment/decrement) {
Statements
----------
------------
----------
}
Example 1:
// Print 1 to 10 Numbers using for loop
for (int i = 1; i <= 10; i++){
System.out.println(i);
}
-------------------------
Example 2:
// Print 10 to 1 Numbers using for loop
for (int i = 10; i >= 1; i--){
System.out.println(i);
}
---------------------------
Example 3:
// Print 1 to 10 numbers except 7
for (int i = 1; i <= 10; i++){
if (i != 7){
System.out.println(i);
}
}
---------------------------------
b) piece loop
It repeats a block of statements piece status is true.
Syntax:
Initialization
while (condition) {
Statements
------------
-----------
increment/decrement
}
Example 1:
// Print 1 to 10 numbers using piece loop
int i = 1;
piece (i <= 10){
System.out.println(i);
i++;
}
---------------------------------
example 2:
// Print 10 to 1 numbers using piece loop
int i = 10;
piece (i >= 1){
System.out.println(i);
i--;
}
-------------------------------------------
c) produce piece loop
It repeats a block of statements piece status is true,
It executes a block of statements at to the lowest degree i time irrespective of the condition.
Syntax:
Initialization
do
{
Statements
------
---------
increment/decrement
} piece (condition);
Example:
---------------
// Print 1 to 10 numbers using produce piece loop
int i = 1;
do
{
System.out.println(i);
i++;
} piece (i <=10);
-----------------------------
int i = 100;
do
{
System.out.println(i);
i++;
} piece (i <=10);
-------------------------------------
d) Enhanced for...loop
It executes all elements inwards an Array
Syntax:
Array declaration
for (declaration: expression/Array){
Statements
-------
}
--------------------------------
Example:
String [] languages = {"C", "COBOL", "Java"};
for (String lang: languages){
System.out.println(lang);
}
-----------------
Example 2:
----------------
int [] mathoperations = novel int [3];
int a =10, b= 20;
mathoperations[0] = a + b;
mathoperations[1] = a - b;
mathoperations[2] = a * b;
for (int operation: mathoperations){
System.out.println(operation);
}
------------------------------------------------
String Handling inwards Java
What is String?
String is a sequence of characters written inwards double quotes.
String may accept Alfa bytes, numbers too special characters.
Example:
System.out.println("Selenium Testing"); //Selenium Testing
System.out.println("123 Selenium Testing");//123 Selenium Testing
System.out.println("Selenium*&123 Testing");//Selenium*&123 Testing
-------------------
Creating Strings
String is considered equally object inwards Java.
Example:
String myTool = "Selenium"; //String variable
String [] myTools = {"UFT", "Selenium", "LoadRunner", "Quality Center"};// Array of Strings
System.out.println(myTool); //Selenium
for (String tool: myTools){
System.out.println(tool);
}
-----------------------------
for (int i = 0; i < myTools.length; i++){
System.out.println(myTools[i]); // Print Array of strings
}
-----------------------------------------
Concatenating Strings
String str1 = "Selenium";
String str2 = " Testing";
System.out.println(str1 + str2); // Selenium Testing
System.out.println("Selenium" + " Testing"); // Selenium Testing
System.out.println(1 + 1 + "Selenium"); // 2Selenium
System.out.println("Selenium" + 1 + 1); // Selenium11
}
---------------------------------
String comparison
a) String comparing using (==) Operator
It supports 2-way comparing (true or false)
b) String comparing using equals() method
It supports 2-way comparing (true or false)
c) String comparing using compareTo() method
It supports 3-way comparing (0, > 1 too <1)
If Str1 = str2 therefore 0
If str1 > str2 therefore > 0
If str1 < str2 therefore < 0
-----------------------------------------
Example:
String str1 = "selenium";
String str2 = "SELENIUM";
String str3 = "SELENIUM";
String str4 = "zselenium";
// String comparing using == Relational Operator
System.out.println(str1 == str2); // false
System.out.println(str2 == str3); // true
// String comparing using equals method
System.out.println(str1.equals(str2)); // false
System.out.println(str2.equals(str3)); // true
// String comparing using compareTo() method
System.out.println(str1.compareTo(str2)); // Greater than 1 (Positive)
System.out.println(str2.compareTo(str3)); // 0
System.out.println(str1.compareTo(str4)); // less than 0 (Negative value)
---------------------------------
Java Arrays
Java Array is an Object that holds a fixed set out of values of a unmarried information type.
The length of an Array is established whe the Array is created.
Array length is fixed, coffee Array has cypher based index.
--------------------------------------
Declaration of Arrays
1st method
int abc []; //Creating Array
abc = novel int [4]; // Defining size
abc[0] = 10; // Assigning values
abc[1] = 20;
System.out.println(abc[0] + abc[1]); //30
-----------------------------
2nd method
int [] xyz = novel int [5];
xyz[0] = 10; // Assigning values
xyz[1] = 20;
System.out.println(xyz[1] + xyz[2]); //20
------------------------------
3rd method:
int [] xyz = {10, 20, 30, 40, 50};
System.out.println(xyz[1] + xyz[2]); //50
--------------------------------------------
Creating dissimilar types of Arrays
char [] abc = {'A', 'B', 'C', 'd'}; //Array of Characters
int [] xyz = {10, 20, 30, 40}; // Array of Integers
String [] a = {"Selenium", "UFT", "LoadRunner"}; //Array of Strings
boolean [] b = {true, false, false, true}; //Array of Boolean values
System.out.println(abc [1]); //B
System.out.println(xyz [2]);//30
System.out.println(a [1]); //UFT
System.out.println(b [1]); // false
-------------------------------------------------
Copy values an Array into simply about other Array
int [] array1 = {1, 2, 3, 4, 5};
int [] array2;
array2 = array1;
System.out.println(array2[1]); // 2
for (int i=0; i < array2.length; i++){
System.out.println(array2[i]);
}
--------------------------------------------------
Types of Arrays
Two types of Arrays
i) Single dimensional Array
ii) Multi dimensional Array
Ex:
int [] array1 = {1, 2, 3, 4, 5}; // Single dimensional Array
int [] [] array2 = {{1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}}; //Multi dimensional Array
System.out.println((array2[0][0]));//1
System.out.println((array2[1][0]));//2
System.out.println((array2[0][4]));//9
System.out.println((array2[1][3])); //8
Assignment:
print Multi dimensional array values using nested for loop.
------------------------------------------------------
Advantages of Arrays
> Using Arrays nosotros tin optimize the code, information tin hold upward retrieved easily
> nosotros tin instruct required information using index position.
-----------------------------
Disadvantages of Arrays
We tin shop fixed set out of elements only
It doesn't alter its size during execution.
-----------------------------------------
Java Methods
What is Method?
A laid of statements to perform an Operation.
Methods are also known equally procedures or functions.
In structured programming nosotros utilization functions (Built inwards too user defined)
In Object Oriented programming nosotros utilization Methods (Built inwards too user defined)
Usage of Methods:
---------------------
Whenever nosotros desire to perform operations several times therefore nosotros prefer methods.
Using methods nosotros tin trim the code size.
Types of Methods:
Basically nosotros accept ii types of methods inwards Java.
i) Built inwards methods
ii) User defined methods
-----------------------------------------
Built inwards Methods:
> Java has a library of classes too methods, organized inwards packages.
> In gild to utilization built inwards methods, nosotros import pre-defined packages/particular classes.
> java.lang bundle is automatically imported inwards whatever coffee program.
> Using import keyword nosotros tin import pre-defined packages.
-------------------------------
Categories of Built inwards methods:
a) String Methods
b) Character methods
c) Number methods
d) Array methods etc...
--------------------------------------
User defined methods inwards Java:
Two types of user defined methods:
i) Method without returning whatever value
ii) Method alongside returning a value.
--------------------------------------
Writing Methods (With Returning value)
Syntax:
modifier returnType methodname (parameters) {
Statements
----------
----------
}
Example:
public static int add together (int a, int b){
int result;
lawsuit = a + b;
provide result;
}
--------------------
calling a Method
dataType variablename = Method(Values)
----------------------------------
Example:
int a = add(123, 456);
System.out.println(a);
--------------------------------------
Example 2:
public static void primary (String [] args){
int a = xyz(2, 4, 6);
System.out.println(a);
}
// Write a method to multiply three numbers
world static int xyz (int a, int b, int c){
int result;
lawsuit = a * b * c;
provide result;
}
----------------------------------------------
Method without returning whatever value
Syntax:
modifier methodName(Parameters){
Statements
----------
---------
}
Example:
public static void primary (String [] args){
studentRank(700);
}
public static void studentRank(int marks){
if (marks >= 600){
System.out.println("Rank A1");
}
else if (marks >= 500){
System.out.println("Rank A2");
}
else {
System.out.println("Rank A3");
}
}
---------------------------------------
// Calling a method from simply about other shape (External Method)
public shape SeleniumClass extends ArrayExamples {
world static void primary (String [] args){
studentRank(650); // External method
}
-------------------------------------
Java Loop Statements
a) for..Loop
b) while...Loop
c) produce while...Loop
d) enhanced for...Loop
--------------------------
a) for..Loop
It repeats a block of statements for a specified set out of times
Syntax:
for (stratValue; endvalue; increment/decrement) {
Statements
----------
------------
----------
}
Example 1:
// Print 1 to 10 Numbers using for loop
for (int i = 1; i <= 10; i++){
System.out.println(i);
}
-------------------------
Example 2:
// Print 10 to 1 Numbers using for loop
for (int i = 10; i >= 1; i--){
System.out.println(i);
}
---------------------------
Example 3:
// Print 1 to 10 numbers except 7
for (int i = 1; i <= 10; i++){
if (i != 7){
System.out.println(i);
}
}
---------------------------------
b) piece loop
It repeats a block of statements piece status is true.
Syntax:
Initialization
while (condition) {
Statements
------------
-----------
increment/decrement
}
Example 1:
// Print 1 to 10 numbers using piece loop
int i = 1;
piece (i <= 10){
System.out.println(i);
i++;
}
---------------------------------
example 2:
// Print 10 to 1 numbers using piece loop
int i = 10;
piece (i >= 1){
System.out.println(i);
i--;
}
-------------------------------------------
c) produce piece loop
It repeats a block of statements piece status is true,
It executes a block of statements at to the lowest degree i time irrespective of the condition.
Syntax:
Initialization
do
{
Statements
------
---------
increment/decrement
} piece (condition);
Example:
---------------
// Print 1 to 10 numbers using produce piece loop
int i = 1;
do
{
System.out.println(i);
i++;
} piece (i <=10);
-----------------------------
int i = 100;
do
{
System.out.println(i);
i++;
} piece (i <=10);
-------------------------------------
d) Enhanced for...loop
It executes all elements inwards an Array
Syntax:
Array declaration
for (declaration: expression/Array){
Statements
-------
}
--------------------------------
Example:
String [] languages = {"C", "COBOL", "Java"};
for (String lang: languages){
System.out.println(lang);
}
-----------------
Example 2:
----------------
int [] mathoperations = novel int [3];
int a =10, b= 20;
mathoperations[0] = a + b;
mathoperations[1] = a - b;
mathoperations[2] = a * b;
for (int operation: mathoperations){
System.out.println(operation);
}
------------------------------------------------
String Handling inwards Java
What is String?
String is a sequence of characters written inwards double quotes.
String may accept Alfa bytes, numbers too special characters.
Example:
System.out.println("Selenium Testing"); //Selenium Testing
System.out.println("123 Selenium Testing");//123 Selenium Testing
System.out.println("Selenium*&123 Testing");//Selenium*&123 Testing
-------------------
Creating Strings
String is considered equally object inwards Java.
Example:
String myTool = "Selenium"; //String variable
String [] myTools = {"UFT", "Selenium", "LoadRunner", "Quality Center"};// Array of Strings
System.out.println(myTool); //Selenium
for (String tool: myTools){
System.out.println(tool);
}
-----------------------------
for (int i = 0; i < myTools.length; i++){
System.out.println(myTools[i]); // Print Array of strings
}
-----------------------------------------
Concatenating Strings
String str1 = "Selenium";
String str2 = " Testing";
System.out.println(str1 + str2); // Selenium Testing
System.out.println("Selenium" + " Testing"); // Selenium Testing
System.out.println(1 + 1 + "Selenium"); // 2Selenium
System.out.println("Selenium" + 1 + 1); // Selenium11
}
---------------------------------
String comparison
a) String comparing using (==) Operator
It supports 2-way comparing (true or false)
b) String comparing using equals() method
It supports 2-way comparing (true or false)
c) String comparing using compareTo() method
It supports 3-way comparing (0, > 1 too <1)
If Str1 = str2 therefore 0
If str1 > str2 therefore > 0
If str1 < str2 therefore < 0
-----------------------------------------
Example:
String str1 = "selenium";
String str2 = "SELENIUM";
String str3 = "SELENIUM";
String str4 = "zselenium";
// String comparing using == Relational Operator
System.out.println(str1 == str2); // false
System.out.println(str2 == str3); // true
// String comparing using equals method
System.out.println(str1.equals(str2)); // false
System.out.println(str2.equals(str3)); // true
// String comparing using compareTo() method
System.out.println(str1.compareTo(str2)); // Greater than 1 (Positive)
System.out.println(str2.compareTo(str3)); // 0
System.out.println(str1.compareTo(str4)); // less than 0 (Negative value)
---------------------------------
Java Arrays
Java Array is an Object that holds a fixed set out of values of a unmarried information type.
The length of an Array is established whe the Array is created.
Array length is fixed, coffee Array has cypher based index.
--------------------------------------
Declaration of Arrays
1st method
int abc []; //Creating Array
abc = novel int [4]; // Defining size
abc[0] = 10; // Assigning values
abc[1] = 20;
System.out.println(abc[0] + abc[1]); //30
-----------------------------
2nd method
int [] xyz = novel int [5];
xyz[0] = 10; // Assigning values
xyz[1] = 20;
System.out.println(xyz[1] + xyz[2]); //20
------------------------------
3rd method:
int [] xyz = {10, 20, 30, 40, 50};
System.out.println(xyz[1] + xyz[2]); //50
--------------------------------------------
Creating dissimilar types of Arrays
char [] abc = {'A', 'B', 'C', 'd'}; //Array of Characters
int [] xyz = {10, 20, 30, 40}; // Array of Integers
String [] a = {"Selenium", "UFT", "LoadRunner"}; //Array of Strings
boolean [] b = {true, false, false, true}; //Array of Boolean values
System.out.println(abc [1]); //B
System.out.println(xyz [2]);//30
System.out.println(a [1]); //UFT
System.out.println(b [1]); // false
-------------------------------------------------
Copy values an Array into simply about other Array
int [] array1 = {1, 2, 3, 4, 5};
int [] array2;
array2 = array1;
System.out.println(array2[1]); // 2
for (int i=0; i < array2.length; i++){
System.out.println(array2[i]);
}
--------------------------------------------------
Types of Arrays
Two types of Arrays
i) Single dimensional Array
ii) Multi dimensional Array
Ex:
int [] array1 = {1, 2, 3, 4, 5}; // Single dimensional Array
int [] [] array2 = {{1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}}; //Multi dimensional Array
System.out.println((array2[0][0]));//1
System.out.println((array2[1][0]));//2
System.out.println((array2[0][4]));//9
System.out.println((array2[1][3])); //8
Assignment:
print Multi dimensional array values using nested for loop.
------------------------------------------------------
Advantages of Arrays
> Using Arrays nosotros tin optimize the code, information tin hold upward retrieved easily
> nosotros tin instruct required information using index position.
-----------------------------
Disadvantages of Arrays
We tin shop fixed set out of elements only
It doesn't alter its size during execution.
-----------------------------------------
Java Methods
What is Method?
A laid of statements to perform an Operation.
Methods are also known equally procedures or functions.
In structured programming nosotros utilization functions (Built inwards too user defined)
In Object Oriented programming nosotros utilization Methods (Built inwards too user defined)
Usage of Methods:
---------------------
Whenever nosotros desire to perform operations several times therefore nosotros prefer methods.
Using methods nosotros tin trim the code size.
Types of Methods:
Basically nosotros accept ii types of methods inwards Java.
i) Built inwards methods
ii) User defined methods
-----------------------------------------
Built inwards Methods:
> Java has a library of classes too methods, organized inwards packages.
> In gild to utilization built inwards methods, nosotros import pre-defined packages/particular classes.
> java.lang bundle is automatically imported inwards whatever coffee program.
> Using import keyword nosotros tin import pre-defined packages.
-------------------------------
Categories of Built inwards methods:
a) String Methods
b) Character methods
c) Number methods
d) Array methods etc...
--------------------------------------
User defined methods inwards Java:
Two types of user defined methods:
i) Method without returning whatever value
ii) Method alongside returning a value.
--------------------------------------
Writing Methods (With Returning value)
Syntax:
modifier returnType methodname (parameters) {
Statements
----------
----------
}
Example:
public static int add together (int a, int b){
int result;
lawsuit = a + b;
provide result;
}
--------------------
calling a Method
dataType variablename = Method(Values)
----------------------------------
Example:
int a = add(123, 456);
System.out.println(a);
--------------------------------------
Example 2:
public static void primary (String [] args){
int a = xyz(2, 4, 6);
System.out.println(a);
}
// Write a method to multiply three numbers
world static int xyz (int a, int b, int c){
int result;
lawsuit = a * b * c;
provide result;
}
----------------------------------------------
Method without returning whatever value
Syntax:
modifier methodName(Parameters){
Statements
----------
---------
}
Example:
public static void primary (String [] args){
studentRank(700);
}
public static void studentRank(int marks){
if (marks >= 600){
System.out.println("Rank A1");
}
else if (marks >= 500){
System.out.println("Rank A2");
}
else {
System.out.println("Rank A3");
}
}
---------------------------------------
// Calling a method from simply about other shape (External Method)
public shape SeleniumClass extends ArrayExamples {
world static void primary (String [] args){
studentRank(650); // External method
}
-------------------------------------