Java Tutorial 4

Java Tutorial 4
(Java Arrays, Java user defined Methods too Java Built inward Methods Part-1)

IX) Java Arrays
i) Introduction

> Java Array is an Object that holds a fixed release of values of a unmarried information type.

> The length of an Array is established when the Array is created.

> Array length is fixed, Java Array has Zero based index.
------------------------------------
ii) Declaration of Arrays
1st method:
int abc []; // Creating Array
    abc = novel int [4]; // Define Size
    abc[0] = 10; // Assign Values
    abc[1] = 20;
    System.out.println(abc[0] + abc[1]);//30
------------------
int abc []; // Creating Array
    abc = novel int [4]; // Define Size
    abc[0] = 10; // Assign Values
    abc[1] = 20;
    abc[4] = 40; //Error
    System.out.println(abc[0] + abc[1]);//30
--------------------------------------
2nd Method
int [] abc = novel int [5]; // Creating an Array alongside Size
    abc[0] = 10;
    abc[1] = 20;
    System.out.println(abc[1] + abc[2]); //20
------------------------------------------------
3rd Method
int [] abc = {10, 20, 30, 40, 50};
    System.out.println(abc[1] + abc[2]); //50   
--------------------------------------
iii) Creating unlike types of Arrays
char [] abc = {'A', 'B', 'c'}; //Array of Characters
    int [] xyz = {10, 20, 30, 40}; //Array of Integers
    String [] a = {"Selenium", "UFT", "LoadRunner"}; //Array of Strings
    boolean [] b = {true, false, false, true}; // Array of Boolean values
   
    System.out.println(abc[1]); //B
    System.out.println(xyz[0]);//10
    System.out.println(a[1]);//UFT
    System.out.println(b[2]);//false
---------------------------------------------
iv) Copy values an Array into about other Array
int [] array1 = {1, 2, 3, 4, 5};
    int [] array2 = array1;
    System.out.println(array2[2]); //3
    for (int i = 0; i < array2.length; i++){
        System.out.println(array2[i]);
    }
-----------------------------------
v) Types of Arrays
i) Single Dimensional Array

ii) Multi Dimensional Array

int [] array1 = {1, 2, 3, 4, 5};
    int [] [] array2 = {{1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}}; //Multi dimensional Array
    System.out.println((array2[0][0]));//1
    System.out.println((array2[1][0]));//2
    System.out.println((array2[0][4]));//9
    System.out.println((array2[1][3]));//8
    System.out.println((array2[1][4]));//10
    System.out.println((array2[0][2]));//5
    System.out.println((array2[1][2]));//6
--------------------------------
Assignment

Print multi dimensional Array values using nested for loop
--------------------------------------------------------
vi) Advantages & Disadvantages of Arrays
Advantages:
i) Using Arrays nosotros tin sack optimize the code, information tin sack last retrieved easily.

ii) We tin sack larn required information using index position.

Disadvantages
i) We tin sack shop fixed release of elements only.

ii) It doesn't alter its size during execution.
----------------------------------------------
Java Methods

i) Introduction

What is Method?

A laid of statements to perform an Operation.

Methods besides known equally Procedures or Functions.

In Structure programming nosotros role Functions (Built inward too User defined)

In Object Oriented programming nosotros role Methods (Built inward too User defined)

Functions Vs. Methods
-----------------------
Functions are standalone things tin sack last used individually.

Methods are mostly associated alongside objects, but without object besides nosotros tin sack telephone telephone methods inward Java.

Types of Methods:

Basically nosotros accept 2 types of Methods

i) Built inward Methods

ii) User defined Methods.
------------------------------------
i) Built inward Methods
> Java has a library of classes too methods, organized inward packages.

Ex:

import java.io.Console;
import java.io.*;

> In firm to role built inward methods nosotros necessitate to import packages or detail classes.

> java.lang packet is automatically imported inward whatever Java program.

> using import keyword nosotros tin sack import packages/classes.
-------------------------------------
Categories of Built inward Methods

i) String methods

ii) Character methods

iii) Number methods

iv) Array methods
etc...
------------------------------------
X) User defined Methods inward Java

Two types of User defined methods

i) Method without returning whatever value

ii) Method alongside returning a value
----------------------------------------
Writing methods (with returning value)
a) Syntax for creating a method too calling the method without invoking whatever object:


accessmodifier nonaccessModifier returnType methodName(parameters){
Statements
----------
----------
----------
}

b) Syntax for Creating a method too calling the method alongside invoking object:

accessModifier returnType methodName(Parameter){
Statements
---------
---------
----------
}
---------------------------

Example:
public static int add together (int a, int b){ // Creating a Method
        int result;
        effect =  a +  b;
        supply result;
    }
------------------------
Calling a Method alongside Returning a Value
Syntax:
dataType variableName = methodName(values);

example:

int xyz = add(123, 456); // Calling a Method
-----------------------------------------------
public static int multiply (int a, int b, int c){
        int result;
        effect = a * b * c;
        supply result;
}
public static void primary (String [] args){
int a = multiply(2, 4, 6);
System.out.println(a);
}
-------------------------------------
Creating too Calling Methods using Object
Creating Objects

Synatx:

className objectName = novel className();

Example:

JavaArrays obj = novel JavaArrays();
----------------------------------
public int multiply (int a, int b, int c){
        int result;
        effect = a * b * c;
        supply result;
}
public static void primary (String [] args){

    JavaArrays obj = novel JavaArrays();
    int a = obj.multiply(2, 4, 6);
    System.out.println(a);
}
-----------------------------------------
ii) Method without returning whatever Value
a) Syntax for Creating a method too calling the method without invoking whatever object:

acessModifier nonaccessModifier retrunType null methodname(parameters){
Statements
---------
-----------
------------
}
Example:
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 primary (String [] args){
    studentRank(450);
    }
       
}
-----------------------------------
b) Syntax for Creating a method too calling the method alongside invoking object:
accessModifier retrunType null methodName(parameters){
Statements
------------
-----------
----------
}

Example:

public  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 primary (String [] args){
    JavaArrays xyz = novel JavaArrays();
    xyz.studentRank(450);
    }
-------------------------------
// Calling a method from about other class

public static void main(String [] args){
        JavaArrays abc = novel JavaArrays();
        abc.studentRank(650);
    }
------------------------
public static void main(String [] args){
        studentRank(650);
    }
--------------------------------------------
Creating User defined methods

    i) Creating a Method alongside returning a value

    ii) Creating a Method without returning whatever value
------------------------------------
Calling User defined methods

    i) Calling a method alongside returning a value too without invoking object
    ii) Calling a method alongside returning a value too alongside invoking object
    iii) Calling a method without returning whatever value too without invoking object
    iv) Calling a method without returning whatever value too alongside invoking object
    v) Calling an external method without invoking object
    vi) Calling an external method alongside invoking object
----------------------------------------
Method Overloading

Two or to a greater extent than methods alongside same name, but alongside unlike parameters
(different release of parameters or unlike information types)

i) past times changing unlike release of parameters
a) int add together (int a, int b)

b) int add together (int a, int b, int c)

Example:

public static int add together (int a, int b){
        int result;
        effect = a+b;
        supply result;
    }
public static int add together (int a, int b, int c){
    int result;
    effect = a + b + c;
    supply result;
}
public static void primary (String [] args){
    int a = add together (10, 20);
    int b = add together (10, 20, 30);
    System.out.println(a);//30
    System.out.println(b);//60
}
----------------------------------------
ii) past times changing information types
public static int add together (int a, int b){
        int result;
        effect = a+b;
        supply result;
    }
public static double add together (double a, double b){
    double result;
    effect = a + b;
    supply result;
}
public static void primary (String [] args){
    int a = add together (10, 20);
    double b = add together (1.234, 2.4567);
    System.out.println(a);//30
    System.out.println(b);//
}
--------------------------------------
Method Overriding
if 2 methods alongside same get upwards too same release of arguments available inward super class
and sub class, therefore nosotros telephone telephone those 2 methods are overridden.

Example:

Super class

int a = 10, b = 20;
public int add together (){
    int result;
    effect = a+b;
    supply result;
    }

Sub Class

    int a = 100, b = 200;
    world int add together (){
        int result;
        effect = a+b;
        supply result;
    }
   world static void main(String [] args){
       MethodOverloading2 xyz = novel MethodOverloading2();
       int x = xyz.add();
       System.out.println(x); // 300
      
       MethodOverloading abc = novel MethodOverloading();
       int y = abc.add();
       System.out.println(y);//30
   }
---------------------------------------
XI) Java Built inward Methods

Categories of Built inward Methods

i) String methods

ii) Number methods

iii) Character methods

iv) Array methods etc...
--------------------------------
i) String methods

1) compareTo() method (It compares 2 strings) - iii means comparison

if str1 = str2 therefore 0
if str1 > str2 therefore positive value
if str1 < str2 therefore negative value

Ex:

 public static void main(String [] args){
String str1 = "SELENIUM";
String str2 = "selenium";
String str3 = "seleniuma";
String str4 = "selenium";

System.out.println(str1.compareTo(str2));
System.out.println(str3.compareTo(str2));
System.out.println(str2.compareTo(str4)); //0
   }

2) equals() method (It compares 2 strings) - 2 means comparison

 public static void main(String [] args){
String str1 = "SELENIUM";
String str2 = "selenium";
String str3 = "selenium";

System.out.println(str1.equals(str2)); //false
System.out.println(str2.equals(str3)); //true
 }
------------------------------------------------------
3) concat() Method (It concatenates / joins 2 strings)

 public static void main(String [] args){
String str1 = "Selnium";
String str2 = " Testing";

System.out.println(str1.concat(str2)); //Selenium Testing
 }
------------------------------------------
4) charAt() Method (Returns a grapheme past times position)

Example:

 public static void main(String [] args){
String str1 = "Selnium";

System.out.println(str1.charAt(1)); //e
 }
--------------------------------
5) equalsIgnorecase() method

Examples:

 public static void main(String [] args){
String str1 = "SELENIUM";
String str2 = "selenium";
String str3 = "UFT";

System.out.println(str1.equalsIgnoreCase(str2)); //true
System.out.println(str1.equalsIgnoreCase(str3)); //false
System.out.println(str1.equals(str2)); //false
 }
----------------------------------------
6) toUppercase() method (Converts values to Upper case)

Example:

public static void main(String [] args){
String str1 = "slenium";
String str2 = "SELEnium";
String str3 = "SELENIUM";
String str4 = "selenium123";

System.out.println(str1.toUpperCase()); //SELENIUM
System.out.println(str2.toUpperCase()); //SELENIUM
System.out.println(str3.toUpperCase()); //SELENIUM
System.out.println(str4.toUpperCase()); //SELENIUM123
}
------------------------------
7) toLowercase() Method (Converts values to Lower case)

Example:

public static void main(String [] args){
String str1 = "slenium";
String str2 = "SELEnium";
String str3 = "SELENIUM";
String str4 = "selenium123";

System.out.println(str1.toLowerCase()); //selenium
System.out.println(str2.toLowerCase()); //selenium
System.out.println(str3.toLowerCase()); //selenium
System.out.println(str4.toLowerCase()); //selenium123
}
-------------------------------------
8) trim() method (Removes spaces from both sides of a string)

Example:
public static void main(String [] args){
String str1 = "            Selenium               ";

System.out.println(str1);
System.out.println(str1.trim());
}
----------------------------------------

Sumber http://www.gcreddy.com/
Post a Comment (0)
Previous Post Next Post