Tuesday, February 26, 2013

Final variable


    
      public class FinalVariableExample {
    
            public static void main(String[] args) {
                  
                                     final int hoursInDay=24;
                                
                    System.out.println("Hours in 25 days = " + hoursInDay * 25);
                  
            }
    }
    
   
    Output
    Hours in 25 days = 600
   

Do While loop

    public class DoWhileExample {
   
      public static void main(String[] args) {
     
             int i =0;
     
        do
        {
          System.out.println("i is : " + i);
          i++;
       
        }while(i < =5);
     
      }
    }
   
 OUTPUT-
i is 0
i is 1
i is 2
i is 3
i is 4
i is 5

Monday, October 15, 2012

Java all data types example


package javaapplication2 ;

import java.util.Date;

  /**
 *
 * @author ASHISH SINGH
 */
class JavaDataType
  {

    public static void main(String[] args)
    {
                //Byte example
                
                 byte b1 = 100;
                 byte b2 = 20;
                
                System.out.println("Value of byte variable b1 is :" + b1);
                System.out.println("Value of byte variable b1 is :" + b2);

                //Charecter Example
               
        char ch1 = 'a';
                char ch2 = 65; /* ASCII code of 'A'*/
              
                System.out.println("Value of char variable ch1 is :" + ch1);  
                System.out.println("Value of char variable ch2 is :" + ch2);     


        //Double example
       
        double d = 1232.44;
                System.out.println("Value of double variable d is :" + d);

        //Float example
       
        float f = 10.4f;
                System.out.println("Value of float variable f is :" + f);

        //Integer example

         int i = 0;
                 int j = 100;
                 System.out.println("Value of int variable i is :" + i);
                 System.out.println("Value of int variable j is :" + j);

        //Long example

        long timeInMilliseconds = new Date().getTime();
                System.out.println("Time in milliseconds is : " + timeInMilliseconds);     

            //Short example

         short s1 = 50;
                 short s2 = 42;
                System.out.println("Value of short variable b1 is :" + s1);
                System.out.println("Value of short variable b1 is :" + s2);

               //Integer to string conversion

       
                i = 11;
                String str = Integer.toString(i);
        System.out.println("int to String : " + i);
          
    }
}


Output

 

Boolean Example in java

public class JavaBooleanExample {
        public static void main(String[] args) 
     {
              
       

                boolean b1 = true;
                boolean b2 = false;
                boolean b3 = (10 > 2)? true:false;
              
                System.out.println("Boolean variable b1 is :" + b1);
                System.out.println("Boolean variable b2 is :" + b2);
                System.out.println("Boolean variable b3 is :" + b3);           
        }
}


Output
 Boolean variable b1 is :true
 Boolean variable b2 is :false
 Boolean variable b3 is :true

Heapsort Algorithm and Source Code


//Heap sort
#include <iostream.h>
#include <conio.h>
const int MAX = 10 ;
class array
{
    private :
        int arr[MAX] ;
        int count ;
    public :
        array( ) ;
        void add ( int num ) ;
        void makeheap(int ) ;
        void heapsort( ) ;
        void display( ) ;
} ;
array :: array( )
{
    count = 0 ;
    for ( int i = 0 ; i < MAX ; i++ )
        arr[MAX] = 0 ;
}
void array :: add ( int num )
{
    if ( count < MAX )
    {
        arr[count] = num ;
        count++ ;
    }
    else
        cout << "\nArray is full" << endl ;
}
void array :: makeheap(int c)
{

    for ( int i = 1 ; i < c ; i++ )
    {
        int val = arr[i] ;
        int s = i ;
        int f = ( s - 1 ) / 2 ;
        while ( s > 0 && arr[f] < val )
        {
            arr[s] = arr[f] ;
            s = f ;
            f = ( s - 1 ) / 2 ;
        }
        arr[s] = val ;
    }
}
void array :: heapsort( )
{
    for ( int i = count - 1 ; i > 0 ; i-- )
    {
        int ivalue = arr[i] ;
        arr[i] = arr[0] ;
        arr[0]=ivalue;
        makeheap(i);

    }
}
void array :: display( )
{
    for ( int i = 0 ; i < count ; i++ )
        cout << arr[i] << "\t" ;
    cout << endl ;
}
int main( )
{
    array a ;

    a.add ( 11 ) ;
    a.add ( 2 ) ;
    a.add ( 9 ) ;
    a.add ( 13 ) ;
    a.add ( 57 ) ;
    a.add ( 25 ) ;
    a.add ( 17 ) ;
    a.add ( 1 ) ;
    a.add ( 90 ) ;
    a.add ( 3  ) ;
    a.makeheap(10) ;
    cout << "\nHeap Sort.\n" ;
    cout << "\n\n\nBefore Sorting:\n"  ;
    a.display( ) ;
    a.heapsort( ) ;
    cout << "\n\n\nAfter Sorting:\n" ;
    a.display( ) ;
    getche();
}


Output

Bubble sort Algorithm and Source Code


#include <stdio.h>
#include <conio.h>

//return p,q in ascending order
void Order(int *p,int *q) {
  int temp;
  if(*p>*q) {
    temp=*p;
    *p=*q;
    *q=temp;
  }
}

//Buuble sorting of integer array A[]
void Bubble(int *a,int n) {
  int i,j;

  for (i=0; i<n; i++)
    for (j=n-1; i<j; j--)
      Order(&a[j-1], &a[j]);

}

int main() {
  int i,j,n=0;
  static int a[20];
  printf("Enter number of item you want to enter=\t\t");
  scanf("%d",&n);
  printf("\n\nEnter the elements=\n");
  for(j=0;j<n;j++)
  {
                  scanf("%d",&a[j]);
  }

  printf("\n\n Initial table A:\n");
  for(i=0; i<n; i++)
    printf(" %d ",a[i]);

  Bubble(a,n);

  printf("\n\n Sorted table A:\n");
  for(i=0; i<n; i++)
    printf(" %d ",a[i]);

  printf("\n\n");
  getche();
}

Output

Merge sort algorithm and source code

#include <stdio.h>
#include <math.h>
#include <conio.h>

  void sort(double *a, int lo, int hi) {

    if (lo>=hi) return; //no action
    int mid = (lo + hi) / 2;

    // Partition the list into two lists and sort them recursively
    sort(a, lo, mid);
    sort(a, mid + 1, hi);

    // Merge the two sorted lists
    int start_hi = mid + 1;
    int end_lo = mid;
    while ((lo <= end_lo) && (start_hi <= hi)) {
      if (a[lo] < a[start_hi])
        lo++;
      else {
      // a[lo] >= a[start_hi]
      // The next element comes from the second list,
      // move the a[start_hi] element into the next
      // position and shuffle all the other elements up.
        double T = a[start_hi];
        for (int k = start_hi - 1; k >= lo; k--) {
          a[k+1] = a[k];
        }
        a[lo] = T;
        lo++;
        end_lo++;
        start_hi++;
      }
    }
  }

int main() {
    int i,n=16;
    static double a[] = {4,3,1,67,55,8,0,4,-5,37,7,4,2,9,1,-1};

    printf("\n\n Initial table A:\n");
    for(i=0; i<n; i++) {
      printf(" %5.2f ",a[i]);
      if(i==7) printf("\n");
    }

    sort(a,0, n-1);

    printf("\n\n Sorted table A:\n");
    for(i=0; i<n; i++) {
      printf(" %5.2f ",a[i]);
      if(i==7) printf("\n");
    }
    printf("\n\n");
    getche();
  }
Output