Java Quick Tutorial for Selenium Part 2

Java Quick Tutorial for Selenium Part 2

In Java Quick Tutorial Part 1 I explained,

Java Environment Setup & Verify

A) Java Fundamentals
i) Comments inward Java
ii) Java Data Types
iii) Java Modifiers
iv) Variables
v) Operators
vi) Conditional Statements
vii) Loop Statements
viii) String Handling inward Java
ix) Arrays inward Java
x) Java IO Operations
----------------------------------------------------- 
Java for Selenium Part 2

xi) File Handling
xii) Java Built-in Methods 
xiii) Java User defined Methods
xiv) Java Exception Handling

B) Java OOPS (Object Oriented Programming System)
i) Inheritance
ii) Polymorphism
iii) Abstraction
iv) Encapsulation
--------------------------------------- 
xi) File Handling

Using File Class nosotros tin grip Computer files.

Example:

1) Create a Folder

File fileObject = novel File("C:/Users/gcreddy/Desktop/ABC");
fileObject.mkdir();
--------------------------------------- 
xii) Java Methods 

A Java Method is a educate of statements that are grouped together to perform an operation.

Methods are too known equally Functions.

In structured programming (Ex: C Language) nosotros utilization Functions (Built-in together with User defined)

In Object Oriented Programming (Ex: Java Language) nosotros utilization Methods (Built-in together with User defined)
-------------------------------------------
When nosotros pick out Methods?

Whenever nosotros desire perform whatever functioning multiple times together with then nosotros pick out methods.

Advantages of Methods

Code Reusability, using methods nosotros tin cut back the projection code size.

Code Maintenance is easy.
--------------------------------------- 
ii) Types Methods inward Java

Two types of Methods

1) Built inward /Pre-defined Methods

2) User defined Methods
--------------------------------------- 
Examples for Built inward Methods

equals() Method

Compares 2 strings together with supports 2-way comparison

Example:

public static void main(String[] args) {
String str1 = "selenium";
String str2 ="SELENIUM";
String str3 ="seleniuma";
String str4 = "selenium";

System.out.println(str1.equals(str2));//false
System.out.println(str1.equals(str4));//true
--------------------------------------- 
round()
Rounds the value to nearest integer.

public static void main(String[] args) {
double a =10.234;
double b =-10.784;

System.out.println(Math.round(a));//10
System.out.println(Math.round(b));//-11
}
--------------------------------------- 
isAlphabetic()

Checks conditions the value is alfa byte or not?

public static void main(String[] args) {
char a ='Z';
char b ='1';

System.out.println(Character.isAlphabetic(a));//true
System.out.println(Character.isAlphabetic(b));//false
System.out.println(Character.isAlphabetic('B'));//true
System.out.println(Character.isAlphabetic('a'));//true
System.out.println(Character.isAlphabetic('*'));//false
--------------------------------------- 
xiii) User Defined methods inward Java

Two types of user defined methods

1) Method without returning whatever value
a) Calling Methods yesteryear invoking Object
b) Calling methods without invoking Object

2) Method amongst returning a Value
a) Calling Methods yesteryear invoking Object
b) Calling methods without invoking Object

Examples:

//User defined methods
public int multiply(int a, int b, int c){
int effect = a*b*c;
return result;
}
public static void main(String[] args) {
//Create Object
JavaMethods abc = novel JavaMethods();

//Call methods
int x = abc.multiply(10, 25, 35);
System.out.println(x);
---------------------------------
//Create a method without returning whatever value
public static void studentRank(int marks){
if (marks >= 600){
System.out.println("Rank A");
}
else if (marks >=500){
System.out.println("Rank B");
}
else{
System.out.println("Rank C");
}
}

public static void main(String[] args) {
//Call Methods
JavaMethods.studentRank(650);
}
--------------------------------------- 
xiv) Java Exception Handling

> An Exception is an lawsuit that occurs during execution of a programme when normal execution of a program
    is interrupted.

> Exception treatment is a machinery to grip exceptions.

Common Scenarios where exceptions may occur

1) Scenario where ArithemeticException occurs

2) Scenario where NullPointerException occurs.

3) Scenario where NumberFormatException occurs

4) Scenario where ArrayIndexOutofBounds exception occurs

Example:

public static void main(String[] args) {
int a =10;
int b =0;
int effect = a/b;
System.out.println(result);
System.out.println("Hello Java");
System.out.println("Hello Selenium");
}
--------------------------------------- 
B) Java OOPS

Four fundamentals of OOPS:

i) Inheritance

ii) Polymorphism

iii) Abstraction

iv) Encapsulation
--------------------------------------- 
i) Inheritance

> It is a procedure of Inheriting (reusing) the degree members (Variables together with Methods) from ane degree to another.

> Non static degree members alone tin last inherited.

> The Class where the degree members are getting inherited is called equally Super Class/parent Class/Base Class.

> The Class to which the degree members are getting inherited is called equally Sub Class/Child Class/Derived Class.

> The Inheritance betwixt Super degree together with Sub degree is achieved using "extends" keyword.
--------------------------------------- 
Example

Class 1:

public degree ClassA {
int a =10;
int b =20;

public void add(){
System.out.println(a+b);
}
public static void main(String[] args) {
ClassA objA = novel ClassA();
System.out.println(objA.a);//10
objA.add();//30
}
}
------------------------------
Class 2:

public degree ClassB extends ClassA{
int a =100;
int b =200;

public void add(){
System.out.println(a+b);
}
public static void main(String[] args) {
ClassB objB = novel ClassB();
System.out.println(objB.a);//100
objB.add();//300
}
}
--------------------------------------- 
ii) Polymorphism

Existence of Object involve inward many forms

There 2 types of Polymorphism

1) Compile Time Polymorphism / Method Overloading

2) Run Time Polymorphism / Method Overriding
--------------------------------------- 
1) Compile Time Polymorphism / Method Overloading

If 2 or to a greater extent than methods amongst same cite inward the same degree but they differ inward next ways.

a) Number of Arguments

b) Type of Arguments
------------------------------------
Example:
------------------------------------
public degree Class1 {

public void add(int a, int b){
System.out.println(a+b);
}

public void add(int a, int b, int c){
System.out.println(a+b+c);
}

public void add(double a, double b){
System.out.println(a+b);
}

public static void main(String[] args) {

Class1 obj = novel Class1();
obj.add(1.23, 2.34);
obj.add(10,  20);
obj.add(1,  5, 9);
}
}
--------------------------------------- 
iii) Abstraction

> It is a procedure of hiding implementation details together with showing alone functionality to the user.

Two types of Methods inward Java

1) Concrete Methods (The methods which are having body)

2) Abstract Methods (The methods which are non having body)

> If nosotros know the method cite but don't know the method functionality together with then nosotros become for Abstract methods.

> Java Class contains 100% concrete methods.

> Abstract degree contains ane or to a greater extent than abstract methods.
--------------------------------------- 
Example for Abstract Class:

public abstract degree Bikes {

public void handle(){
System.out.println("Bikes accept handle");
}
public void seat(){
System.out.println("Bikes accept Seats");
}

public abstract void engine();

public abstract void wheels();

public static void main(String[] args) {
//Bikes obj = novel Bikes();

}
}
--------------------------------------- 
iv) Encapsulation

It is a procedure of wrapping code together with information into a unmarried unit.

Ex: Capsule (mixer of several medicines)

Encapsulation is the technique making the fields inward a degree person together with providing access via world methods.

> It provides command over the data

> By providing setter together with getter methods nosotros tin brand a degree read alone or write only.
--------------------------------------- 
Example:

Class 1:

public degree Class1 {
private String cite ="Test Automation";

public String getName(){
return name;
}
public void setName(String newName){
name = newName;
}
public static void main(String[] args) {
Class1 obj = novel Class1();
System.out.println(obj.getName());
}
}
-----------------------------------------------
Class 2:

public degree Class2 extends Class1{

public static void main(String[] args) {
Class2 abc = novel Class2();
//abc.setName("Selenium Testing");
System.out.println(abc.getName());
}
}
----------------------------------------------------- 
Java Tutorial for Selenium Part 1

Sumber http://www.gcreddy.com/
Post a Comment (0)
Previous Post Next Post