Selenium Tutorial 5
Java Variables Part-2
Java Variables Declaration
Example:
package JavaExamples;
public shape VariablesExamples {
world static void master copy (String [] args){
// dataType variableName;
int a;
a = 10;
// dataType VariableName = Value;
int b = 20;
// Declaration of multiple variables inward a statement
int c, d, e;
c =30;
d =40;
e = 50;
// Declaration of multiple variables too 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 nosotros add together to those definitions to alter their meanings.
a) Access Modifiers
b) Non-access modifiers
-----------------------
a) Access Modifiers
There are 4 types of access modifiers
We role Access modifiers to define access command for classes, methods too variables.
1) private
The someone access modifier is accessible solely within class.
Ex:
class Abc {
private int a = 100;
.
.
----------------
2) default
If nosotros don't role whatever modifier therefore it is treated equally default,
this tin live accessible solely within package.
Ex:
package abcd;
class Sample {
int a;
.
.
----------
3) protected
The protected access modifier is accessible within package, exterior of the parcel but through Inheritance only.
4) public
public access modifier is accessible everywhere.
Ex:
public shape Abc{
.
.
------------------------------------
b) Non Access Modifiers
1) static
static modifier to practice class, method, variables
Ex:
class A{
static String get upwardly ="Selenium"
.
.
-------------------
2) final
final modifier for finalizing the implementation of classes, methods too variables.
ex:
class Abc{
final int a = 100;
.
.
-------------------
3) abstract
abstract modifier is to practice abstract classes, methods.
ex:
abstract Sample{
.
.
}
-----------------------------------------
Java Operators
Operators are used to perform mathematical, Comparison too 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 too +=
a = 10;
a += 20;
3) Subtract too Assign -=
a = 10;
a -= 5;
4) Multiply too Assign *=
a = 10;
a *= 5;
------------------------
public static void master copy (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 add-on too string concatenation)
2) Subtraction - (fro subtraction too negation)
3) Multiplication *
4) Division /
5) Modules %
6) Increment ++
7) Decrement --
-----------------------------
Example:
public static void master copy (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 inward Computer Programming
i) Value based Result
3 + v = 8
2 * vii = 14
ii) Boolean / Logical Result
True or false
iii) Constant based Result
-------------------------------------
Relational operators supply Boolean / Logical result
1) ==
2) !=
3) >
4) >=
5) <
6) <=
----------------------
Example:
public static void master copy (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 master copy (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 master copy (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 too for Error handling.
Two types of Conditional statements inward Java
1) if statement
2) Switch statement
------------------------------
Types of Conditions
1) Single condition
Ex:
if (a > b) {
----
----
2) Compound status
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 status is true.
Syntax:
if (Condition) {
Statements
---------
---------
}
Example:
public static void master copy (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 chemical compound status 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 status is true. otherwise executing about other block of statements.
Syntax:
if (Condition) {
Statements
----------
--------
}
else {
statements
---------
--------
}
Example:
public static void master copy (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 amid several Alternates (else if structure)
Syntax:
if (condition) {
Statements
------
---------
}
else if (condition) {
Statements
------
---------
}
else if (condition) {
Statements
------
---------
}
else
{
statements
----------
--------
}
Example:
public static void master copy (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 to a greater extent than than i status is truthful (Nested if).
Syntax:
if (condition) {}
if (condition) {}
if (condition) {
Statements
--------
---------
}
else
{
Statements
---------
---------
}
Example:
public static void master copy (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 amid several alternates (using Switch illustration structure).
Syntax:
switch (expression) {
case value:
Statements
-------
break;
case value:
Statements
-------
break;
case value:
Statements
-------
break;
default
Statements
-------
------
}
Example:char degree = 'X';
switch (grade){
illustration 'A':
System.out.println ("Excellent");
break;
illustration 'B':
System.out.println ("Well Done");
break;
illustration 'C':
System.out.println ("Better");
break;
default:
System.out.println("Invalid Grade");
}
---------------------------------------------------------
Java Loop statements
Whenever nosotros desire to execute a block of statements several times therefore nosotros role Loop structures.
There are 4 types of loop construction inward Java.
1) for Loop
2) piece Loop
3) do...while Loop
4) Enhanced for Loop
-------------------------
1) for Loop
It repeats a block of statements for a specified disclose of times.
Syntax:
for(stratvalue; endValue; increment/decrement) {
Statements
---------
--------
}
Example:
for (int i=1; i<=10; i++)
{
System.out.println(i);
}
Example 2:
// impress 10 to 1 Numbers using For Loop.
for (int i=10; i>=1; i--)
{
System.out.println(i);
}
Example 3:// impress every tenth Number upwardly to 100.
for (int i=10; i<=100; i=i+10)
{
System.out.println(i);
}
--------------------------------------
2) piece Loop
It repeats a block of statements piece status is true.
Syntax:
Initialization
while (condition) {
statements
------
-------
increment/decrement
}
Example:// impress 1 to 10 Numbers using While Loop.
int i = 1;
piece (i <=10){
System.out.println(i);
i++;
}
Example 2:// impress 10 to 1 Numbers using While Loop.
int i = 10;
piece (i >= 1){
System.out.println(i);
i--;
}
Example 3:// impress every tenth disclose upwardly to 100 using While Loop.
int i = 10;
piece (i <= 100){
System.out.println(i);
i= i+10;
}
----------------------------------------
**Java Loop Structures incomplete
Selenium Tutorial vi
Java Variables Part-2
Java Variables Declaration
Example:
package JavaExamples;
public shape VariablesExamples {
world static void master copy (String [] args){
// dataType variableName;
int a;
a = 10;
// dataType VariableName = Value;
int b = 20;
// Declaration of multiple variables inward a statement
int c, d, e;
c =30;
d =40;
e = 50;
// Declaration of multiple variables too 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 nosotros add together to those definitions to alter their meanings.
a) Access Modifiers
b) Non-access modifiers
-----------------------
a) Access Modifiers
There are 4 types of access modifiers
We role Access modifiers to define access command for classes, methods too variables.
1) private
The someone access modifier is accessible solely within class.
Ex:
class Abc {
private int a = 100;
.
.
----------------
2) default
If nosotros don't role whatever modifier therefore it is treated equally default,
this tin live accessible solely within package.
Ex:
package abcd;
class Sample {
int a;
.
.
----------
3) protected
The protected access modifier is accessible within package, exterior of the parcel but through Inheritance only.
4) public
public access modifier is accessible everywhere.
Ex:
public shape Abc{
.
.
------------------------------------
Modifier | With inward 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 practice class, method, variables
Ex:
class A{
static String get upwardly ="Selenium"
.
.
-------------------
2) final
final modifier for finalizing the implementation of classes, methods too variables.
ex:
class Abc{
final int a = 100;
.
.
-------------------
3) abstract
abstract modifier is to practice abstract classes, methods.
ex:
abstract Sample{
.
.
}
-----------------------------------------
Java Operators
Operators are used to perform mathematical, Comparison too 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 too +=
a = 10;
a += 20;
3) Subtract too Assign -=
a = 10;
a -= 5;
4) Multiply too Assign *=
a = 10;
a *= 5;
------------------------
public static void master copy (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 add-on too string concatenation)
2) Subtraction - (fro subtraction too negation)
3) Multiplication *
4) Division /
5) Modules %
6) Increment ++
7) Decrement --
-----------------------------
Example:
public static void master copy (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 inward Computer Programming
i) Value based Result
3 + v = 8
2 * vii = 14
ii) Boolean / Logical Result
True or false
iii) Constant based Result
-------------------------------------
Relational operators supply Boolean / Logical result
1) ==
2) !=
3) >
4) >=
5) <
6) <=
----------------------
Example:
public static void master copy (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 master copy (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 master copy (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 too for Error handling.
Two types of Conditional statements inward Java
1) if statement
2) Switch statement
------------------------------
Types of Conditions
1) Single condition
Ex:
if (a > b) {
----
----
2) Compound status
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 status is true.
Syntax:
if (Condition) {
Statements
---------
---------
}
Example:
public static void master copy (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 chemical compound status 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 status is true. otherwise executing about other block of statements.
Syntax:
if (Condition) {
Statements
----------
--------
}
else {
statements
---------
--------
}
Example:
public static void master copy (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 amid several Alternates (else if structure)
Syntax:
if (condition) {
Statements
------
---------
}
else if (condition) {
Statements
------
---------
}
else if (condition) {
Statements
------
---------
}
else
{
statements
----------
--------
}
Example:
public static void master copy (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 to a greater extent than than i status is truthful (Nested if).
Syntax:
if (condition) {}
if (condition) {}
if (condition) {
Statements
--------
---------
}
else
{
Statements
---------
---------
}
Example:
public static void master copy (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 amid several alternates (using Switch illustration structure).
Syntax:
switch (expression) {
case value:
Statements
-------
break;
case value:
Statements
-------
break;
case value:
Statements
-------
break;
default
Statements
-------
------
}
Example:char degree = 'X';
switch (grade){
illustration 'A':
System.out.println ("Excellent");
break;
illustration 'B':
System.out.println ("Well Done");
break;
illustration 'C':
System.out.println ("Better");
break;
default:
System.out.println("Invalid Grade");
}
---------------------------------------------------------
Java Loop statements
Whenever nosotros desire to execute a block of statements several times therefore nosotros role Loop structures.
There are 4 types of loop construction inward Java.
1) for Loop
2) piece Loop
3) do...while Loop
4) Enhanced for Loop
-------------------------
1) for Loop
It repeats a block of statements for a specified disclose of times.
Syntax:
for(stratvalue; endValue; increment/decrement) {
Statements
---------
--------
}
Example:
for (int i=1; i<=10; i++)
{
System.out.println(i);
}
Example 2:
// impress 10 to 1 Numbers using For Loop.
for (int i=10; i>=1; i--)
{
System.out.println(i);
}
Example 3:// impress every tenth Number upwardly to 100.
for (int i=10; i<=100; i=i+10)
{
System.out.println(i);
}
--------------------------------------
2) piece Loop
It repeats a block of statements piece status is true.
Syntax:
Initialization
while (condition) {
statements
------
-------
increment/decrement
}
Example:// impress 1 to 10 Numbers using While Loop.
int i = 1;
piece (i <=10){
System.out.println(i);
i++;
}
Example 2:// impress 10 to 1 Numbers using While Loop.
int i = 10;
piece (i >= 1){
System.out.println(i);
i--;
}
Example 3:// impress every tenth disclose upwardly to 100 using While Loop.
int i = 10;
piece (i <= 100){
System.out.println(i);
i= i+10;
}
----------------------------------------
**Java Loop Structures incomplete
Selenium Tutorial vi