Android apps to use in appium android test examples

Breaking

Friday, June 1, 2018

Java User Defined Methods

Selenium Class xviii - Java User Defined Methods

Java Methods
Predefined Methods
String Methods
Array Methods
Number Methods
Character Methods etc...

User defined Methods
Methods amongst furnish value (Perform Operations, in addition to furnish values)
Call Methods past times invoking Object (Non Static Methods)
Call Methods without invoking Object (Static Methods)

Methods returns goose egg (Performing Operations only)
Call Methods past times invoking Object (Non Static Methods)
Call Methods without invoking Object (Static Methods)
----------------------------------------------
User defined Methods - Examples

1) Write methods (with furnish value)
a) Call the methods past times invoking object (Non-static methods)

Syntax:

acessModifier returnType methodName (Parameters...){
Statements
---------------
-------------
------------
return Statement...
}

Note: methods attain got Parameters but Parameters are optional, if nosotros don't desire purpose Parameters in addition to then write ()...
And purpose Data Type for these type methods,
Ex:
(String Username, String Password)
(int val1, int val2, int val3)
----------------------------------------------
Call methods

Create Object...
ClassName objectName = novel Classname();

Call methods...
dataType variableName = objectname.method(values for Parameters...);
Or
system.out.println(objectname.method(values for Parameters...);
----------------------------------------------
Example:
public course of pedagogy JavaMethods {
// Create a Non-static method amongst Parameters....
public int add1(int num1, int num2){
int number = num1 + num2;
return result;
}
//Create a Non static method without Parameters....
public int add2(){
int num1 =10, num2 =20;
int res = num1 + num2;
return res;
}
public static void main(String[] args) {
//Create Object 
JavaMethods obj = novel JavaMethods();

//Call Methods
int x = obj.add1(11, 22);
int y = obj.add2();

System.out.println(x);//33
System.out.println(y);//30

//OR
System.out.println(obj.add1(14, 25));//39
System.out.println(obj.add2());//30
}
}
----------------------------------------------
1) Writing Methods (Methods amongst furnish value)
b) Call methods without invoking Object (Static Method)

Syntax:

accessModifier nonAcessModifier / static retun Type methodName (parameters...){
Statements
-------------
-------------
--------------
return statement;
}
----------------------------------------------
Call Static Methods
dataType variableName = ClassName.method(values for Parameters...);

Note: nosotros tin telephone phone this type of methods without ClassName besides / directly.
----------------------------------------------
Example:

public course of pedagogy JavaMethods {
//Create a Static Method amongst Return value in addition to Parameters....
public static int multiply1(int a, int b){
int number = a * b;
return result;
}
//Create a Static Method amongst Return value in addition to No Parameters....
public static int multiply2(){
int a =10, b =20;
int number = a*b;
return result;
}
public static void main(String[] args) {
int res1 = JavaMethods.multiply1(7, 9);
int res2 = JavaMethods.multiply2();

System.out.println(res1);
System.out.println(res2);
//Or
System.out.println(JavaMethods.multiply1(1, 2));
System.out.println(JavaMethods.multiply2());
}
}
----------------------------------------------
Call Methods inside the Methods....

i) Call Static method inside Static Method
public static int sub2(){
int a=10, b =7;
int val =sub1(); 
int number = (a-b) + val;
return result;
}
----------------------------------------------
ii) Call Static inside Non Static Method

public int add2(){
int c=100;
Sample obj = novel Sample();
int d = sub2();
int res = (obj.add()) + c +d;
return res;
}
----------------------------------------------
iii) Call Non static method inside Non Static Method

public int add2(){
int c=100;
Sample obj = novel Sample();
int d = sub2();
int res = (obj.add()) + c +d;
return res;
}
----------------------------------------------
iv) Call Non Static Method inside Static method

public static int sub1(){
int a=100, b =30;
Sample obj = novel Sample();
int val =obj.add(); 
int number = (a-b) - val;
return result;
}
----------------------------------------------
Executing Methods...

Sample obj2 = novel Sample();
int x = obj2.add();
int y = obj2.add2();
System.out.println(x);//30
System.out.println(y);//173

System.out.println(sub1());//40
System.out.println(sub2());//43
----------------------------------------------
2) Writing Methods (Methods furnish Nothing)
a) Call Methods past times invoking object (Non static method)

Syntax:

accessModifier returnsNothing methodName(Parameters....){
Statements
-------------
----------
--------
}

Example:

public course of pedagogy Sample2 {
//Create a Non Static Method in addition to returns Nothing
public void comparison(){
int a=10, b=20;

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) {
//Create Object
Sample2 abc = novel Sample2();
abc.comparison();
}
}
----------------------------------------------
2) Writing Methods (Methods furnish Nothing)
b) Call Methods without invoking object (Static method)

Syntax:

accessModifier static returnsNothing methodName(Parameters){
Statements
-----------
------------
-------------
}

Example:
public course of pedagogy Sample2 {
//Create a Static Method in addition to returns Nothing
public static void comparison(){
int a=100, b=20;

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) {
Sample2.comparison();
comparison();
}
}
----------------------------------------------
Usage of user defined methods
i) Internal purpose (Create in addition to Call Methods inside the same class/program)
ii) External purpose (Call Methods from about other class/program)
----------------------------------------------
Class 1:
public course of pedagogy Class1 {
public int add(int a, int b){
int number = a+b;
return result;
}
public static void main(String[] args) {
Class1 obj1 = novel Class1();
int res= obj1.add(10, 20);
System.out.println(res);
}
}
----------------------------------------------
Class 2:
public course of pedagogy Class2 extends Class1 {

public static void main(String[] args) {
Class2 obj2 = novel Class2();
int x= obj2.add(12, 23);
System.out.println(x);
}
}
----------------------------------------------
public course of pedagogy Class2 extends Class1 {
public int sub(){
int a=100, b=45;
int number = a-b;
return result;
}
public static void main(String[] args) {
Class2 obj2 = novel Class2();
int x= obj2.add(12, 23);
System.out.println(x);//35

int y = obj2.sub();
System.out.println(y);//55
}
}
----------------------------------------------
public course of pedagogy Class1 {
public static int add(int a, int b){
int number = a+b;
return result;
}
public static void main(String[] args) {
int x= add(10, 30);
System.out.println(x);//40
}
}
----------------------------------------------
public course of pedagogy Class2 {

public static void main(String[] args) {
int y = Class1.add(1, 2);
System.out.println(y);
}
}
----------------------------------------------
public course of pedagogy Class2 extends Class1 {

public static void main(String[] args) {
int y = Class2.add(1, 2);
System.out.println(y);
}
}
----------------------------------------------

Sumber http://www.gcreddy.com/

No comments:

Post a Comment