AutomationTesting_Java_14 (1)
AutomationTesting_Java_14 (1)
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---
--open downloads folder --> unzip eclipse file ---> open eclipse folder--> double
click on eclipse application file (blue colour icon)--> it should ask for workspace
path--> keep as it is(default path)-->select checkbox-->launch--> welcome page
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--
package Sample1;
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
1.Variables:
Variables are nothing but piece of memory use to store information.
one variable can store 1 information at a time.
-------------------------------------------------------------------------
-
package Variables; -
-
public class sample2 -
{ -
public static void main(String[] args) {
//3. Usage
System.out.println(sname);
System.out.println(rollnum);
System.out.println(grade);
System.out.println(per);
-
} -
} -
-
-------------------------------------------------------------------------
package Variables;
public class sample2
{
public static void main(String[] args) {
//3. Usage
System.out.println("Student name: "+sname);
System.out.println("student rollNum: "+rollnum);
System.out.println("student grade: "+grade);
System.out.println("student percentage: "+ per +" %");
}
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
Data Types:
Data type are used to represent type of data or information which we are
going to use in our java program.
1.Primitive datatype:
There are 8 type of primitive datatypes.
all the primitive datatypes are keywords.
* Memory size of primitive datatype are fix.
The types of primitive datatype are:
1.(Numeric + Non-decimal):-
Ex: 80,85,10,..etc
2.(Numeric + decimal):-
Ex: 22.5,22.8,6.4....
5. float 4 byte
6. double 8 byte
3.Character :-
Ex: A,B,X,Z.
7. char 2 byte
---------------------------------------
4.Conditional:-
Ex: true,false.
8. boolean 1 bit
---------------------------------------------------------
2. Non-primitive datatype:
There are 2 types of non primitive datatypes .
all the Non primitive datatypes are identifiers.
* Memory size of non primitive datatype is not defined or not fix.
Methods:
A method is a block of code which only runs when it is called.
You can pass data, known as parameters, into a method.
Methods are used to perform certain actions, and they are also known as
functions.
Why use methods? To reuse code: define the code once, and use it many
times.
1. main method
In any Java program, the main() method is the starting point from where
compiler starts program execution.
So, the compiler needs to call the main() method.
2. Regular method
package Methods;
//1. static regular method call from same class --> methodname();
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
package Methods;
demo3.m4();
package Methods;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--
Note: At the time of program execution main method is going to get executed
automatically,
where as regular methods are not going to get executed automatically.
At the time of program execution priority is sceduled for main method only.
To call a regular method we need to make call method call from main method,
until unless if the method call is not made regular method will not get
executed.
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
package Methods;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
package Methods;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
package Methods;
addition();
package Methods;
studentName();
studentName("Anil");
studentName("Sunil");
System.out.println("main method ended");
}
package Methods;
public static void studentInfo(String sname, int sRollNum, char sGrade, float sper)
{
System.out.println(sname);
System.out.println(sRollNum);
System.out.println(sGrade);
System.out.println(sper);
}
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
package Control_Statements;
//20>=35
if(marks>=35)
{
System.out.println("Pass");
}
// if(condition)
// {
//
// }
}
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
package Control_Statements;
// 35>=35
if(marks>=35)
{
System.out.println("Pass");
}
else
{
System.out.println("Fail");
}
}
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
package Control_Statements;
if(marks>=65)
{
System.out.println("distinction");
}
else if (marks>=60)
{
System.out.println("1st class");
}
else if (marks>=55)
{
System.out.println("higher 2nd class");
}
else if (marks>=50)
{
System.out.println("2nd class");
}
else if (marks>=35)
{
System.out.println("Pass");
}
else
{
System.out.println("Fail");
}
}
}
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------
package Control_Statements;
if("ab"==UN)
{
System.out.println("Correct UN");
System.out.println("Enter your password");
if("xyz"==PWD)
{
System.out.println("Correct PWD--> Login Scuccessful");
}
else
{
System.out.println("Wrong PWD--> Login Failed");
}
}
else
{
System.out.println("Wrong UN --> Login Failed");
}
}
}
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
package Control_Statements;
switch (day)
{
case 1: System.out.println("Today is mon");
break;
}
}
}
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
Constructor:
A constructor in Java is a special method that is used to initialize
objects/variables.
The constructor is called when an object of a class is created.
Use of Constructor
1. To copy/load all members of class into object --> when we create
object of class
2. To initialize data member/variable
Types of Constructor
1. Default Constructor
2. User defined Constructor
1. Default Constructor
If Constructor is not declared in java class then at the time of
compilation compiler will provide Constructor for the class
If programer has declared the constructor in the class then compiler
will not provided default Constructor.
The Constructor provided by compiler at the time of compilation is
known as Default Constructor
}
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------------
package Contructor;
}
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------------
package Contructor;
//step2: initialization
sample3()
{
a=50;
b=60;
}
System.out.println("------------------");
//step3: usage
public void addition()
{
int sum=a+b;
System.out.println(sum);
}
//step3: usage
public void mul()
{
int mul=a*b;
System.out.println(mul);
}
}
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------------
package Contructor;
//step1: declaration
int a;
int b;
//constructor--> user defined
// use1: use to copy all the members of class into object
// use2. To initialize data member/variable/objects
//step2: initialization
sample4()
{
a=10;
b=5;
}
//step3: usage
public void div()
{
int divValue= a/b;
System.out.println(divValue);
}
}
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
package Contructor;
//step1: declaration
int num1; //10, 15
int num2;
//step2: initialization
}
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
package Contructor;
int num1;
int num2;
String sname;
}
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
package Contructor;
System.out.println("----------------");
System.out.println("----------------");
sample8 s10=new sample8("abc");
s10.studentName();
}
}
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
------------------------------------------------------
Object-Oriented Programming System(OOPS)
OOps concept provides 5 important pillers/principles for the language they are
1. Inheritance
2. Polymorphism
3. Encapsulation
4. Abstraction
-----------------------------------------------------------------------------------
---------------------
Inheritance:
It is one of the Oops principle where one class acquires properties of
another class with the help of 'extends' keywords is called Inheritance.
2. Multilevel Inheritance:
Multilevel Inheritance takes place between 3 or more than 3 classes.
3. Multiple Inheritance:
1 subclass acquiring properties of 2 super classes at the same time is known
as Multiple Inheritance.
Java doesn't support Multiple Inheritance using class because of "dimond
ambiguity" problem.
4. Hirarchicle Inheritance:
multiple sub classes can acquire properties of 1 super class is known as
hirarchicle Inheritance.
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------------
package Inheritance;
// super/base/parent class
package Inheritance;
}
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
package Inheritance;
//Multilevel Example
package Inheritance;
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
package Inheritance;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
package Inheritance;
}
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
package Inheritance;
// super/base/parent class
public class father
{
public void car()
{
System.out.println("Car: kia seltos");
}
package Inheritance;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
package Inheritance;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
package Inheritance;
package Inheritance;
System.out.println("---properties of son1---");
son1 s1 =new son1();
s1.mobile();
s1.car();
s1.home();
s1.money();
System.out.println("---properties of son2---");
son2 s2=new son2();
s2.laptop();
s2.car();
s2.home();
s2.money();
System.out.println("---properties of son3---");
son3 s3=new son3();
s3.PC();
s3.car();
s3.home();
s3.money();
}
}
-----------------------------------------------------------------------------------
-----------------------------
Abstract Class:
A class declared with "abstract" keyword is called abstract class.
Concrete class:
A class which provides definations for all the incomplete methods which are
present in abstarct class with the help of extends keywords is called
concrete class.
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------------
package Abstarct_Concrete_Class;
//incomplete method
abstract public void m2(); //method declaration
//incomplete method
abstract public void m3(); //method declaration
}
-----------------------------------------------------------------------------------
------------------------------
package Abstarct_Concrete_Class;
//concrete class
public class sample2 extends sample1
{
package Abstarct_Concrete_Class;
Features of Interface:
1. Data Members/variable declared inside Interface are by default static and
final.
2. methods declared inside Interface are by default public & abstract.
3. constructor concept in not present inside Interface.
4. object of Interface can't be created.
5. Interface support multiple inheritance.
6. To create object of Interface programmer need to make use of
Implementation class using implements keyword.
Implementation class:
A class which provides definations for all the incomplete methods which are
present in interface with the help of "implements" keyword is called
Implementation class.
-----------------------------------------------------------------------------------
--------------------------------------------
package Interface_ImplementationClass;
void m2();
}
-----------------------------------------------------------------------------------
-------------
package Interface_ImplementationClass;
//implementation class
public class demo2 implements demo1
{
public void m1()
{
System.out.println("method m1 completed in implementation class");
}
}
-----------------------------------------------------------------------------------
---------------------------
package Interface_ImplementationClass;
package Interface_ImplementationClass;
//super interface 1
public interface I1
{
void m3();
void m4();
}
-----------------------------------------------------------------------------------
-----------------------------
package Interface_ImplementationClass;
//super interface 2
public interface I2
{
void m5();
}
-----------------------------------------------------------------------------------
------------------------------------
package Interface_ImplementationClass;
}
-----------------------------------------------------------------------------------
----------------------------------
package Interface_ImplementationClass;
public class MultipleInheritance
{
//example of multiple inheritance using interface
}
-----------------------------------------------------------------------------------
------------------------------------------
Types of variables:
1. global variable:
the variable which is declared outside the method/block/constructor is called
global variable.
scope of global variable remains throught the class.
global variable is also called permanant variable.
2. local variable:
The variable which is declared inside the method/block/constructor is called
local variable.
scope of local variable remains only within the method/block/constructor.
local variable is also called temporary variable.
-----------------------------------------------------------------------------------
---------------------------------
1.this keyword
this keyword is use to access global variable from same class.
2. super keyword
super keyword is use to access global variable from super/diffrent class.
-----------------------------------------------------------------------------------
---------------------------------
package This_and_Super_Keywords;
}
}
-----------------------------------------------------------------------------------
-------------------------------
package This_and_Super_Keywords;
}
}
-----------------------------------------------------------------------------------
--------------------------------
package This_and_Super_Keywords;
}
-----------------------------------------------------------------------------------
-------------------------------
package This_and_Super_Keywords;
}
}
-----------------------------------------------------------------------------------
--------------------------------
package This_and_Super_Keywords;
public class TestDemo {
}
}
-----------------------------------------------------------------------------------
--------------------------------
In Java, access specifiers (also known as access modifiers) are used to define the
visibility
and accessibility of classes, methods, and variables. There are four types of
access specifiers:
1. private
The private access specifier restricts access to the members (variables, methods)
of the class
only within the same class. No other class can access these members, even
subclasses or classes
in the same package.
Example:
//Private modifier
class MyClass {
private int age;
-----------------------------------------------------------------------------------
--------------------------------
2. default (Package-Private)
When no access specifier is explicitly mentioned, it is known as default access.
The members are accessible only within the same package. They are not accessible
outside
the package, even if it's a subclass.
Example:
//Default Example
class MyClass {
int number; // default access
void show() {
System.out.println("Default method");
}
}
-----------------------------------------------------------------------------------
--------------------------------
3. protected
The protected access specifier allows members to be accessed within the same
package and by subclasses
(even if they are in different packages).
Example:
//protected Example
class MyClass {
protected int num;
-----------------------------------------------------------------------------------
--------------------------------
4. public
The public access specifier makes members accessible from any other class,
regardless of package.
Example:
java
Copy
public class MyClass {
public int number;
-----------------------------------------------------------------------------------
--------------------------------
Access Specifier Same Class Same Package Subclass (Different Package) World
(Anywhere)
private Yes No No No
default Yes Yes No No
protected Yes Yes Yes No
public Yes Yes Yes Yes
-----------------------------------------------------------------------------------
--------------------------------
Abstract classes
Interfaces
1. Abstract Class
An abstract class is a class that cannot be instantiated directly. It can have
abstract methods (methods without a body) as well as concrete methods (methods with
a body). An abstract class is meant to be extended by other classes to provide
implementations for the abstract methods.
java
Copy
interface Animal {
void sound();
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
class Employee {
// Private data members
private String name;
private int age;
private double salary;
// Getter for name
public String getName() {
return name;
}
-----------------------------------------------------------------------------------
--------------------------------------------------------------
System.out.println(ar.length); //5
//step3: usage
System.out.println(ar[2]);
//5<=4 5
// for(int i=0; i<=4; i++)
// { //ar[4]
// System.out.println(ar[i]);
// }
}
}
-----------------------------------------------------------------------------------
--------------------------------------------------------------------
package Array;
System.out.println(ar1.length);
System.out.println(ar1[3]);
}
}
-----------------------------------------------------------------------------------
--------------------------------------------------------------
import java.util.Arrays;
ar1[0]=200; //100
ar1[1]=300; //200
ar1[2]=400; //300
ar1[3]=100; //400
ar1[4]=500; //500
}
}
-----------------------------------------------------------------------------------
---------------------------------------------------------
import java.util.Arrays;
ar[0] = "ganesh";
ar[1] = "mahesh";
ar[2] = "suresh";
ar[3] = "ramesh";
ar[4] = "rahul";
Arrays.sort(ar);
}
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------
public class example8
{// 0 1 2
//0 10 20 30
//1 40 50 60
//2<=1 2
for(int i=0; i<=1; i++)
{
//inner for loop for col
//3<=2 3
for(int j=0; j<=2; j++)
{ // 1 2
System.out.print(ar[i][j]+" "); //10 20 30
} // 40 50 60
System.out.println(); //
}
}
-----------------------------------------------------------------------------------
------------------------------------------------------
package Array;
}
}
-----------------------------------------------------------------------------------
------------------------------------------------------
String class:
1. String is non-primitive data type, memory size is not fixed.
2. String is use to store collection of characters.
3. String is a inbuilt class present inside "java.lang" package.
4. String class is final class can't be inherited to other classes.
5. At the time of String declaration, initialization, object creation takes
place.
6. String objects are immutable in nature/cant be change.
7. object creation of String can be done in 2 ways:
1. without using new keyword
2. using new keyword
8. String objects are going to get stored inside String pool area which is
present inside heap area.
String pool area:
It is use to store String objects.
It is classified into 2 areas:
1. constant pool area
2. non-constant pool area.
2. During object creation time if you make use of new keyword then
object creation takes place inside non-constant pool area.
String str="abc";
str=str+"d"; //abcd
System.out.println(str);
}
}
-----------------------------------------------------------------------------------
---------------------------------
package StringClass_Methods;
System.out.println(s1==s2); //true
System.out.println(s1==s3); //false
System.out.println(s1==s4); //false
System.out.println(s4==s5); //false
}
}
-----------------------------------------------------------------------------------
----------------------------------------
String s1="abhishek";
String s2="ABHISHEK";
String s3="";
System.out.println(s1.length()); //8
System.out.println(s1.toUpperCase());
//s1=s1.toUpperCase(); //re-initialization
System.out.println(s2.toLowerCase());
//s2=s2.toLowerCase();
System.out.println(s1.isEmpty()); //false
System.out.println(s3.isEmpty()); //true
}
}
-----------------------------------------------------------------------------------
------------------------------------
public class StringClass_Methods
{
public static void main(String[] args) {
String s1="abhishek";
String s2="ABCD";
String s3="";
String s4="ABHISHEK";
String s5 ="city";
String s6 ="abcabcab";
String s7 = "java classes";
System.out.println(s1.substring(4,6)); //ci
System.out.println(s1.substring(6)); //city
System.out.println(s7.replace("java", "selenium"));
System.out.println(s2.concat(s5)); //ABCDcity
System.out.println(s2+s5); //ABCDcity
System.out.println(s1.endsWith("city")); //true
System.out.println(s1.startsWith("ve")); //true
System.out.println(s6.lastIndexOf('a')); //6
System.out.println(s6.indexOf('b')); //1
System.out.println(s1.charAt(5)); //i
System.out.println(s1.contains(s5)); //true
System.out.println(s1.equalsIgnoreCase(s4)); //true
System.out.println(s1.equals(s4)); //false
System.out.println(s1.length()); //8
System.out.println(s1.toUpperCase());
// s1=s1.toUpperCase(); //re-initialization
// System.out.println(s1);
System.out.println(s2.toLowerCase()); //abcd
// s2=s2.toLowerCase(); //re-initialization
// System.out.println(s2);
System.out.println(s1.isEmpty()); //false
System.out.println(s3.isEmpty()); //true
}
}
-----------------------------------------------------------------------------------
------------------------------------------