Java for Selenium Part 2


Java Program Example and Comments in Java Programs

i) Java Program Example

ii) Comments in Java
--------------------------------------------
i) Java Program Example

//It is a Java Program Example to Understand the Java Syntax and Program Structure
package javaExamples;

public class SampleProgram {
//Create a Method with Parameters and returning value, Access the method by invoking the object
public int add(int a, int b){
int result = a+b;
return result;
}
//Create a Method with No Parameters and Reruns Nothing, Access the method by invoking the object   
public void comparison(){
int a=100, b=50;
if (a > b){
System.out.println("A is a Big Number");
}
else
{
System.out.println("B is a Big Number");   
}
}
//Create a Method with Parameters, returning a value and access the method without Object
public static int sub(int a, int b){
int result = a -b;
return result;
}
//Create a Method with No Parameters, Returns Nothing and access the method without Object
public static void comparison2(){
int a=100, b=500;
if (a > b){
System.out.println("A is a Big Number");
}
else
{
System.out.println("B is a Big Number");   
}
}

public static void main(String[] args) {
int a; //Variable Declaration
a=10; //Initialization
int b=20; //Variable Declaration and Initialization
double c = 1.234;
int d, e, f; //Declaration of Multiple Variables
int g =30, h=40, k=50;//Declaration of Multiple Variables and Initialization

int l = a + b;
System.out.println("Addition of a, b is: "+l);//30
System.out.println(k);//50
k=123;
System.out.println(k);//123

final int abc = 100;//Constant Declaration
System.out.println(abc);//100

if (a > b){
System.out.println("A is a Big Number");
}
else {
System.out.println("B is a Big Number");
}
String mytool = "Selenium Testing";
System.out.println(mytool);

char grade = 'A';

switch (grade){ //Decide among several Alternates

case 'A':
System.out.println("Excellent");
break;

case 'B':
System.out.println("Good");
break;

case 'C':
System.out.println("Better");
break;

default:
System.out.println("Invalid Grade");
}

for (int i = 1; i <= 10; i++){ //Repetitive Execution
if (i != 7){
System.out.println(i);   
}
}

int j=5; //Display 1 to 5 Numbers using while loop
while(j>=1){
System.out.println(j);
j--;
}

int m =20;
do
{
System.out.println(m);
m++;
} while (m<=6);

String [] tools = {"Selenium","UFT", "RFT", "SilkTest"};
System.out.println(tools.length);

for (String xyz: tools){
System.out.println(xyz);   
}

//Create Object
SampleProgram obj = new SampleProgram();
int res = obj.add(123, 321);
System.out.println(res);

System.out.println(obj.add(123, 321));

obj.comparison();

//Access Static methods using Class Name

int res2 = SampleProgram.sub(200, 123);
System.out.println(res2);

System.out.println(SampleProgram.sub(200, 123));

SampleProgram.comparison2();

}
}
------------------------------------------------------
ii) Comments in Java

Comments are English words used for Code Documentation.

1) Purpose of Comments

 
    a) To make the Code Readable
    b) To make the Code disable from Execution.

2) Comments Syntax in Java
a) Use // for Single Comment

b) Use /*.............
.....................
.....................
..................*/ for multiple line comment

Or
Comment Multiple Lines

(Eclipse IDE)
Select Statements/Steps
Source Menu -> Add Block Comment

Uncomment

(Eclipse IDE)
Select Comment block
Source Menu -> Remove Block Comment
---------------------------------------------------
3) Example:

public class Sample2 {

public static void main(String[] args) {
int a, b; //Variables Declaration
a =10; b=20;
int c = a+b;
//Display the Addition value of a and b
System.out.println("Addition of a, b is: "+c);
System.out.println("");

/*if (a > b) {
System.out.println("A is a Big Number");   
}
else
{
System.out.println("B is a Big Number")    ;
}*/

}
}
------------------------------------------------
4) Usage of Comments in Test Automation
a) To Write Test case header

b) To Write Method header

c) To Explain Complex Logic
---------------------------------------------

Java for Selenium Part 3 Link

0 comments:

Post a Comment