Android apps to use in appium android test examples

Breaking

Monday, June 4, 2018

Java Program Structure With Example

Java Program Structure amongst Example

i) Java Program Structure
ii) Java Program Example
---------------------------------------------------
i) Java Program Structure

1) Documentation Section

2) Package Declaration Statement

3) Import Statement/s

We import Predefined in addition to user defined libraries using "import" Keyword

ex:

import java.io.Console;

java - Project Name
io - Package
Console - Class Name

import coffee .io.*;
java - Project
io- Package
io.* - import all classes from io package
----------------------------------------------
4) Class Declaration

Ex:
public course of pedagogy Sample{
}

public - Access Modifier
class - Java Keyword to declare class
Sample - Class Name
-----------------------------------------
5) primary Method (Java Program Execution Starts from primary method)

public static void primary (String [] args) {
Code...
}

public - Access Modifier
static Non -Access Modifier (use primary method without invoking whatever object)
object.method();
object.method(val1, val2);
----------------------------
void - Returns nothing
main - Method name
--------------------------------
(String [] args) - Jyothi
----------------------------------------------------------
6) Declaration Statements

We declare Variables in addition to Constants

int a;
a=10;

int b, c, d;
b=20, c=30, d=40;

int f=60, g=70, h=80;
-----------------------------
final int y;
y=100; //Incorrect

final x = 200; //Correct
----------------------------------------
Variables vs. Constants

int a=10;
------
------
---------
a=20; 
------
-----
a=10;
---------------
final int b=100;
----------
----------
b=200; //Incorrect
---------------------------------------------
7) Normal Statements

System.out.println("Hello Java");
System - Predefined Class
out - Object
println - Method
"Hello Java" - Message

a+b = d;
--------------------------------
8) Code Blocks

Conditions, Loops, Methods....
---------------------------------------------
9) Object Creation Statement, etc...

Syntax:
ClassName objectName = novel ClassName();
---------------------------------------------------
ii) Java plan Example

//Note: We declare User defined Methods earlier the primary method in addition to telephone telephone those methods subsequently the primary method

Java Methods
 Predefined
 User Defined 
  Non -Static Methods (Method amongst Retuning Value, Method Returns Nothing)
  Static Methods (Method amongst Retuning Value, Method Returns Nothing)
---------------------------------------------------
//It is a Sample Program to Understand the Java Syntax in addition to Program Structure
package abcd;
public course of pedagogy JavaProgram {
//Create a Method amongst Parameters in addition to Return value (Non Static Method)
public int add(int a, int b){
int result;
result = a + b;
return result;
}
//Create a Method without Parameters in addition to Returns Nothing (Non Static Method)
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 amongst Parameters in addition to Return value (Static Method)
public static int sub(int a, int b){
int trial = a-b;
return result;
}
//Create a Method without Parameters in addition to Returns Nothing (Static Method)
public static void comparison2(){
int a =100, b=500;

if (a > b){
System.out.println("A is a Big Number"); 
}
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 amongst Initialization
int c, d, e; //Declaring Multiple Variables
int f=40, g=50, h=60; //Declaring multiple Variables amongst Initialization

double l =12.3456;
char m= 'Z';
boolean x = true;
String y = "Selenium Testing";

System.out.println(y); //Selenium Testing
System.out.println(l);//12.3456
System.out.println("Hello Java");//Hello Java

int z = a+b;
System.out.println("Value of the z Variable is: "+ z);// Value of z Variable is: 30 

final int abc =100;//Declare Integer Type Constant 
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");
}//B is a Big Number

char degree ='A';
switch (grade) {//Decide amid 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");
}//Excellent
//Print 1 to five Numbers except quaternary number
for (int i = 1; i <=5; i++){
if (i != 4){
System.out.println(i);
}
}

int j =1;
while (j <=10){
System.out.println(j);
j++;
}

int n =1;
do
{
System.out.println(n);
n++;
} piece (n<=5);

String [] tools = {"Selenium", "UFT", "RFT", "SilkTest"};

for (String xyz: tools){
System.out.println(xyz);
}
//Create Object inwards gild to access /call Non-Static Methods

JavaProgram smriti = novel JavaProgram();
//Call Non-static methods

int r = smriti.add(10, 20);
System.out.println(r);//30
System.out.println(smriti.add(10, 20));//30

smriti.Comparison();//A is a Big Number

//Class Static Methods
int r2= JavaProgram.sub(18, 11);
System.out.println(r2);//7
System.out.println(JavaProgram.sub(18, 11));//7

JavaProgram.comparison2();//B is a Big Number

comparison2();//B is a Big Number
}
}
-------------------------------------------------------------------

Sumber http://www.gcreddy.com/

No comments:

Post a Comment