Go to Top

Friday 30 September 2011

Utilities 8 - Stopwatch

Here's a simple program that can act as a Java Stopwatch Timer.

Java Program :

class stopwatch
{
 static void main()
 {
    long s=0,m=0,h=0;
     for(;;)
     {
       
         try
         {Thread.sleep(1000);}
         catch(InterruptedException e)
         {}
         s=s+1;
            if(s==60)
            {
                s=0;
                m=m+1;
                }
            if(m==60)
            {
                m=0;
                h=h+1;
            }
            System.out.print("\f");
            System.out.print("Timer : "+s+" : "+m+" : "+h);
        }
    }
}
           

Thursday 29 September 2011

Utilities 7 - Printing arrays

Having problems while printing arrays ? Use this simple program to print an array with a single statement.

Java Program : 

class array
{
 static void display(int a[])
 {
     for(int i=0;i<a.length;i++)
     System.out.print(a[i]+" ");
    }
    static void display(short a[])
 {
     for(int i=0;i<a.length;i++)
     System.out.print(a[i]+" ");
    }static void display(byte a[])
 {
     for(int i=0;i<a.length;i++)
     System.out.print(a[i]+" ");
    }static void display(long a[])
 {
     for(int i=0;i<a.length;i++)
     System.out.print(a[i]+" ");
    }static void display(float a[])
 {
     for(int i=0;i<a.length;i++)
     System.out.print(a[i]+" ");
    }static void display(double a[])
 {
     for(int i=0;i<a.length;i++)
     System.out.print(a[i]+" ");
    }static void display(char a[])
 {
     for(int i=0;i<a.length;i++)
     System.out.print(a[i]+" ");
    }static void display(boolean a[])
 {
     for(int i=0;i<a.length;i++)
     System.out.print(a[i]+" ");
    }
    static void display(String a[])
 {
     for(int i=0;i<a.length;i++)
     System.out.print(a[i]+" ");
    }
}

Next time you have to print an array, just type array.display(array_name); and you are done. No more annoying for and while loops now.

Syntax : array.display(array_name);

For example :

//Rest part is omitted. 
int[] my_array=new int[10];
array.display(my_array);


Visitor's Request - 1

Hi ! Lunik. I have seen your program and made a few changes to it.

Java Program : 

import java.io.*;
public class array_names
{
   public static void main()throws IOException
   {
       BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
       System.out.print("Enter The First Alphabet : ");
       char a = in.readLine().charAt(0),c;
       String[] n = new String[10];
       for(int i=0;i<10;i++)
       {
           System.out.print("\nEnter The Name One By One : ");
           n[i]=in.readLine();
        }
        System.out.println("\nThe Name(s) Whose First Alphabet Starts With "+a+" are : ");
        for(int j=0;j<10;j++)
        {
            c=n[j].charAt(0);
            if(c==a)
            System.out.println(n[j]+" ");
        }
    }
}

See, I have removed the String " s " and found two mistakes.
1) The first mistake was with the Buffer part. Your buffer " in " was probably not initialised before the loop started, so the first cell of your array went blank, generating an IndexOutofBounds error when you went for checking.

2) Secondly, your character " c " needed to be initialised before the loop "j".

3) Extra : I have further beautified your program. Please check and comment.

Wednesday 28 September 2011

Utilities 6 - Playing housie

So, you have no one to pick up the lot numbers, or do all of your friends want to play ? Java to the rescue again. This java program will help you to pick random numbers from the lot.

Java Program : 

import java.io.*;
import java.util.*;
class housie
{
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    static int n1,n2;
    static int[] a;
    static Random r=new Random();
    static void main()throws IOException
    {
         input();
         iterate();
    }
    static void input()throws IOException
    {
        int c1=0,i;
        System.out.print("Enter the lower limit of lot numbers : ");
        n1=Integer.parseInt(br.readLine());
        System.out.print("\nEnter the upper limit of lot numbers : ");
        n2=Integer.parseInt(br.readLine());
        a=new int[(n2-n1)+1];
        for(i=n1;i<=n2;i++)
        {
            a[c1]=i;
            c1++;
        }
    }
    static void iterate()throws IOException
    {
        int i,c,l=a.length;
        String s;
        for(i=0;i<l;i++)
        {
            c=r.nextInt(a.length);
            if(a[c]<0)
            l++;
            else
            {
                System.out.print("\nLucky number is : "+a[c]);
                s=br.readLine();
                a[c]=-1;
            }
        }
    }
}

Again, just put in the upper and lower limit of the lots numbers and press return. CLICK HERE to know hoe to generate random housie tickets.

Utilities 5 - Housie ticket generator

You all are familiar with this popular game named Housie, where the players are provided with tickets with random numbers on them. A player calls out the number that he picks from a bag of number chits and the players, on getting the numbers in their tickets mark them with a pen or something. The lucky one with all the numbers marked out yells "House full", thus declaring his win. As you see, it becomes quite tedious, to write the tickets, when you don't have them. Java to the rescue. This program will help you to generate random housie tickets :

Java Program : 

import java.io.*;
import java.util.*;
class housie_ticket
{
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    static int l,n1,n2,n3;
    static int[] a,b;
    static void main()throws IOException
    {
        input();
        init();
        random();
    }
    static void input()throws IOException
    {
        System.out.print("Enter the lower limit of lot numbers : ");
        n1=Integer.parseInt(br.readLine());
        System.out.print("\nEnter the upper limit of lot numbers : ");
        n2=Integer.parseInt(br.readLine());
        System.out.print("\nEnter no. of tickets : ");
        n3=Integer.parseInt(br.readLine());
        for(int i=0;;i++)
        {
            System.out.print("\nEnter the no. of lucky numbers in a ticket : ");
            l=Integer.parseInt(br.readLine());
            if(l>=((n2-n1)+1))
            System.out.print("\nError (No. of lucky number cannot be more than limit)\n");
            else
            break;
        }
        a=new int[(n2-n1)+1];
    }
    static void init()
    {
        int i,c1=0;
        for(i=n1;i<=n2;i++)
        {
            a[c1]=i;
            c1++;
        }
    }
    static void random()
    {
        Random r=new Random();
        int i,j,k,c,c1=0;
        for(i=1;i<=n3;i++)
        {
            b=new int[l];
            for(j=0;j<l;j++)
            {
                c=r.nextInt(a.length);
                if(a[c]!=-1)
                {
                    b[j]=a[c];
                    a[c]=-1;
                }
                else
                j--;
            }
            init();
            System.out.print("\n\nTicket no. "+i+" : \n\n");
            c1=0;
            for(k=0;k<l;k++)
            {
                System.out.print(b[k]+"  ");
                c1++;
                if(c1==10)
                {
                    System.out.print("\n");
                    c1=0;
                }
            }
        }
    }
}
 
Just put int the number of tickets, the upper limit and the lower limit, and you're done. Copy them onto a piece of paper and get set to play. In my next post, I'll be posting a java program to help you to pick up the random numbers from the lot.

CLICK HERE to know how to use your computer to help you out in this game.

To print maximum and minimum numbers

Question 45 : Write a program in Java to print the maximum and minimum of the numbers entered in the form of a list by the user. Program should display the results when the user enters 0.

Java Program : 

import java.io.*;
class max_min
{
    public static void main()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter any number or '0' to exit\n\n");
        int n=0,i=9;
        System.out.print("Enter a number : ");
        n=Integer.parseInt(br.readLine());
        System.out.print("\n");
        int max=n,min=n;
        do
        {
            if(n>max)
            max=n;  
            if(n<min)
            min=n;
            System.out.print("Enter a number : ");
            n=Integer.parseInt(br.readLine());
            System.out.print("\n");
            if(n==0)
            i=n;
        }while(i!=0);
        System.out.print("The maximum number is : "+max+"\n");
        System.out.println("\nThe minimum number is : "+min);
    }
}

Sunday 25 September 2011

Selection Sort Descending

Java Program :

class selsort_d
{
 static void sort(int a[])
 {
     int i,j,c,k;
     for(i=0;i<a.length-1;i++)
     {
         c=i+1;
         for(j=0;j<a.length-1-i;j++)
         {
             if(a[i]<a[c])
             {
                 k=a[c];
                 a[c]=a[i];
                 a[i]=k;
                }
                c++;
            }
        }
        System.out.print("Sorted array : ");
        for(i=0;i<a.length;i++)
        System.out.print(a[i]+" ");

    }
}

Selection Sort Ascending

Java Program :

class selsort_a
{
 static void sort(int a[])
 {
     int i,j,c,k;
     for(i=0;i<a.length-1;i++)
     {
         c=i+1;
         for(j=0;j<a.length-1-i;j++)
         {
             if(a[i]>a[c])
             {
                 k=a[c];
                 a[c]=a[i];
                 a[i]=k;
                }
                c++;
            }
        }
        System.out.print("Sorted array : ");
        for(i=0;i<a.length;i++)
        System.out.print(a[i]+" ");
    }
}

Bubble Sort Descending

Java Program :

class descending
{
 static void compute(int[] a)
 {
     int i,j,k;
     System.out.print("Original array : ");
     array.display(a);
     System.out.print("\n\nLength : "+a.length);
     for(i=0;i<a.length-1;i++)
     {
         for(j=0;j<a.length-1;j++)
         {
             if(a[j]<a[j+1])
             {
                 k=a[j];
                 a[j]=a[j+1];
                 a[j+1]=k;
                }
            }
        }
        System.out.print("\n\nDescending data : ");
        for(i=0;i<a.length;i++)
        System.out.print(a[i]+" ");

    }
}

Bubble Sort Ascending

Java Program : 

class ascending
{
 static void compute(int[] a)
 {
     int i,j,k;
     System.out.print("Original array : ");
     array.display(a);
     System.out.print("\n\nLength : "+a.length);
     for(i=0;i<a.length-1;i++)
     {
         for(j=0;j<a.length-1;j++)
         {
             if(a[j]>a[j+1])
             {
                 k=a[j];
                 a[j]=a[j+1];
                 a[j+1]=k;
                }
            }
        }
        System.out.print("\n\nAscending data : ");
        for(i=0;i<a.length;i++)
        System.out.print(a[i]+" ");

    }
}

Linear Search

Java Program for Linear Search : 

Given an array "a" containing integers, and a value "key", write a function which takes these as parameters and performs a linear search to determine whether "key" exists in "a" or not.

class lsearch
{
 static void search(int[]a,int key)
 {
     int p=-1,i;
     for(i=0;i<=a.length-1;i++)
     {
         if(a[i]==key)
         p=i;
      }
     if(p==-1)
     System.out.print("Search key '"+key+"' not found");
     else
     System.out.print("Search key '"+key+"' found at : Position "+(p+1));
    }
}

Binary search

Java Program for Binary Search : 


class bsearch
{
 static void search(int[]a,int key)
 {
     int p=-1,h=a.length-1,l=0,m;
     while(p==-1 && h>=l)
     {
         m=(h+l)/2;
         if(a[m]>key)
         h=m-1;
         if(a[m]<key)
         l=m+1;
         if(a[m]==key)
         p=m;
        }
     if(p==-1)
     System.out.print("Search key '"+key+"' not found");
     else
     System.out.print("Search key '"+key+"' found at : Position "+(p+1));
    }
}

Monday 12 September 2011

Applet 4 : Change Background

Just click on one of those "colour blocks" and see the colour of the background change.


Sunday 11 September 2011

Applet 3 : Colour change



Applet 2 : Clicked

Just click inside the box and you'll see a log of all your actions.


Friday 9 September 2011

To check whether a number is a Krishna Murthy number or not

Question 44 : Write a program in Java to check whether the number given by the user is a Krishna Murthy number or not.

Krishna Murthy Number : It is a number which is equal to the sum of the factorials of all its digits.

For example : 145 = 1! + 4! + 5! = 1 + 24 + 120 = 145

Java Program : 

import java.io.*;
class krishnamurthy
{
 int fact(int n)
 {
     int i,p=1;
     for(i=n;i>=1;i--)
     p=p*i;
     return p;
    }
    void krishna()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int a,b,s=0;
        System.out.print("Enter the number : ");
        a=Integer.parseInt(br.readLine());
        System.out.print("\n");
        int n=a;
        while(a>0)
        {
            b=a%10;
            s=s+fact(b);
            a=a/10;
        }
        if(s==n)
        System.out.print(n+" is a krishnamurthy number");
        else
        System.out.print(n+" is not a krishnamurthy number");
    }
}

Tips & Tricks - 3 - How to create a folder in Windows XP without a name

Hi amigos! Now this is a very easy but very useful trick to baffle your friends. Let us see how to create a folder in Windows XP. Follow the simple steps given below :

1) Firstly, create a folder in any location.

2) Then rename the folder, and enter Alt + 255 (The numbers should be entered from the Numpad. Laptop users can use the Fn button to enter the numbers written in superscript (at a high level) over the keyboard buttons like U,I,O,P etc.

3) You'll see a space kind of thing in the text box, as the I-Cursor (that blinking one) shifts a bit. (You can ask me why I didn't put a space their in the text box instead of all these, but that won't work. The previous folder name will again re-appear).

4) And you are done. Press enter or return to complete the creation of a folder without a name in XP.

Bonus point :

For additional fun, you can even change the icon picture of this folder to make it invisible. Just follow the steps given below :

1) Right click the folder and click on "Properties".

2) Go to the "Customize" tab.

3) Click on the "Change Icon..." tab at the bottom.

4) Search and select the invisible icon picture (just a blank picture) and click on "Ok".

5) Click on "Ok" or "Apply" whichever button and there you go. The folder is invisible.

Warning : Be sure that you don't Refresh your computer while your friends are around because that will make that invisible folder visible for a moment and you'll make a fool of of yourself.

To revert back to normal : Just rename the folder and change the icon picture in the same way as described above or click here. Or else just delete that folder (if it is useless) and create a new one.

Comment if you liked this post.

Wednesday 7 September 2011

Utilities 4 - Captcha

This is just a small code resembling the Captcha codes seen on different websites.

Java Program : 


import java.io.*;
import java.util.Random;
class captcha
{
 static String s,capt="",data="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 static void main()throws IOException
 {
     capt="";
     System.out.print("\f");
     generate();
     input();
     match();
    }
    static void generate()
    {
        Random r=new Random();
        char k;
        int i,c,l=data.length();
        System.out.print("Captcha : ");
        for(i=1;i<=5;i++)
        {
            c=r.nextInt(l);
            k=data.charAt(c);
            capt=capt+k;
            System.out.print(k+" ");
        }
    }
    static void input()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("\n\nPlease enter the code as you see above( w/o spaces ) : ");
        s=br.readLine();
    }
    static void match()throws IOException
    {
        if(s.equals(capt))
        System.out.print("\nCaptch match");
        else
        {
            System.out.print("\nCaptcha mismatch");
            try
            {
            Thread.sleep(3000);}
            catch(InterruptedException e)
            {}
            main();
        }
    }
}

Monday 5 September 2011

Applet 1 : A bouncing ball

Bouncing Ball Applet



That what you see is my first java applet. To download the source code, just click on those links below :

Download Link 1 : Download

Download Link 2 : Download

Please comment if you like the applet or even if you don't.

Thursday 1 September 2011

Re-joining notice : Back on track

                           Phew ! The exams are finally over. These exams seriously exhausted me. But now that its over, I will back to more blogging.

ShareThis