Encapsulation in java

Encapsulation:

  • Data encapsulation is used to hide the implementation details from users.
  • If a data member is private it means it can only be accessed within the same class. 
  • No outside class can access private data member (variable) of other class.

Syntax:

  1. class class_name

    {

    private data_members;

    public methods()

    {

    }

    }

Example program:
  1. class Account {

        private int account_number;

        private int account_balance;

     

        public void show_Data() {

            account_number=13456;

            account_balance=5000;

            System.out.println("Before deposit of "+"account XXXXX"+account_number+" number of balance :"+account_balance);

        }

     

        public void deposit(int a) {

            if (a < 0) {

                System.out.println("Given amount is wrong...");

            } else

                account_balance = account_balance + a;

            System.out.println("After deposit of current balance :"+account_balance);

        }

    }

     

    class Encapsulation{

        public static void main(String args[])

        {

            Account ac=new Account();

            ac.show_Data();

            ac.deposit(200);

        }

    }

Output:
Share:

Exception handling in java

Exception handling:

  • Exception in java is an indication of some unusual event.
  • Usually it indicates the error.
  • Various keywords used in handling the exception are :
  • try-A block of source code that is to be monitored for the exception.
  • catch  -The catch block handles the specific type of exception along with the     try block.
  • finally - It specifies the code that must be executed even though exception may or                may not occur.
  • throw - Used to throw specific exception from the program code.
  • throws - It specifies the exceptions that can be thrown by a particular method.

Syntax:

  1. class class_name

    {

    try

    {

    //Exception occures here

    }

    catch(Exception_type exception_variable)

    {

    //Exception handling here

    }

    }

Types of exception:

1) Built-in exceptions:

Before using exception:

  1. public class Divide_By_Zero {

        public static void main(String args[]) {

            int a = 0, b = 0, c = 0;

            a = 5;

            b = 0;

            c = a / b;

            System.out.println(a + " divide by zero of value is : " + c);

        }

    }

Output:

Handling above error by using exception:

  1. public class Divide_By_Zero {

        public static void main(String args[])

        {

            int a = 0,b=0,c=0;

            try

            {

                a=5;

                b=0;

                c=a/b; 

            }

            catch(ArithmeticException e)

            {

                System.out.println(a+" divide by zero is infinity.");

            }

        }

Output:

Another Example program:

  1. public class Divide_By_Zero {

        public static void main(String args[]) {

            int a[] = new int[3];

            try {

                for (int i = 1; i <= 3; i++) {

                    a[i] = i * i;

                    System.out.println("Arrsy index"+"\t"+"Added value");

                    System.out.println(i+"\t\t"+a[i]);

                }

            } catch (ArrayIndexOutOfBoundsException e) {

                System.out.println("Array index is out of bounds.");

            }

        }

    }

Output:

2) User-defined exception:

  • If you need to create user-defined exception, must be constructor method.

Example program:

  1. import java.lang.Exception;

    public class user_defined_exception extends Exception {

     

        public user_defined_exception(String msg) {

            super(msg);

        }

    }

    class Objects {

        public static void main(String args[]) {

            int age=15;

            try {

                if (age < 21) {

                    throw new user_defined_exception("Your age is very less than the condition.");

                }

            } catch (user_defined_exception e) {

                System.out.println("This is exception block.");

                System.out.println(e.getMessage());

     

            } finally {

                System.out.println("Finally exception block is end.");

            }

        }

    }

Output:
Share:

String handling in java

String handling:

String is a collection of character. In java, String defines the objects. The string object is used in order to handle some text.

Most widely used functions are,

1. The length() function:

It is used to count and return the number of characters presents.

Example program length():

  1. import java.util.Scanner;

    public class String_length {

         public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);

            System.out.print("Enter any string: ");

            String a = sc.next();

            System.out.print("Length of "+a+" is : "+a.length()+" Characters \n");

         }

       

    }

Output:

2. String compare:

Method 1:

  1. import java.util.Scanner;

    public class String_equals {

         public static void main(String args[]) {

             int i;

        Scanner sc = new Scanner(System.in);

            System.out.print("Enter any string: ");

            String a = sc.next();

            String str="Whereisstuff";

            if (a.equals(str))

            {

                System.out.println("Entered string "+a+" is equal to stored string("+str+").");

            }

            else

            {

               System.out.println("Entered string "+a+" is not equal to stored string("+str+").");

            }

           

         }

    }

Method 2:

  1. import java.util.Scanner;

    public class String_compare_method2 {

         public static void main(String args[]) {

             int i;

        Scanner sc = new Scanner(System.in);

            System.out.print("Enter any string: ");

            String a = sc.next();

            String str="Whereisstuff";

            if (a==str)

            {

                System.out.println("Enterd string "+a+" is equal to stored string("+str+").");

            }

            else

            {

               System.out.println("Enterd string "+a+" is not equal to stored string("+str+").");

            }

           

         }

    }

Output:

If condition:

Else condition:

3. String compare with ignore case:

  1. import java.util.Scanner;

    public class String_EqualsIgnore {

         public static void main(String args[]) {

             int i;

        Scanner sc = new Scanner(System.in);

            System.out.print("Enter any string: ");

            String a = sc.next();

            String str="Whereisstuff";

            if (a.equalsIgnoreCase(str))

            {

                System.out.println("Enterd string "+a+" is equal to stored string("+str+").");

            }

            else

            {

               System.out.println("Enterd string "+a+" is not equal to stored string("+str+").");

            }

           

         }

    }

Output:

If condition:

Else condition:

4. String to compare with array:

  1. import java.util.Scanner;

    public class String_Compare_in_Array {

        public static void main(String[] args) {

            int[] num = {1000, 2000, 3000, 4000};

            boolean found = false;

            Scanner input = new Scanner(System.in);

            System.out.println("Enter your PIN number : ");

            int pin = input.nextInt();

            for (int n : num) {

                if (n == pin) {

                    found = true;

                    break;

                }

            }

            if (found) {

                System.out.println("Entered PIN number is correct.");

            } else {

                System.out.println("Sorry try again!!!");

            }

        }

    }

Output:

If condition:

Else part:

5. String upper and lower case:

  1. import java.util.Scanner;

    public class String_upper_lower {

        public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

            System.out.print("Enter any string in capital letters: ");

            String a = sc.next();

            System.out.print("Entered string is converted upper into lower : "+a.toLowerCase()+"\n");

            System.out.print("Enter any string in small letters: ");

            String b = sc.next();

            System.out.print("Entered string is converted lower into upper : "+b.toUpperCase()+"\n");

        }

    }

     

Output:
Share:

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me