Title

JAVA

      In this blog, you will find programs on JAVA and also some help on how to write programs for JAVA...

December 18, 2014

WAP to print the Fibonacci Series uptil the term 'n' entered by the User

import java.util.*;
class Fibonacci
{
    public static void main()
    {
        int a = -1, b = 1;
        Scanner s=new Scanner(System.in);
        System.out.println("Enter the value for n: ");
        int n=s.nextInt();
        int c;
        for(int x = 1; x <= n; x++)
        {
            c = a + b;
            System.out.println(c);
            a = b;
            b = c;
        }
    }
}

WAP to get the full path of a file. Extract the file path, file name & extension.

Sample Input:
Enter the path: C:\Users\User\Pictures\Ferrai.jpeg

Sample Output:
Path: C:\Users\User\Pictures\
Name: Ferrai
Extention: jpeg


import java.util.Scanner;
public class Path
{
    public static void main()
    {
        Scanner s=new Scanner(System.in);
        System.out.print("Enter the path: ");
        String str=s.nextLine();
        String a[]=str.split("\\\\");
        String b[]=a[a.length-1].split("\\.");
        String f="";
        for(int x=0; x<a.length-1; x++)
            f=f+a[x]+"\\";
        System.out.println("Path: " + f);
        System.out.println("Name: " + b[0]);
        System.out.println("Extention: " + b[1]);
    }
}

WAP to get the name of a person and print his initials

import java.util.Scanner;
public class Initials
{
    public static void main()
    {
        Scanner s=new Scanner(System.in);
        String str=s.nextLine();
        String a[]=str.split("\\ +");
        String p="";
        for(int x=0; x<a.length; x++)
        {
            char c=a[x].charAt(0);
            p=p+c+".";
        }
        System.out.println(p);
    }
}

Pascal's Triangle

Sample Output: n=5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1 


import java.util.Scanner;
public class PT
{
    public static void main()
    {
        Scanner s=new Scanner(System.in);
        System.out.println("Please enter value for n");
        int n=s.nextInt();
        int pas[]=new int [n+1];
        pas[0]=1;
        for(int i=0;i<n;i++)
        {
            for(int j=0; j<=i; j++)
            {
                System.out.print(pas[j] + " ");
            }
            System.out.println();
            for(int k=i+1; k>=1; k--)
            {
                pas[k]=pas[k]+pas[k-1];
            }
        }
    }
}

Bubble Sort

public class BSort
{
    public static void main()
    {
        int a[]={12,45,34,23,1,3,2,7,98};
        for(int x=0; x<a.length-1; x++)
        {
            for(int y=x+1; y<a.length; y++)
            {
                if(a[x] > a[y])
                {
                    int temp=a[y];
                    a[y]=a[x];
                    a[x]=temp;
                }
            }
        }
        for(int x=0; x<a.length; x++)
        {
            System.out.println(a[x]);
        }
    }
}

Selection Sort



public class SSort
{
   public static void main()
   {
       int a[]={12,45,34,23,1,3,2,7,98};
       for(int x=0; x<a.length-1; x++)
       {
           int l=a[x], pos=x;
           for(int y=x+1;y<a.length;y++)
           {
               if(a[y]<l)
               {
                   l=a[y];
                   pos=y;
               }
           }
           if(x != pos)
           {
               int temp=a[x];
               a[x]=a[pos];
               a[pos]=temp;
           }
       }
       for(int x=0; x<a.length; x++)
           System.out.println(a[x]);
   }
}

Binary Search


import java.util.Scanner;

public class BSearch
{
    public static void main()
    {
        Scanner s=new Scanner(System.in);
        System.out.print("Enter the no you want to find: ");
        int n=s.nextInt();
        int m[]={12,45,34,23,1,3,2,7,98}, lo=0, hi=m.length-1, mid=(hi + lo)/2;
        boolean found=false;
        while(lo<=hi)
        {
            if(m[mid]==n)
            {
                found=true;
                break;
            }
            if(n>m[mid])
                lo=mid+1;
                else
                hi=mid-1;
            mid=(hi+lo)/2;
        }
        if(found==true)
            System.out.println("Found");
            else
            System.out.println("Not Found");
    }
}

Sequential Search

import java.util.Scanner;

public class SSearch
{
    public static void main()
    {
        Scanner s=new Scanner(System.in);
        System.out.print("Enter the no you want to search: ");
        int n=s.nextInt();
        int m[]={12,45,34,23,1,3,2,7,98};
        boolean found=false;
        for(int x=0; x<m.length; x++)
        {
            if(m[x]==n)
            {
                found=true;
                break;
            }
        }
        if(found==true)
            System.out.println("Found");
            else
            System.out.println("Not Found");
    }
}