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 in Java
ii) Java Data Types
iii) Java Modifiers
iv) Variables
v) Operators
vi) Conditional Statements
vii) Loop Statements
viii) String Handling in Java
ix) Arrays in 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 we can handle Computer files.

Example:

1) Create a Folder

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

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

Methods are also known as Functions.

In structured programming (Ex: C Language) we use Functions (Built-in and User defined)

In Object Oriented Programming (Ex: Java Language) we use Methods (Built-in and User defined)
-------------------------------------------
When we choose Methods?

Whenever we want perform any operation multiple times then we choose methods.

Advantages of Methods

Code Reusability, using methods we can reduce the project code size.

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

Two types of Methods

1) Built in /Pre-defined Methods

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

equals() Method

Compares two strings and 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 weather 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 in Java

Two types of user defined methods

1) Method without returning any value
a) Calling Methods by invoking Object
b) Calling methods without invoking Object

2) Method with returning a Value
a) Calling Methods by invoking Object
b) Calling methods without invoking Object

Examples:

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

//Call methods
int x = abc.multiply(10, 25, 35);
System.out.println(x);
---------------------------------
//Create a method without returning any 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 event that occurs during execution of a program when normal execution of a program
    is interrupted.

> Exception handling is a mechanism to handle 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 result = 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 process of Inheriting (reusing) the class members (Variables and Methods) from one class to another.

> Non static class members only can be inherited.

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

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

> The Inheritance between Super class and Sub class is achieved using "extends" keyword.
--------------------------------------- 
Example

Class 1:

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

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

public class 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 = new ClassB();
System.out.println(objB.a);//100
objB.add();//300
}
}
--------------------------------------- 
ii) Polymorphism

Existence of Object behavior in many forms

There two types of Polymorphism

1) Compile Time Polymorphism / Method Overloading

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

If two or more methods with same name in the same class but they differ in following ways.

a) Number of Arguments

b) Type of Arguments
------------------------------------
Example:
------------------------------------
public class 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 = new Class1();
obj.add(1.23, 2.34);
obj.add(10,  20);
obj.add(1,  5, 9);
}
}
--------------------------------------- 
iii) Abstraction

> It is a process of hiding implementation details and showing only functionality to the user.

Two types of Methods in Java

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

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

> If we know the method name but don't know the method functionality then we go for Abstract methods.

> Java Class contains 100% concrete methods.

> Abstract class contains one or more abstract methods.
--------------------------------------- 
Example for Abstract Class:

public abstract class Bikes {

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

public abstract void engine();

public abstract void wheels();

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

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

It is a process of wrapping code and data into a single unit.

Ex: Capsule (mixer of several medicines)

Encapsulation is the technique making the fields in a class private and providing access via public methods.

> It provides control over the data

> By providing setter and getter methods we can make a class read only or write only.
--------------------------------------- 
Example:

Class 1:

public class Class1 {
private String name ="Test Automation";

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

public class Class2 extends Class1{

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

0 comments:

Post a Comment