Java Tutorial 3

Java Tutorial 3
(Java Conditional Statements, Java Loop Statements, String handling in Java, Java Arrays Part-1)

Java Programming Fundamentals and OOPS concepts
--------------------------------------------------
Java Environment Setup and write First Java Program

I) Comments in Java

II) Java Data Types

III) Java Modifiers

IV) Variables in Java

V) Operators in Java
--------------------------------
VI) Java Conditional Statements
 

a) Types of Conditional statements
 

i) if statement

ii) switch statement
-----------------------------
b) Types of Conditions

i) Single Condition

Ex:

if (a > b) {
Statements
-------
--------
.
.

}

ii) Compound Condition

Ex:

if ((a > b) && or || (a < c)) {
---------
----------
.
.

}
iii) Nested Condition

if (a > b) {
 if (a < c) {
  if (a < d) {
---------
------
}
}
}
-------------------
c) Usage of Conditional statements in Selenium Test Automation
 

i) To insert Verification points

ii) For Error handling
-------------------------------
d) Usage of Conditional statements
i) Execute a block of statements when condition is True.
Syntax:
if (condition) {
Statements
--------
------
-------
}
One way (Positive) Condition
Example:

public static void main (String [] args){
        int a, b;
        a = 10; b= 50;
       
        if (a > b) {
            System.out.println("A is a Big Number");
        }
    }
}
---------------------------------------
One way (Negative) Condition

public static void main (String [] args){
        int a, b;
        a = 10; b= 5;
       
        if (! (b > a)) {
            System.out.println("A is a Big Number");
        }
    }
}
-----------------------------------------
ii) Execute a block of statements when condition is true; otherwise execute another block of statements.
 

Syntax:

if (condition) {
Statements
---------
-------
------
}
else
{
Statements
---------
--------
---------
}

Example:

public static void main (String [] args){
        int a, b;
        a = 10; b= 10;
       
        if (a > b) {
            System.out.println("A is a Big Number");
        }
        else {
            System.out.println("B is a Big Number");
        }
    }
}
--------------------------------------------
3) Execute a block of statements when a Compound condition is true. 

Syntax:

if ((condition1) && or || (condition2)) {
Statements
----------
----------
----------
}

Example:

public static void main (String [] args){
        int a, b, c;
        a = 10; b= 7; c = 5;
       
        if ((a > b) && (a > c)) {
            System.out.println("A is a Big Number");
        }
    }
--------------------------------
public static void main (String [] args){
        int a, b, c;
        a = 10; b= 7; c = 15;
       
        if ((a > b) || (a > c)) {
            System.out.println("A is a Big Number");
        }
    }
------------------------------------
4) Decide among several alternates (else if)
 

Syntax:

if (condition) {
Statements
---------
---------
--------
}
else if (condition) {
Statements
---------
---------
--------
}
else if (condition) {
Statements
---------
---------
--------
}
else {
Statements
----------
---------
}

Example:

Requirement:
Verify the range of a value

Conditions:
if the value is in between 1 to 100 then display value is a Small number
if the value is in between 101 to 1000 then display value is a Medium number
if the value is in between 1001 to 10000 then display value is a Big number
Otherwise display the value is either zero or negative value

Program:

    public static void main (String [] args){
        int val = -100;
    if ((val >= 1) && (val <=100)) {
        System.out.println("Value is a Small Number");
    }
    else if ((val > 100 ) && (val <=1000)) {
        System.out.println("Value is a Medium Number");
    }
    else if ((val > 1000 ) && (val <=10000)) {
        System.out.println("Value is a Big Number");
    }
    else {
        System.out.println("Value is Either Zero or Negaive Number");
    }
}
--------------------------------------
v) Execute a Block of statements when more than one condition is True (Nested If)

Syntax:

if (condition) {
 if (condition) {
  if (condition {
Statements
--------
--------
}
}
}

Example:

public static void main (String [] args){
        int a = 100, b = 90, c = 70, d = 50;
   
        if (a > b){
         if (a > c){
          if (a > d) {
              System.out.println("A is a Big Number");
          }
         }
        }
}
------------------------------
public static void main (String [] args){
        int a = 100, b = 90, c = 70, d = 50;
   
        if (((a > b) && (a > c) && (a > d))) {
          System.out.println("A is a Big Number");
          }
}
----------------------------------
Difference between Compound and Nested Conditions

If it is Compound condition we can write single else part only.

If it is Nested condition we can write multiple else parts.
----------------------------------------------------
vi) Decide among several alternates (switch statement)
 

Syntax:

switch (expression) {

case value:
Statements
----------
----------
break;
case value:
Statements
----------
----------
break;
case value:
Statements
----------
----------
break;
default:
Statements
----------
-----
}

Example:

public static void main (String [] args){
        char grade = 'Z';
       
        switch (grade){
        case 'A':
            System.out.println("Excellent");
            break;
        case 'B':
            System.out.println("Well Done");
            break;
        case 'C':
            System.out.println("Better");
            break;
        default:
            System.out.println("Invalid Grade");
        }
}
------------------------------------------------------
Java Flow Control Statements
    Conditional Statements
    Loop Statement

i) We can use conditional statements only in our programs.

ii) We can use Loop statements only in our programs.

iii) We can use Conditional statements and Loop statements together.
---------------------------------------------------------------
VII) Loop Statements
 

Whenever we want execute a block of statements multiple times then we use Loop structures.

There are four types of Loop structures in Java.

i) for loop

ii) while loop

iii) do while loop

iv) Enhanced for loop
---------------------
i) for loop
 

Description:

It repeats a block of statements for a specified number of times.

Syntax:

for (startvalue; endvalue; increment/Decrement){
Statements
----------
----------
----------
}

Example: Print 1 to 10 Numbers
public static void main (String [] args){
       
        for (int i = 1; i <= 10; i++){
            System.out.println(i);
        }
        }
---------------------------------------------------
Example: Print 10 to 1 Numbers

public static void main (String [] args){
       
        for (int i = 10; i >= 1; i--){
            System.out.println(i);
        }
        }
-----------------------------------
Example: Print 1 to 10 Numbers, except 4th and 7th numbers
public static void main (String [] args){
       
        for (int i = 1; i <= 10; i++){
            if ((i != 4) && (i != 7)) {
            System.out.println(i);
            }
        }
        }
-------------------------------------------------
ii) While loop

It repeats a block of statements while condition is True.

Syntax:

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

Example1: Print 1 to 10 Numbers

public static void main (String [] args){
        int i = 1;
        while (i <= 10){
            System.out.println(i);
            i++;
        }
        }
---------------------------------
Example2: print 10 to 1 Numbers
public static void main (String [] args){
        int i = 10;
        while (i >= 1){
            System.out.println(i);
            i--;
        }
        }
-------------------------------------------
Example3: print every 10th Number up to 100
    public static void main (String [] args){
        int i = 10;
        while (i <= 100){
            System.out.println(i);
            i = i + 10;
        }
        }
---------------------------------------------------
Example4: Print 1 to 10 Numbers, except 4th and 7th numbers
public static void main (String [] args){
        int i = 1;
        while (i <= 10){
            if ((i != 4) && (i != 7)){
            System.out.println(i);
            }
            i++;
        }
                }
----------------------------------------------------       
iii) do while loop

It repeats a block of statements while condition is true.
It executes statements at least once irrespective of the condition.

Difference between while loop and do while loop:
---------------------------------------------
while loop first checks the condition, if condition is true then it will execute the statements.

do while loop first executes the statements then it will check the condition, if condition
is true, it will continue otherwise come out from the loop.
------------------------------------------------------
Syntax:

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

Example:

public static void main (String [] args){
        int i = 1;
        do
        {
        System.out.println(i);   
            i++;
        }
        while (i <= 10);
        }
---------------------------------
public static void main (String [] args){
        int i = 11;
        do
        {
        System.out.println(i);   
            i++;
        }
        while (i <= 10);
        }
--------------------------------------
iv) Enhanced for loop
It executes all elements in an Array.

Syntax:

Array declaration
for (declaration: expression/Array) {
Statements
---------
}

Example1:

public static void main (String [] args){
        String [] languages = {"C", "COBOL", "Java"};
        for (String lang: languages){
            System.out.println(lang);
        }
        }
Example2:
public static void main (String [] args){
        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);
        }
        }
------------------------------------------------------------
VIII) String Handling in Java
 

What is String?

String is a sequence of character written double quotes.

String may have Alfa bytes, numbers and special characters.

Example:
System.out.println("Selenium Testing"); // selenium Testing
System.out.println("123 Selenium"); // 123 Selenium
System.out.println("Selenium*&123Testing");//Selenium*&123Testing

Creating strings:

String is considered Object in Java.

Example:
public static void main (String [] args){
    String myTool = "Selenium"; // String variable
    String [] myTools =    {"UFT", "Selenium", "LoadRunner"}; //Array of Strings
   
    System.out.println(myTool);//Selenium
   
    for (String tool: myTools){
        System.out.println(tool);//Print Array of Strings
    }
}
---------------------------------
Concatenating Strings:

public static void main (String [] args){
    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:

i) String comparison using (==) operator.
It supports 2-way comparison (true/false)

ii) String comparison using equals() method
It supports 2-way comparison (true/false)

iii) String comparison using compareTo() method
It supports 3-way comparison (0, >1, <1)

if str1 == str2 then 0
if str1 > str2 then > 1 (Positive value)
if str1 < str2 then < 1 (Negative value)

ANSI character codes

A to Z (65 to 90)
a to z (97 to 122)
0 to 9 (48 to 57)

Example:

public static void main (String [] args){
    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(str1 == str3);//true
   
    // String comparison using equal() method
    System.out.println(str1.equals(str2));//false
    System.out.println(str1.equals(str3)); // true
   
    // String comparison using compareTo() method
    System.out.println(str1.compareTo(str3)); //0
    System.out.println(str2.compareTo(str1)); //Positive value
    System.out.println(str1.compareTo(str4)); //Negative value
        }
--------------------------------------------------
IX) Java Arrays
 

> Java Array is an object that holds fixed number of values of a single data type.

> The length of an Array is established when the Array is created.

> Array length is fixed, Java Array has 0 based index.

Declaration of Arrays:

1st method:

int abc [] ; // Creating Array

abc = new int [4]; //Defining size

abc [0] = 10; // Assigning Value
abc [1] = 20;
abc [2] = 30;
abc [3] = 40;
System.out.println(abc[0] + abc[2]); //40
----------------------------------------
2nd method

int [] abc = new int [5]; // Creating Array with Size

abc[0] =10; //Assigning value
abc[1] =20;

System.out.println(abc[0] + abc [1]);//30
------------------------------------------------
3rd Method

// Creating Array and Assigning values

int [] abc = {10, 20, 30, 40, 50};

System.out.println(abc[1] + abc[3]);// 60
int a = abc.length;
System.out.println(a);//5
--------------------------------------------
Creating different type of Arrays


public static void main (String [] args){
    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
   
    System.out.println(abc[1]); //B
    System.out.println(xyz[2]);//30
    System.out.println(a[1]); // UFT
    System.out.println(b[1]); //false
    }

0 comments:

Post a Comment