Showing posts with label java programming. Show all posts
Showing posts with label java programming. Show all posts

Monday, 4 June 2012

Recursion in java

public class MyClass {
public static void main(String[] args) {
    System.out.println(fact(5));
   
}   

public static long fact(long n){
    if(n <= 1){
        return 1;
    }
    else{
        return n*fact(n-1);
    }
}

}

Output:
120

concat Method of String


public class MyClass {
public static void main(String[] args) {
    String str1 = "harry";
    String str2 = "jerry";
    System.out.println(str1.concat(str2));
}   
}
 Output:
harryjerry

indexOf Method of String

public class MyClass {public static void main(String[] args) {
    String str = "harryjerryharryjerryharry";
    System.out.println(str.indexOf('e'));
}   
}

Output:
6


The method str.indexOf('e') will return the index of letter e of first occurrence in the string


Another example:
public class MyClass {
public static void main(String[] args) {
    String str = "harryjerryharryjerryharry";
    System.out.println(str.indexOf("yj"));
}  
}
Output:
4

startsWith Method of String


public class MyClass {
    public static void main(String[] args) {
        String [] words = {"tom","jerry","tomato","potato"};
       
        for(String w:words){
            if(w.startsWith("to")){
                System.out.println(w+" starts with to");
            }
        }
    }
}


Output:
tom starts with to
tomato starts with to


Saturday, 17 September 2011

User defined exception

class AgeException extends Exception
{
    private String message;
   
    public AgeException()
    {
        this.message = "Age Cannot Be Less Than 1";
    }
   
    public AgeException(String message)
    {
        this.message = message;
    }
   
    public String toString()
    {
        return this.message;
    }
}

class SetAge
{
    private int age;
   
    public void setAge(int age)
    {
        this.age = age;
    }
   
   
    public void showAge()
    {
        String message = "Age Cannot Be Greater Than 120";
       
        if(age >=1 && age <= 120)
        {
            System.out.println("Age Set : " + age);
        }
   
        else if ( age > 120)
        {
            throw new AgeException(message);
           
        }
        else
        {
            throw new AgeException();
        }
    }
}



public class AgeMain
{
    public static void main(String[] args)
    {
        SetAge setage = null;
       
        try
        {
            setage = new SetAge();
            setage.setAge(-5);
           
            setage.showAge();
           
        }
        catch(NullPointerException exception)
        {
            System.out.println("Exception Object Reference is Null ");
        }

        catch(AgeException exception )
        {
            System.out.println("Exceptio  : " +  exception);
        }
    }
}

IllegalArgumentException exeption


public class SetValues
{
    public static void main(String[] args)
    {
        try
        {
            System.out.println("Value : " + Integer.parseInt("4545"));
            System.out.println("Value : " + Integer.parseInt("60,40"));
        }
        catch(IllegalArgumentException exeption)
        {
            System.out.println("Exception : " + exeption);
        }
    }
}

IndexOutOfBoundsException exception in java


public class ArrayTesting
{
    public static void main(String[] args)
    {
        int arr[]= new int[2];
       
        arr[0]=1;
        arr[1]=2;
       
       
        int res;
       
       
        try
        {
            res = arr[0] + arr[1] + arr[2];
            System.out.println("Sum is : " + res);
        }
        catch(IndexOutOfBoundsException exception)
        {
            System.err.println("Exception : " +exception);
        }
       
       
    }
}

Monday, 5 September 2011

POJO Class in java

 /*here employee is POJO class
POJO = "Plain Old Java Object"
POJO class contain only getter and setter methods*/

class Employee 
{
    private int empid;
    private String empname;
    private float empsalary;
   
    public int getEmpid()
    {
        return empid;
    }
   
    public void setEmpid(int empid)
    {
        this.empid = empid;
    }
   
    public String getEmpname()
    {
        return empname;
    }
    public void setEmpname(String empname)
    {
        this.empname = empname;
    }
    public float getEmpsalary()
    {
        return empsalary;
    }
    public void setEmpsalary(float empsalary)
    {
        this.empsalary = empsalary;
    }
}

public class EmployeeMain
{
    public static void main(String[] args)
    {
        Employee employee1 = new Employee();
        Employee employee2 = new Employee();
        Employee employee3 = new Employee();
       
       
        employee1.setEmpid(10001);
        employee1.setEmpname("Simran Singh");
        employee1.setEmpsalary(30000);
       
       
       
        System.out.println("Id          : " + employee1.getEmpid());
        System.out.println("Name        : " + employee1.getEmpname());
        System.out.println("Salary      : " + employee1.getEmpsalary());
       
        System.out.println("\n");
       
        employee2.setEmpid(10002);
        employee2.setEmpname("Rajat");
        employee2.setEmpsalary(30000);
       
       
        System.out.println("Id          : " + employee2.getEmpid());
        System.out.println("Name        : " + employee2.getEmpname());
        System.out.println("Salary      : " + employee2.getEmpsalary());
       
       
        System.out.println("\n");
       
        employee3.setEmpid(10003);
        employee3.setEmpname("Raghav");
        employee3.setEmpsalary(30000);
       
       
       
        System.out.println("Id          : " + employee3.getEmpid());
        System.out.println("Name        : " + employee3.getEmpname());
        System.out.println("Salary      : " + employee3.getEmpsalary());
       
       
       
       
    }
}