Selenium Tutorial 6


Selenium Tutorial 6

Java Loop Statements
a) for..Loop
b) while...Loop
c) do while...Loop
d) enhanced for...Loop
--------------------------
a) for..Loop
It repeats a block of statements for a specified number 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) while loop
It repeats a block of statements while condition is true.

Syntax:

Initialization
while (condition) {
Statements
------------
-----------
increment/decrement
}

Example 1:
// Print 1 to 10 numbers using while loop
    int i = 1;
    while (i <= 10){
        System.out.println(i);
        i++;
        }
---------------------------------
example 2:

// Print 10 to 1 numbers using while loop
    int i = 10;
    while (i >= 1){
        System.out.println(i);
        i--;
        }
-------------------------------------------
c) do while loop
It repeats a block of statements while condition is true,
It executes a block of statements at least once irrespective of the condition.

Syntax:
Initialization
do
{
Statements
------
---------
increment/decrement
} while (condition);

Example:
---------------
// Print 1 to 10 numbers using do while loop
    int i = 1;
    do
    {
    System.out.println(i);
    i++;
    } while (i <=10);
-----------------------------
int i = 100;
    do
    {
    System.out.println(i);
    i++;
    } while (i <=10);

-------------------------------------
d) Enhanced for...loop
It executes all elements in 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 = new 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 in Java
What is String?
String is a sequence of characters written in double quotes.

String may have Alfa bytes, numbers and 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 as object in 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 comparison using (==) Operator

    It supports 2-way comparison (true or false)

b) String comparison using equals() method

    It supports 2-way comparison (true or false)

c) String comparison using compareTo() method

    It supports 3-way comparison (0, > 1 and <1)
    If Str1 = str2 then 0
    If str1 > str2 then > 0
    If str1 < str2 then < 0

-----------------------------------------
Example:
String str1 = "selenium";
    String str2 = "SELENIUM";
    String str3 = "SELENIUM";
    String str4 = "zselenium";
   
    // String comparison using == Relational Operator
    System.out.println(str1 == str2); // false
    System.out.println(str2 == str3); // true
   
    // String comparison using equals method
    System.out.println(str1.equals(str2)); // false
    System.out.println(str2.equals(str3)); // true
   
    // String comparison 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 number of values of a single data type.

The length of an Array is established whe the Array is created.

Array length is fixed, java Array has zero based index.
--------------------------------------
Declaration of Arrays
1st method
int abc []; //Creating Array
    abc = new int [4]; // Defining size
   
    abc[0] = 10; // Assigning values
    abc[1] = 20;
    System.out.println(abc[0] + abc[1]); //30
-----------------------------
2nd method

int [] xyz = new 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 different 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 another 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 we can optimize the code, data can be retrieved easily

> we can get required data using index position.
-----------------------------
Disadvantages of Arrays
We can store fixed number of elements only
It doesn't change its size during execution.
-----------------------------------------
Java Methods
What is Method?

A set of statements to perform an Operation.

Methods are also known as procedures or functions.

In structured programming we use functions (Built in and user defined)

In Object Oriented programming we use Methods (Built in and user defined)

Usage of Methods:
---------------------
Whenever we want to perform operations several times then we prefer methods.

Using methods we can reduce the code size.

Types of Methods:
Basically we have two types of methods in Java.

i) Built in methods

ii) User defined methods
-----------------------------------------
Built in Methods:
> Java has a library of classes and methods, organized in packages.

> In order to use built in methods, we import pre-defined packages/particular classes.

> java.lang package is automatically imported in any java program.

> Using import keyword we can import pre-defined packages.
-------------------------------
Categories of Built in methods:
a) String Methods

b) Character methods

c) Number methods

d) Array methods etc...
--------------------------------------
User defined methods in Java:
Two types of user defined methods:

i) Method without returning any value

ii) Method with returning a value.
--------------------------------------
Writing Methods (With Returning value)
Syntax:

modifier returnType methodname (parameters) {
Statements
----------
----------
}

Example:

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

--------------------
calling a Method

dataType variablename = Method(Values)
----------------------------------
Example:
int a = add(123, 456);
    System.out.println(a);
--------------------------------------
Example 2:

public static void main (String [] args){
    int a = xyz(2, 4, 6);
    System.out.println(a);
    }

// Write a method to multiply 3 numbers

    public static int xyz (int a, int b, int c){
        int result;
        result = a * b * c;
        return result;
    }
----------------------------------------------
Method without returning any value
Syntax:

modifier methodName(Parameters){
Statements
----------
---------
}

Example:
public static void main (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 another class (External Method)
public class SeleniumClass extends ArrayExamples {
    public static void main (String [] args){
        studentRank(650); // External method
    }
-------------------------------------

1 comments:

I am getting .exe file is not a valid win32 application when installing.. How can i correct it?

Post a Comment