Thursday 11 August 2016

Vector program in java

import java.io.*;
import java.util.*;
class VectorDemo1
{
public static void main(String args[]) throws IOException
{
Vector v = new Vector( );
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
while(true)
{
System.out.println("1.Creation");
System.out.println("2.Insertion");
System.out.println("3.Deletion");
System.out.println("4.Display");
System.out.println("5.exit");
System.out.println("Enter Ur Choice:");
int n=Integer.parseInt(br.readLine());
switch(n)
{
    case 1:    
        System.out.println("Enter the Element:");
        v.addElement(new Integer(br.readLine()));
        break;
    case 2: System.out.println("Enter the position to be inserted:");
        int x=Integer.parseInt(br.readLine());
        System.out.println("Enter the Element:");
        int y=Integer.parseInt(br.readLine());
        v.insertElementAt(y,x);
        break;
    case 3:System.out.println("Enter the position to be deleted:");
        int z=Integer.parseInt(br.readLine());
        v.removeElementAt(z-1);
        break;
    case 4: System.out.println("Elements are:");
        for(int i=0;i<v.size();i++)
        System.out.println(v.get(i)+" ");       
        break;
    case 5:System.exit(0);
}
}
}
}

Illegal Argument Exception throw catch exception in java

class MyClock
{
int hours ,minutes, seconds;
 MyClock(int hours, int minutes, int seconds)
{
if (hours < 1 || hours > 12)
{
throw new IllegalArgumentException("Hours must be between 1 and 12");
}
if (minutes < 0 || minutes > 59)
{
throw new IllegalArgumentException("Minutes must be between 0 and 59");
}
if (seconds < 0 || seconds > 59)
{
throw new IllegalArgumentException("Seconds must be between 0 and 59");
}
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
System.out.println(hours+"hrs:"+minutes+"mins:"+seconds+"sec;");
}
public MyClock(int hours, int minutes)
{
this(hours, minutes, 0);
}
public MyClock(int hours)
{
this(hours, 0, 0);
}
}
public class ThrowTest
{
public static void main( String args [])
{
try
{
MyClock clock = new MyClock(1, 10,20);
MyClock c=new MyClock(1);
MyClock a=new MyClock(2,50,80);
}
catch( IllegalArgumentException e)
{
System.out.println("IllegalArgumentException is caught...."+e.getMessage());
}
}
}

Applet program example in java

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
//<applet code="textinput" width=500 height=300></applet>
public class textinput extends Applet implements ActionListener
{
    Font f=new Font("TimesRoman",Font.BOLD,30);
    TextField t1,t2;
    Button b1;
    Label l1;
    public void init()
    {
        setBackground(Color.pink);
        setForeground(Color.blue);
        t1=new TextField(8);
        t2=new TextField(8);
        b1=new Button("Ok");
        setFont(f);
        l1=new Label("Enter a Number");
        add(l1);
        add(t1);
        add(t2);
        add(b1);
        b1.addActionListener(this);
    }
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==b1)
        {
            int n, r,rev=0;
            String s1;
           
            s1=t1.getText();
            n=Integer.parseInt(s1);
            while(n!=0)
            {
                r=n%10;
                rev=rev*10+r;
                n=n/10;
            }
                       
            String x=String.valueOf(rev);
            t2.setText(x);
        }
    }
}

Threads Syncronization in java

class SynchronizeDemo2
{
    public static void main(String args[])
    {
        Shared shared = new Shared();
        CustomThread thread1 = new CustomThread(shared, "one");
        CustomThread thread2 = new CustomThread(shared, "two");
        CustomThread thread3 = new CustomThread(shared, "three");
        CustomThread thread4 = new CustomThread(shared, "four");
        try
        {
            thread1.join();
            thread2.join();
            thread3.join();
            thread4.join();
        }
        catch(InterruptedException e) {}
    }
}

class CustomThread extends Thread
{
    Shared shared;
    public CustomThread(Shared shared, String string)
    {
        super(string);
        this.shared = shared;
        start();
    }
    public void run()
    {
        shared.doWork(Thread.currentThread().getName());
    }
}

class Shared
{
     synchronized void doWork(String string)
    {
        System.out.println("Starting " + string);
        try
        {
            Thread.sleep(1000);
        }
        catch (InterruptedException e) {}
        System.out.println("Ending " + string);
    }
}

Stack operation in java

import java.io.*;
class stack
{
final int max=3;
int a[]=new int[max];
int top=-1;
void push(int x)
{
if(top>=max-1)
System.out.println("Stack over flow");
else
{
top++;
a[top]=x;
}
}
void pop()
{
if(top<0)
System.out.println("Stack under flow");
else
{
System.out.println("deleted item is.."+a[top]);
top--;
}
}
void display()
{
if(top<0)
System.out.println("no elemnets in the stack to display");
else
{
System.out.println("The elements in the stack are..\n");
for(int i=top;i>=0;i--)
{
System.out.println(a[i]);
}
}
}
}
class stackop
{
public static void main(String args[])throws IOException
{
int ch;int x;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
stack st=new stack();
while(true)
{
System.out.println("\n1. Push");
System.out.println("2. POP");
System.out.println("3. Display");
System.out.println("4. Exit");
System.out.println("Enter your choice");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1: System.out.println("Enter the element to be inserted");
    x=Integer.parseInt(br.readLine());
    st.push(x);
    break;
case 2:    st.pop();
    break;
case 3: st.display();
    break;
case 4:System.exit(0);
}
}
}
}

stack using linked list in java

import java.io.*;
class stlin
{
int item;
stlin next;
}
class stack
{
    int i=1;
    stlin start,curr;
    void push(int x)
    {
        curr=new stlin();
        curr.item=x;
        curr.next=start;
        start=curr;
        i++;
       
    }
    void pop()
    {
        if(i==1)
        {
            System.out.println("Stack is empty");
        }
        else
        {
            i--;
            curr=start;
            System.out.println("Deleted Element is:"+curr.item);
            start=start.next;
            curr=null;
        }
               
    }
    void display()
    {
        if(i==1) System.out.println("no elements");
        curr=start;
        while(curr!=null)
        {
            System.out.println(curr.item);
            curr=curr.next;
        }
        System.out.println();
    }
}
class stacklin
{
public static void main(String []args) throws IOException
{
    stack sl=new stack();
    int ch,x;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    while(true)
    {
        System.out.println("1.PUSH");
        System.out.println("2.POP");
        System.out.println("3.SHOW");
        System.out.println("4.EXIT");
        System.out.println("Enter Ur Choice:");
        ch=Integer.parseInt(br.readLine());
        switch(ch)
        {
            case 1: System.out.println("Enter a Number:");               
                x=Integer.parseInt(br.readLine());
                sl.push(x);
                break;
            case 2: sl.pop();
                break;
            case 3: sl.display();
                break;
            case 4: System.exit(0);
        }
    }
}
}

selection sort in java

import java.io.*;
class selectionsort
{
    public static void main(String args[])
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
        int n=0,i,j,t;
        int a[] = new int[10];
        System.out.println("Enter size of the array:");
        try
        {
              n= Integer.parseInt(br.readLine());
        }
        catch(Exception e) { }
        System.out.println("Enter elements into the array:");
        try
        {
             for(i=0;i<n;i++)
                   a[i] = Integer.parseInt(br.readLine());
        }
              catch(Exception e) { }
        i=0;
        while(i<n-1)
        {
        int key=i;
        for(j=i+1;j<n;j++)
        {
            if(a[j]<a[key])
            {
                key=j;
            }
        }
        t=a[key];
        a[key]=a[i];
        a[i]=t;
        i++;
        }
        System.out.println("The sorted array is :");
             for(i=0;i<n;i++)
        System.out.println(a[i]);
         }
}

queue operation in java

import java.io.*;
class queue
{
final int max=3;
int a[]=new int[max];
int F=-1,R=-1;

void insert(int x)
{
if(R==max-1)
System.out.println("insertion is not possible");
else
{
a[R+1]=x;
R++;
}
}

void delete()
{
if(F==R)
System.out.println("deletion is not Possible because Queue Is empty");
else
{
F=F+1;
System.out.println("delete item is .."+a[F]);
}
}

void display()
{
if(F==R)
System.out.println("queue is empty");
else
{
for(int i=F+1;i<=R;i++)
System.out.println(a[i]+"\n");
}
}
}

class queueop
{
public static void main(String args[])throws IOException
{
int ch;int x;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
queue q=new queue();
while(true)
{
System.out.println("\n1. Insertion");
System.out.println("2. Deletion");
System.out.println("3. Display");
System.out.println("4. Exit");
System.out.println("Enter your choice");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1: System.out.println("Enter the element to be inserted");
    x=Integer.parseInt(br.readLine());
    q.insert(x);
    break;
case 2:    q.delete();
    break;
case 3: q.display();
    break;
case 4:System.exit(0);
}
}
}
}

queue using linked list in java

import java.io.*;
class qlin
{
int item;
qlin next;
}
class queue
{
    int i=0;
    qlin first,prev,curr;
    void insert(int x)
    {
        i++;
        if(i==1)
        {
            first=new qlin();
            first.item=x;
            first.next=null;
            prev=first;
        }
        else
        {
            curr=new qlin();
            curr.item=x;
            curr.next=null;
            prev.next=curr;
            prev=curr;

        }
    }
    void delete()
    {
        if(i==0)
        {
            System.out.println("queue is empty");
        }
        else
        {
            i--;
            curr=first;
            System.out.println("Deleted Element="+curr.item);
            first=first.next;
            curr=null;
        }
        System.out.println();
               
    }
    void display()
    {
        curr=first;
        while(curr!=null)
        {
            System.out.println(curr.item);
            curr=curr.next;
        }
        System.out.println();
    }
}
class queuelin
{
public static void main(String []args) throws IOException
{
    queue ql=new queue();
    int ch,x;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    while(true)
    {
        System.out.println("1.Insert");
        System.out.println("2.Delete");
        System.out.println("3.SHOW");
        System.out.println("4.EXIT");
        System.out.println("Enter Ur Choice:");
        ch=Integer.parseInt(br.readLine());
        switch(ch)
        {
            case 1: System.out.println("Enter a Number:");               
                x=Integer.parseInt(br.readLine());
                ql.insert(x);
                break;
            case 2: ql.delete();
                break;
            case 3: ql.display();
                break;
            case 4: System.exit(0);
        }
    }
}
}

Threads program in java

class y
{
  synchronized void doWork(String string)
{
        System.out.println("Starting " + string);
        try {
            Thread.sleep(1000);
        }
        catch (InterruptedException e) {}
        System.out.println("Ending " + string);
}
}

class X extends Thread
{
y p;
String string2;
public X(y string1,String string2)
{
super(string2);
p=string1;
this.string2=string2;
}
public void run()
{
p.doWork(Thread.currentThread().getName());
}
}

class process
{
public static void main(String args[])
{
y p=new y();
X t1=new X(p,"one");
X t2=new X(p,"two");
X t3 =new X(p,"three");
X t4=new X(p,"four");
X t5=new X(p,"five");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}

Exception Handling in java ( ArrayIndexOutOfBoundsException)

class MyException extends Exception
{
String s1;
MyException(String s1)
{
this.s1=s1;
}
public String toString()
{
return(s1);
}
}
class Myclass
{
public static void main(String args[])
{
int marks[]={59,33,99,100,97};
int stno[]={1,2,3,4,5};
try
{
for(int i=0;i<6;i++)
{
if (marks[i]>100)
throw new MyException("invaid data");
if (i<4)
System.out.println("rollno="+stno[i]+"\tmarks="+marks[i]);
else
throw new ArrayIndexOutOfBoundsException("Array out of bnds");
}
}
catch(MyException e)
{
System.out.println("Exception="+e);
}
catch(ArrayIndexOutOfBoundsException a)
{
System.out.println("Index out of range"+a.getMessage());
}
finally
{
System.out.println("finished");
}
}
}

linked list in java

import java.io.*;
class lists
{
    int data;
    lists next;
}
class slist
{
    lists head,last,new1,temp;
    slist()
    {
        head=null;
    }
    public void create(int x)
    {
        new1=new lists();
        if(new1==null)
        {
            System.out.println("creation is not poosible");
            return;
        }
        new1.data=x;
        if(head==null)
        {
            head=last=new1;
            last.next=null;
        }
        else
        {
            last.next=new1;
            last=new1;
            last.next=null;
        }
    }
   

    public void display()
    {
        if(head==null)
        {
            System.out.println("Empty list");
            return;
        }
        System.out.println("The Elements are:");
        for(temp=head;temp!=null;temp=temp.next)
        {
            System.out.print(temp.data+"\t");
        }
        System.out.println();
    }
   


public void insert(int x,int d)
    {
        int pos,i;
        pos=x;
        new1=new lists();
        if(new1==null)
        {
            System.out.println("Insufficient Memory");
            return;
        }
        new1.data=d;
        if(head==null && pos==1)
        {
            head=last=new1;
            new1.next=null;
        }
        else if(head!=null && pos==1)
        {
            new1.next=head;
            head=new1;
        }
        else
        {
            i=2;
            temp=head;
            while(i<pos)
            {
                temp=temp.next;
                i++;
            }
            if(temp==null)
            {
                last.next=new1;
                last=new1;
                last.next=null;
            }
            else
            {
                new1.next=temp.next;
                temp.next=new1;
            }
        }
    }
    public void deletes(int p)
    {
        int pos,i;
        lists curr;
        pos=p;
        if(head==null)
        {
            System.out.println("Empty Linked List");
            return;
        }
        if(pos==1)
        {
            temp=head;
            head=head.next;
            temp=null;
        }
        else
        {
            i=2;
            temp=head;
            curr=temp.next;
            while(i<pos)
            {
                temp=temp.next;
                curr=temp.next;
                i++;
            }
            temp.next=curr.next;
            curr=null;
        }
    }
}
class linklist
{
    public static void main(String []args) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        slist sl=new slist();
        int opt,y,z;
        while(true)
        {
            System.out.println("1.creation");
            System.out.println("2.Insertion");
            System.out.println("3.Deletion");
            System.out.println("4.display");
            System.out.println("5.exit");
            System.out.println("enter ur choice:");
            opt=Integer.parseInt(br.readLine());
            switch(opt)
            {
                case 1:     System.out.println("Enter the number:");
                    y=Integer.parseInt(br.readLine());
                    sl.create(y);
                    break;
                case 2:     System.out.println("Enter the position:");
                    y=Integer.parseInt(br.readLine());
                    System.out.println("Enter the Data:");
                    z=Integer.parseInt(br.readLine());
                    sl.insert(y,z);
                    break;
                case 3:     System.out.println("Enter the position:");
                    y=Integer.parseInt(br.readLine());
                    sl.deletes(y);
                    break;
                case 4:     sl.display();
                    break;
                case 5:     System.exit(0);
            }
        }
    }
}
       

Linear Search in java

import java.io.*;
class linear
{
    public static void main(String []args) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int a[]=new int[10];
        int i,key,flag=0,n;
        System.out.println("Enter the no of elements");
        n=Integer.parseInt(br.readLine());
        System.out.println("Enter the Values");
        for(i=0;i<n;i++)
        a[i]=Integer.parseInt(br.readLine());       
        System.out.println("Enter the Search element");
        key=Integer.parseInt(br.readLine());
        for(i=0;i<n;i++)
        {
            if(a[i]==key)
            {
                flag=0;
                break;
            }
        }
        if(flag==0)
        System.out.println("Search Element found at location "+ (i+1));
        else
        System.out.println("Search Element Not found");
    }
}

       

Insertion sort in java

import java.io.*;
class insertionsort
{
   public static void main(String args[])
   {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    int n=0,i,j,t;
    int a[] = new int[10];
    System.out.println("Enter size of the array:");
    try
    {
      n= Integer.parseInt(br.readLine());
    }
    catch(Exception e) { }
   
      System.out.println("Enter elements into the array:");
    
     try
     {
     for(i=0;i<n;i++)
       a[i] = Integer.parseInt(br.readLine());
     }
      catch(Exception e) { }
     for(i=0;i<n-1;i++)
    {
        for(j=i+1;j>0;j--)
        {
            if(a[j]<a[j-1])
            {
                t=a[j];
                a[j]=a[j-1];
                a[j-1]=t;
            }
        }
    }

     System.out.println("The sorted array is :");
     for(i=0;i<n;i++)
    System.out.println(a[i]);
     }
}

Fibonacci Series Prime numbers in java


class fibo extends Thread
{
int f=0,s=1,next;
public void run()
{
System.out.println("fibo of "+f+"\n"+s);
for(int i=2;i<20;i++)
{
next=f+s;
f=s;
s=next;
System.out.println("fibo of "+next);
}
}
}
class prime extends Thread
{
int i,j,flag=0;
public void run()
{
try
{
sleep(5000);
}
catch(Exception e){}
for(i=1;i<=20;i++)
{
flag=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
{
flag++;
}
}
if(flag==2)
System.out.println("prime no:"+i);
}
}
}
class fibprim
{
public static void main(String args[])
{
fibo t1=new fibo();
prime t2=new prime();
t1.start();
t1.setPriority(10);
t2.start();
t2.setPriority(3);
}
}

Double Linked List in java

import java.io.*;
class dlink
{
    int data;
    dlink prev,next;
}
 class dlinks
{
    dlink head,temp,new1,last,curr;

    void create(int x)
    {   
        new1=new dlink();
        if(new1==null)
        {
            System.out.println("Insufficient Memory");
            System.exit(0);
        }
        else
        {
            new1.data=x;
            if(head==null)
            {
                head=last=new1;
                head.prev=null;
                head.next=null;
            }
            else
            {
                last.next=new1;
                new1.prev=last;
                last=new1;
                last.next=null;
            }
        }
    }
    void displayfor()
    {
        if(head==null)
        {
            System.out.println("Empty Linked List");
            return;
        }
        for(temp=head;temp!=null;temp=temp.next)
        {
            System.out.println(temp.data);
        }
        System.out.println(" ");
    }
    void displayback()
    {
        if(head==null)
        {
            System.out.println("Empty Linked List");
            return;
        }
        for(temp=last;temp!=null;temp=temp.prev)
        {
            System.out.println(temp.data);
        }
        System.out.println(" ");
    }
    void insert(int p,int d)
    {
        int pos,i;
        new1=new dlink();
        pos=p;
        new1.data=d;
        if(head==null && pos==1)
        {
            head=last=new1;
            head.prev=null;
            head.next=null;
        }
        else if(head!=null & pos==1)
        {
            new1.next=head;
            head.prev=new1;
            head=new1;
            head.prev=null;
        }
        else
        {
           
            i=2;
            temp=head;
            while(i<pos)
            {           
                temp=temp.next;
                i++;
            }
            if(temp.next==null)
            {
                last.next=new1;
                new1.prev=last;
                last=new1;
                last.next=null;
            }
            else
            {
                new1.next=temp.next;
                temp.next.prev=new1;
                temp.next=new1;
                new1.prev=temp;
            }
        }
    }
    void deletes(int p)
    {
        int pos,i;
        pos=p;
        if(head==null)
        {
            System.out.println("Empty Linked List");
            return;
        }
        if(pos==1)
        {
           
            if(head==last)   
            {
                temp=head;
                head=head.next;
                last=null;
                temp=null;
            }
            else
            {
                temp=head;
                head=head.next;
                head.prev=null;
                temp=null;
            }
        }
        else
        {
            i=2;
            temp=head;
            curr=temp.next;
            while(i<pos)
            {
                i++;
                temp=temp.next;
                curr=temp.next;
            }
            if(curr.next==null)
            {
                temp.next=curr.next;
                last=last.prev;
                curr=null;
            }
            else
            {
                temp.next=curr.next;
                curr.next.prev=temp;
                curr=null;
            }
        }
    }
}
class doublelink
{
    public static void main(String []args) throws IOException
    {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        dlinks dl=new dlinks();
        int ch,z,y;
        while(true)
        {
            System.out.println("1.Creation");
            System.out.println("2.Insertion");
            System.out.println("3.Deletion");
            System.out.println("4.Display Forward");
            System.out.println("5.Display Backward");
            System.out.println("6.Exit");
            System.out.println("Enter the choice:");
            ch=Integer.parseInt(br.readLine());
            switch(ch)
            {
                case 1: System.out.println("Enter the number:");
                    z=Integer.parseInt(br.readLine());
                    dl.create(z);
                    break;
                case 2: System.out.println("Enter the position:");
                    y=Integer.parseInt(br.readLine());
                    System.out.println("Enter the number:");
                    z=Integer.parseInt(br.readLine());
                    dl.insert(y,z);
                    break;
                case 3: System.out.println("Enter the position:");
                    y=Integer.parseInt(br.readLine());
                    dl.deletes(y);
                    break;
                case 4: dl.displayfor();
                    break;
                case 5: dl.displayback();
                    break;
                case 6: System.exit(0);
            }
        }
    }    }

Circular queue in java

import java.io.*;
class cirqueue
{
final int max=5;
int a[]=new int[max];
int front=-1,rear=-1;

void insert(int x)
{
    if((front==0 && rear==max-1 ) || (front==rear+1))
    {
    System.out.println("Queue is Full");
    }
    if(front==-1)
    {
        front=0;
        rear=0;
    }
    else
    {
    if(rear==max-1)
    rear=0;
    else
    rear=rear+1;
    }
    a[rear]=x;
}

void delete()
{
    if(front==-1)
    {
        System.out.println("queue is empty");
    }
System.out.println("The element deleted from the queue is:"+a[front]);
    if(front==rear)
    {
        front=-1;
        rear=-1;
    }
    else
    {
    if(front==max-1)
    front=0;
    else
    front=front+1;
    }


}

void display()
{
    int front_pos=front;
    int rear_pos=rear;
    if(front_pos==-1)
    {
        System.out.println("\n No element in Queue");
        return;
    }
    if(front_pos<=rear_pos)
    {
        while(front_pos<=rear_pos)
        {
            System.out.println(a[front_pos]+" "+front_pos++);
        }
        System.out.println();
    }
    else
    {
        while(front_pos<=max-1)
        {
            System.out.println(a[front_pos]+" ");
            front_pos++;
        }
        front_pos=0;
        while(front_pos<=rear_pos)
        {
            System.out.println(a[front_pos]+" ");
            front_pos++;
        }
    System.out.println();
    }
}
}
class circularqueueop
{
public static void main(String args[])throws IOException
{
int ch;int x;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
cirqueue cq=new cirqueue();
while(true)
{
System.out.println("\n1.Insertion");
System.out.println("2. Deletion");
System.out.println("3. Display");
System.out.println("4. Exit");
System.out.println("Enter your choice");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1: System.out.println("Enter the element to be inserted");
    x=Integer.parseInt(br.readLine());
    cq.insert(x);
    break;
case 2:    cq.delete();
    break;
case 3: cq.display();
    break;
case 4:System.exit(0);
}
}
}
}

Bubble Sort in java

/* Bubble Sort */
import java.io.*;
class BubbleSort
{
   public static void main(String args[])
   {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    int n=0,i,j,t;
    int a[] = new int[10];
    System.out.println("Enter size of the array:");
    try
    {
      n= Integer.parseInt(br.readLine());
    }
    catch(Exception e) { }
   
      System.out.println("Enter elements into the array:");
    
     try
     {
     for(i=0;i<n;i++)
       a[i] = Integer.parseInt(br.readLine());
     }
      catch(Exception e) { }
     for(i=0;i<n-1;i++)
      for(j=0;j<n-1-i;j++)
       if(a[j] > a[j+1])
       {
          t = a[j];
          a[j] = a[j+1];
          a[j+1] = t;
        }
     System.out.println("The sorted array is :");
     for(i=0;i<n;i++)
    System.out.println(a[i]);
     }
}

Binary Search java program

import java.io.*;
class binary
{
    public static void main(String []args) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int a[]=new int[10];
        int i,key,flag=0,n;
        int top,bott;
        System.out.println("Enter the no of elements");
        n=Integer.parseInt(br.readLine());
        System.out.println("Enter the Values in sorted order");
        for(i=0;i<n;i++)
        a[i]=Integer.parseInt(br.readLine());       
        System.out.println("Enter the Search element");
        key=Integer.parseInt(br.readLine());
        top=0;bott=n-1;
        int mid=(top+bott)/2;
        while(top <= bott && flag == 0)
           {
               mid = (top + bott) /2 ;
               if(a[mid] == key)
          flag = 1;
        else if(a[mid]>key)
           top = mid + 1;
        else
           bott = mid - 1;
        }

        if(flag==1)
        System.out.println("Search Element found at location "+(mid+1));
        else
        System.out.println("Search Element Not found");
    }
}