Java Tutorial 4

Java Tutorial 4
(Java Arrays, Java user defined Methods and Java Built in Methods Part-1)

IX) Java Arrays
i) Introduction

> Java Array is an Object that holds a fixed number of values of a single data 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 = new 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 = new 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 = new int [5]; // Creating an Array with 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 different 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 another 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 we can optimize the code, data can be retrieved easily.

ii) We can get required data using index position.

Disadvantages
i) We can store fixed number of elements only.

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

i) Introduction

What is Method?

A set of statements to perform an Operation.

Methods also known as Procedures or Functions.

In Structure programming we use Functions (Built in and User defined)

In Object Oriented programming we use Methods (Built in and User defined)

Functions Vs. Methods
-----------------------
Functions are standalone things can be used individually.

Methods are generally associated with objects, but without object also we can call methods in Java.

Types of Methods:

Basically we have two types of Methods

i) Built in Methods

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

Ex:

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

> In order to use built in methods we need to import packages or particular classes.

> java.lang package is automatically imported in any Java program.

> using import keyword we can import packages/classes.
-------------------------------------
Categories of Built in Methods

i) String methods

ii) Character methods

iii) Number methods

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

Two types of User defined methods

i) Method without returning any value

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


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

b) Syntax for Creating a method and calling the method with invoking object:

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

Example:
public static int add (int a, int b){ // Creating a Method
        int result;
        result =  a +  b;
        return result;
    }
------------------------
Calling a Method with 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;
        result = a * b * c;
        return result;
}
public static void main (String [] args){
int a = multiply(2, 4, 6);
System.out.println(a);
}
-------------------------------------
Creating and Calling Methods using Object
Creating Objects

Synatx:

className objectName = new className();

Example:

JavaArrays obj = new JavaArrays();
----------------------------------
public int multiply (int a, int b, int c){
        int result;
        result = a * b * c;
        return result;
}
public static void main (String [] args){

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

acessModifier nonaccessModifier retrunType nothing 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 main (String [] args){
    studentRank(450);
    }
       
}
-----------------------------------
b) Syntax for Creating a method and calling the method with invoking object:
accessModifier retrunType nothing 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 main (String [] args){
    JavaArrays xyz = new JavaArrays();
    xyz.studentRank(450);
    }
-------------------------------
// Calling a method from another class

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

    i) Creating a Method with returning a value

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

    i) Calling a method with returning a value and without invoking object
    ii) Calling a method with returning a value and with invoking object
    iii) Calling a method without returning any value and without invoking object
    iv) Calling a method without returning any value and with invoking object
    v) Calling an external method without invoking object
    vi) Calling an external method with invoking object
----------------------------------------
Method Overloading

Two or more methods with same name, but with different parameters
(different number of parameters or different data types)

i) by changing different number of parameters
a) int add (int a, int b)

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

Example:

public static int add (int a, int b){
        int result;
        result = a+b;
        return result;
    }
public static int add (int a, int b, int c){
    int result;
    result = a + b + c;
    return result;
}
public static void main (String [] args){
    int a = add (10, 20);
    int b = add (10, 20, 30);
    System.out.println(a);//30
    System.out.println(b);//60
}
----------------------------------------
ii) by changing data types
public static int add (int a, int b){
        int result;
        result = a+b;
        return result;
    }
public static double add (double a, double b){
    double result;
    result = a + b;
    return result;
}
public static void main (String [] args){
    int a = add (10, 20);
    double b = add (1.234, 2.4567);
    System.out.println(a);//30
    System.out.println(b);//
}
--------------------------------------
Method Overriding
if two methods with same name and same number of arguments available in super class
and sub class, then we call those two methods are overridden.

Example:

Super class

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

Sub Class

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

Categories of Built in Methods

i) String methods

ii) Number methods

iii) Character methods

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

1) compareTo() method (It compares two strings) - 3 way comparison

if str1 = str2 then 0
if str1 > str2 then positive value
if str1 < str2 then 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 two strings) - 2 way 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 two 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 character by 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());
}
----------------------------------------

0 comments:

Post a Comment