Selenium Tutorial 5


Selenium Tutorial 5

Java Variables Part-2 

Java Variables Declaration
 

Example:
package JavaExamples;

public class VariablesExamples {

    public static void main (String [] args){
        // dataType variableName;
        int a;
        a = 10;
        // dataType VariableName = Value;
        int b = 20;
        // Declaration of multiple variables in a statement
        int c, d, e;
        c =30;
        d =40;
        e = 50;
        // Declaration of multiple variables and assigning values
        int f=60, g=70, h=90;
        int j;
        j = a; // Reading
        char x ='A';
        double y = 2.3456;
        String z = "Selenium";
       
        System.out.println (a);
        System.out.println (b);
        System.out.println (c);
        System.out.println (d);
        System.out.println (e);
        System.out.println (f);
        System.out.println (g);
        System.out.println (h);
        System.out.println (x);
        System.out.println (y);
        System.out.println (z);
        System.out.println (j);
    }
}
---------------------------------------
Java Modifiers
 

Modifiers are keywords that we add to those definitions to change their meanings.

a) Access Modifiers

b) Non-access modifiers
-----------------------
a) Access Modifiers 

There are four types of access modifiers

We use Access modifiers to define access control for classes, methods and variables.

1) private
The private access modifier is accessible only within class.

Ex:

class Abc {
private int a = 100;
.
.
----------------
2) default
If we don't use any modifier then it is treated as default,
this can be accessible only within package.

Ex:

package abcd;

class Sample {

int a;

.
.
----------
3) protected
The protected access modifier is accessible within package, outside of the package but through Inheritance only.

4) public
public access modifier is accessible everywhere.

Ex:

public class Abc{
.
.
------------------------------------


Modifier
With in Class
Within Package
Outside of the Package(By Sub Class)
Outside of the Package
private
Y
N
N
N
default
Y
Y
N
N
protected
Y
Y
Y
N
public
Y
Y
Y
Y

b) Non Access Modifiers 
1) static
static modifier to create class, method, variables

Ex:

class A{

static String name ="Selenium"
.
.
-------------------
2) final
final modifier for finalizing the implementation of classes, methods and variables.

ex:

class Abc{

final int a = 100;
.
.
-------------------
3) abstract
abstract modifier is to create abstract classes, methods.

ex:

abstract Sample{
.
.
}
-----------------------------------------
Java Operators
 

Operators are used to perform mathematical, Comparison and Logical operations.

Important categories of Operators
a) Assignment Operators

b) Arithmetic operators

c) Relational Operators

d) Logical operators
etc...
-----------------------
a) Assignment Operators
1) Assignment operator =

a = 10;

2) Add and +=

a = 10;

a += 20;

3) Subtract and Assign -=

a = 10;

a -= 5;

4) Multiply and Assign *=

a = 10;

a *= 5;
------------------------
public static void main (String [] args){
        int a = 10;
        System.out.println (a); // 10
        a += 10;
        System.out.println (a); // 20
        a -= 10;
        System.out.println (a); // 10
        a *= 5;
        System.out.println (a); //50
    }
--------------------------------------
b) Arithmetic operators
1) Addition + (for addition and string concatenation)

2) Subtraction - (fro subtraction and negation)

3) Multiplication *

4) Division /

5) Modules %

6) Increment ++

7) Decrement --
-----------------------------
Example:
public static void main (String [] args){
        int a= 10, b = 5;
        String c ="Selenium", d = "Testing";
       
    System.out.println ("Addition of a, b is: " + (a+b)); // 15
    System.out.println ("Concatenation of c, d is: " + (c+d)); // SeleniumTesting
   
    System.out.println ("Subtraction of a, b is: "+ (a-b)); //5
    System.out.println ("Multiplication of a, b is: "+(a * b)); //50
    System.out.println ("Division of a, b is: "+ (a/b)); //2
    System.out.println ("Modulus of a, b is: "+ (a%b)); //0
    b = 5;
    a = ++b;
    System.out.println (a); // 5
    b = 5;
    a = --b;
    System.out.println (a); //4
    b = 5;
    a = b+4;
    System.out.println (a); // 9
   
    }
--------------------------------------
c) Relational operators
Types of Result in Computer Programming

i) Value based Result

3 + 5 = 8

2 * 7 = 14

ii) Boolean / Logical Result

True or false

iii) Constant based Result
-------------------------------------
Relational operators return Boolean / Logical result

1) ==

2) !=

3) >

4) >=

5) <

6) <=
----------------------
Example:

public static void main (String [] args){
        int a=10, b=20;
        System.out.println ("a > b is "+ (a > b)); //False
        System.out.println ("a >= b is "+ (a >= b)); //False
        System.out.println ("a == b is "+ (a == b)); //False
       
        System.out.println ("a < b is "+ (a < b)); //True
        System.out.println ("a <= b is "+ (a <= b)); //True
        System.out.println ("a != b is "+ (a != b)); //True
        }
----------------------------
d) Logical Operators
1) Logical Not operator !

2) Logical And operator &&

3) Logical Or Operator ||
------------
Result Criteria

1) Not operator
----------------------------
Operand 1    Operand 2    Result
-------------------------------------
true              true             false
true              false            true
false             true             true
false             false            true
-----------------------------------       
2) And operator
----------------------------
Operand 1    Operand 2    Result
-------------------------------------
true               true           true
true              false           false
false             true            false
false             false           false
---------------------------------------
3) Or operator
----------------------------
Operand 1    Operand 2    Result
-------------------------------------
true              true            true
true              false           true
false             true            true
false            false            false
------------------------------------
Example:

public static void main (String [] args){
        boolean a= true, b=false;
        System.out.println("! (a && b) is: " + ! (a && b )); //True
        System.out.println("(a && b) is: " + (a && b )); //False
        System.out.println("(a || b) is: " + (a || b )); //True
        }
--------------------------------------------   
Ex 2:
public static void main (String [] args){
        int a = 100, b = 500, c = 70;
       
        if ((a > b) && (a > c)) {
            System.out.println ("A is a Big Number");
        }
        else
        {
            System.out.println ("A is Not a Big Number");
        }
----------------------------------------------
Java Flow Control
    Conditional Statements
    Loop Statements

Java Conditional Statements   
 

Conditional statements are used to insert verification points and for Error handling.

Two types of Conditional statements in Java
1) if statement

2) Switch statement
------------------------------
Types of Conditions
1) Single condition
Ex:

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

2) Compound condition
Ex:

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

3) Nested Condition
if (a > b){}
 if (a < c) {}
  if (a < d) {
----
----
}
------------------------------
Usage of Conditional Statements
1) Executing a block of statements when condition is true.
Syntax:

if (Condition) {
Statements
---------
---------
}

Example:
public static void main (String [] args){
        int a, b;
        a =10;
        b =5;
       
        if (a > b) {
            System.out.println("A is a Big Nuber");
        }
------------------------------------
2) Executing a block of statements when compound condition is true

Syntax:

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

Example:
int a, b, c = 2;
        a =10;
        b =5;
       
        if ((a > b) && (a > c)) {
            System.out.println("A is a Big Nuber");
        }
-------------------------------------------
3) Executing a block of statements when condition is true. otherwise executing another block of statements.
Syntax:

if (Condition) {
Statements
----------
--------
}
else {
statements
---------
--------
}

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");
        }
        else {
            System.out.println("B is a Big Number");
        }
-----------------------------------------
4) Decide among several Alternates (else if structure)
Syntax:

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

Example:

public static void main (String [] args){
        int a = -100;
               
        if ((a >= 1) && (a < 100)) {
            System.out.println("A is a Small Number");
        }
        else if ((a > 100) && (a <= 1000)) {
            System.out.println("A is a Medium Number");
            }
        else if (a > 1000) {
            System.out.println("A is a Big Number");
            }
        else
        {
            System.out.println("A is either Zero or negative value");
        }
--------------------------------------------       
5) Executing a block of statements when more than one condition is true (Nested if).
Syntax:

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

Example:

public static void main (String [] args){
        int a =10, b =7, c = 5, d = 13;
               
        if (a > b){}
          if (a > c) {}
           if (a > d){
               System.out.println("A is a Big Number");
           }
           else {
               System.out.println("A is Not a Big Number");
           }
---------------------------------------
6) Decide among several alternates (using Switch case structure).
Syntax:
switch (expression) {

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

Example:char grade = 'X';
       
        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 Loop statements
 

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

There are four types of loop structure in Java.

1) for Loop

2) while Loop

3) do...while Loop

4) Enhanced for Loop
-------------------------
1) for Loop
It repeats a block of statements for a specified number of times.

Syntax:
for(stratvalue; endValue; increment/decrement) {
Statements
---------
--------
}

Example:

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 every 10th Number up to 100.
       
        for (int i=10; i<=100; i=i+10)
        {
            System.out.println(i);
        }
--------------------------------------
2) while Loop
It repeats a block of statements while condition is true.

Syntax:

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

Example:// 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--;
        }
Example 3:// print every 10th number up to 100 using While Loop.
       
        int i = 10;
        while (i <= 100){
            System.out.println(i);
            i= i+10;
        }
----------------------------------------

**Java Loop Structures incomplete  
Selenium Tutorial 6

1 comments:

In increment operator system.out.println(a); //6

Post a Comment