Go to Top

Saturday 31 December 2011

Happy New Year !

Sunday 25 December 2011

Merry Christmas !



Friday 23 December 2011

To manipulate vowels in a string

Question 72 : Write a program in Java to accept a word and do the following : 

1) If the word starts with a vowel, then move the vowel to the end of the word and add "ay" to it. 
Example : owl -> wloay.
2) If there are no vowels in the word, then just add "ay" to it.
Example : sky -> skyay.
3) If the vowel lies in between, then remove all the letters from the beginning till the vowel and add them at the end of the word. Finally, attach "ay" to it.
Example : stop -> opstay.

Java Program :

import java.io.*;
class vowel_changer
{
    static String s,s1;
   static void main()throws IOException
   {
       input();
       manipulate();
       display();
    }
  static void input()throws IOException
  {
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Enter a word : ");
      s=br.readLine();
    }
    static void manipulate()
    {
        int i,c=0;
        char k;
        for(i=0;i<s.length();i++)
        {
            k=s.charAt(i);
            if(k=='a' || k=='A' || k=='e' || k=='E' || k=='i' || k=='I' || k=='o' || k=='O' || k=='u' || k=='U')
            {
                if(i==0)
                s1=s.substring(1,s.length())+k+"ay";
                else
                s1=s.substring(i,s.length())+s.substring(0,i)+"ay";
                c=1;
                break;
            }
        }
        if(c==0)
        s1=s+"ay";
    }
    static void display()
    {
        System.out.print("\nManipulated String : "+s1);
    }
}
                 

Wednesday 21 December 2011

Utilities 26 : The nice old cups game

Recently, I had made a nice cups game. Most of you would be familiar with the old cups game, where a ball is kept inside one of the three cups, and while the cups are interchanged amongst themselves, you have to look out for the position of the ball inside that particular cup.

I had posted that game within a package, if you don't remember it, click here. I am posting the game individually now. Enjoy.

Java Program :

/*
 *      _____
 *     |     |              
 *     |     |
 *     |     |
 */

import java.io.*;
import java.util.Random;
class cups
{
 static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 static String feed="",err="",garb,s1,s2,s="      _____       _____       _____\n     |     |     |     |     |     |\n     |     |     |     |     |     |\n     |     |     |     |     |     |";
 static Random r=new Random();
 static int c,pos,time=900,score=0,lvl=1,iter=2,ded=0,ff=0,choice;
 static void main()throws IOException
 {
    control();
}    
 static void control()throws IOException
 {
     String choice=null;
    System.out.print("\fCups : The Game");
        System.out.print("\n\n1: New Game\n2: View score\n3: Visual demo\n4: Help\n5: Feedback\n6: Report an error\n7: Exit");
        System.out.print("\n\nEnter your choice : ");
        choice=br.readLine();
        if(choice.equals(""))
        choice=" ";
        switch(choice.charAt(0))
        {
            case '1':
            cont();
            break;
            case '2':
            details();
            control();
            break;
            case '3':
            viddem();
            break;
            case '4':
            help();
            break;
            case '5':
            feedback();
            break;
            case '6':
            error();
            break;
            case '7':
            exit();
            break;
            case 'q':
            case 'Q':
            viewer();
            break;
            default:
            System.out.print("\nInput error (Invalid input). Press enter to continue : ");
            garb=br.readLine();
            control();
}
}
    static void cont()throws IOException
    {
        String choice=" ";
        if(lvl!=1)
        {
        System.out.print("\fPress enter to continue or 'm' to initiate in-game menu : ");
        choice=choice+br.readLine();
    }
        if(choice.equals(" "))
        {
            gamerand();
            randmove();
            input();
        }
        else
        {
             System.out.print("\fIn-Game Menu\n\n1: Continue\n2: View score\n3: Restart\n4: Exit");
            System.out.print("\n\nEnter your choice : ");
            choice=br.readLine();
            switch(choice.charAt(0))
            {
                case '1':
                gamerand();
                randmove();
                input();
                break;
                case '2':
                details();
                cont();
                break;
                case '3':
                reset();
                break;
                case '4':
                exit();
                break;
                default:
                System.out.print("\nInput error (Invalid input). Press enter to continue : ");
                garb=br.readLine();
                cont();
            }
        }
    }
 static void gamerand()throws IOException
 {
     System.out.print("\fStage : "+lvl+"\n\n");
     c=r.nextInt(4);
     if(c==0)
     gamerand();
     else
     balldisp();
    }
    static void balldisp()throws IOException
    {
        if(c==1)
        {
            System.out.print("      _____       _____       _____\n     |     |     |     |     |     |\n     |  O  |     |     |     |     |\n     |     |     |     |     |     |");
            pos=1;
         }
        if(c==2)
        {
            System.out.print("      _____       _____       _____\n     |     |     |     |     |     |\n     |     |     |  O  |     |     |\n     |     |     |     |     |     |");
            pos=2;
         }
        if(c==3)
        {
            System.out.print("      _____       _____       _____\n     |     |     |     |     |     |\n     |     |     |     |     |  O  |\n     |     |     |     |     |     |");
            pos=3;
         }
        sleep(2000);
        System.out.print("\f");
    }
    static void randmove()throws IOException
        {      
        System.out.print(s);
        for(int i=1;i<iter;i++)
        {
            int c=r.nextInt(7);
            switch(c)
            {
                case 1:
                if(pos==1)
                pos=2;
                else if(pos==2)
                pos=1;
                move12();
                break;
                case 2:
                if(pos==2)
                pos=3;
                else  if(pos==3)
                pos=2;
                move23();
                break;
                case 3:
                if(pos==1)
                pos=3;
                else  if(pos==3)
                pos=1;
                move13();
                break;
                case 4:
                if(pos==1)
                pos=2;
                else  if(pos==2)
                pos=1;
                move21();
                break;
                case 5:
                if(pos==2)
                pos=3;
                else  if(pos==3)
                pos=2;
                move32();
                break;
                case 6:
                if(pos==1)
                pos=3;
                else  if(pos==3)
                pos=1;
                move31();
                break;
                default:
                i=i-1;
            }  
        }
    }
    static void input()throws IOException
    {
        int c=0;
        System.out.print("\n\n\nWhere is the ball ? : ");
        try
        {
            c=Integer.parseInt(br.readLine());
       }
        catch(Exception e)
        {
            ff=ff+1;
            System.out.print("\nLevel forfeited (Forfeits "+ff+" of 3) . Press enter to restart : ");
            garb=br.readLine();
            if(ff==3)
            {
                System.out.print("\nForfeit limit reached. Game over. Press enter to view score : ");
                garb=br.readLine();
                details();
                control();
            }
            cont();
        }
        if(c!=1235)
        {
        if(c==pos)
        {
        System.out.print("\nCorrect Answer");
        score=score+10;
    }
    if(c!=pos)
    System.out.print("\nWrong answer. The ball is in cup "+pos);
    System.out.print("\n\nPress enter to continue : ");
    garb=br.readLine();}
    level();
}  
static void level()throws IOException
{
  lvl=lvl+1;
  if(lvl<5)
  time=time-200;
  if(lvl==5)
  time=time-120;
  iter=iter+1;
  cont();
 }
 static void move12()
 {
     sleep(time);
     s1="\f                  _____       _____\n                 |     |     |     |\n                 |     |     |     |\n                 |     |     |     |\n      _____\n     |     |\n     |     |\n     |     |";
     System.out.print(s1);  
     sleep(time);
     s1="\f                  _____       _____\n                 |     |     |     |\n                 |     |     |     |\n                 |     |     |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |";
     System.out.print(s1);
     sleep(time);
     s1="\f      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |";
     System.out.print(s1);
     sleep(time);
     s1="\f      _____       _____       _____\n     |     |     |     |     |     |\n     |  1  |     |  2  |     |  3  |\n     |     |     |     |     |     |";
  System.out.print(s1);
    }
    static void move23()
 {
        sleep(time);
        s1="\f      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n                              _____\n                             |     |\n                             |     |\n                             |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____       _____\n     |     |     |     |\n     |     |     |     |\n     |     |     |     |\n                              _____\n                             |     |\n                             |     |\n                             |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____       _____       _____\n     |     |     |     |     |     |\n     |  1  |     |  2  |     |  3  |\n     |     |     |     |     |     |";
        System.out.print(s1);
    }
    static void move13()
    {
        sleep(time);
        s1="\f                  _____\n                 |     |\n                 |     |\n                 |     |\n      _____\n     |     |\n     |     |\n     |     |\n                              _____\n                             |     |\n                             |     |\n                             |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f                  _____\n                 |     |\n                 |     |\n                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f                  _____\n                 |     |\n                 |     |\n                 |     |\n                              _____\n                             |     |\n                             |     |\n                             |     |\n      _____\n     |     |\n     |     |\n     |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____       _____       _____\n     |     |     |     |     |     |\n     |  1  |     |  2  |     |  3  |\n     |     |     |     |     |     |";
        System.out.print(s1);
    }
    static void move21()
    {
        sleep(time);
        s1="\f      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n      _____\n     |     |\n     |     |\n     |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f                  _____       _____\n                 |     |     |     |\n                 |     |     |     |\n                 |     |     |     |\n      _____\n     |     |\n     |     |\n     |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____       _____       _____\n     |     |     |     |     |     |\n     |  1  |     |  2  |     |  3  |\n     |     |     |     |     |     |";
        System.out.print(s1);
    }
    static void move32()
    {
        sleep(time);
         s1="\f      _____       _____\n     |     |     |     |\n     |     |     |     |\n     |     |     |     |\n                              _____\n                             |     |\n                             |     |\n                             |     |\n";
        System.out.print(s1);
        sleep(time);
         s1="\f      _____       _____\n     |     |     |     |\n     |     |     |     |\n     |     |     |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |\n";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____       _____       _____\n     |     |     |     |     |     |\n     |  1  |     |  2  |     |  3  |\n     |     |     |     |     |     |";
        System.out.print(s1);
    }
    static void move31()
    {
        sleep(time);
        s1="\f                  _____\n                 |     |\n                 |     |\n                 |     |\n                              _____\n                             |     |\n                             |     |\n                             |     |\n      _____\n     |     |\n     |     |\n     |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f                  _____\n                 |     |\n                 |     |\n                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |\n                  _____\n                 |     |\n                 |     |\n                 |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f                  _____\n                 |     |\n                 |     |\n                 |     |\n      _____\n     |     |\n     |     |\n     |     |\n                              _____\n                             |     |\n                             |     |\n                             |     |";
        System.out.print(s1);
        sleep(time);
        s1="\f      _____       _____       _____\n     |     |     |     |     |     |\n     |  1  |     |  2  |     |  3  |\n     |     |     |     |     |     |";
        System.out.print(s1);
    }
        static void sleep(int c)
  {
      try{Thread.sleep(c);}
      catch(Exception e){}
    }
    static void details()throws IOException
    {
        double fps=1/time;
        System.out.print("\fYour are in level : "+lvl+"\nYour current score is : "+score+"\nFPS : "+fps+"\nNo. of shuffles : "+(iter-1));
        System.out.print("\n\nPress enter to continue : ");
        garb=br.readLine();
    }
    static void exit()
    {
        System.out.print("\nThanks for trying out Cups : The Game");
        System.exit(0);
    }
    static void reset()throws IOException
    {
        time=950;
        lvl=1;
        ded=0;
        iter=2;
        score=0;
        System.out.print("\fGame reset. Press enter to continue : ");
        garb=br.readLine();
        control();
    }
    static void error()throws IOException
    {
        System.out.print("\fPlease type the error : ");
        err=err+br.readLine();
        System.out.print("\nThank you. Your report has been sent for rectification.");
        err=err+" | ";
        System.out.print("\n\nPress enter to continue : ");
        garb=br.readLine();
        control();
    }
    static void feedback()throws IOException
    {
        System.out.print("\fPlease enter your feedback : ");
        feed=feed+br.readLine();
        System.out.print("\nThank you for your feedback.");
        feed=feed+" | ";
        System.out.print("\n\nPress enter to continue : ");
        garb=br.readLine();
        control();
    }
    static void viewer()throws IOException
    {
        System.out.print("\fReports : ");
        if(err!="")
        System.out.print(err);
        else
        System.out.print("No reports submitted yet");
        System.out.print("\n\nFeedbacks : ");
        if(feed!="")
        System.out.print(feed);
        else
        System.out.print("No feedbacks submitted yet");
        System.out.print("\n\nPress enter to continue : ");
        garb=br.readLine();
        if(garb.equalsIgnoreCase("r"))
        {    
            err="";
            viewer();
        }
        if(garb.equalsIgnoreCase("f"))
        {    
            feed="";
            viewer();
        }
        control();
    }
    static void viddem()throws IOException
    {
        System.out.print("\fWelcome to the video demo\n\nThis demo will teach you how to master the game step-by-step\n\nPress enter to continue : ");
        garb=br.readLine();
        System.out.print("\fStep 1 : Look at the initial arrangement carefully\n\n");
        System.out.print("      _____       _____       _____\n     |     |     |     |     |     |\n     |     |     |  O  |     |     |\n     |     |     |     |     |     |\n");
        sleep(4000);
        for(int i=1;i<3;i++)
        {
            System.out.print("                    |\n");
            sleep(500);
        }
        sleep(500);
        System.out.print("                     -");
        sleep(500);
        for(int i=1;i<5;i++)
        {
           System.out.print("-");
           sleep(500);
        }
        sleep(50);
        System.out.print("> See, the ball is at position 'two'");
        System.out.print("\n\nNext step : ");
        garb=br.readLine();
        System.out.print("\fStep 2 : Keep your eyes onto the ball as the cups move\n");
        s1="\n\n      _____       _____       _____\n     |     |     |     |     |     |\n     |     |     |  O  |     |     |\n     |     |     |     |     |     |";
        System.out.print(s1);
        sleep(4000);
        System.out.print("\fStep 2 : Keep your eyes onto the ball as the cups move\n");
        s1="\n\n      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n                  _____\n                 |     |\n                 |  O  |\n                 |     |";
        System.out.print(s1);
        sleep(900);
        System.out.print("\fStep 2 : Keep your eyes onto the ball as the cups move\n");
        s1="\n\n      _____                   _____\n     |     |                 |     |\n     |     |                 |     |\n     |     |                 |     |\n                              _____\n                             |     |\n                             |  O  |\n                             |     |";
        System.out.print(s1);
        sleep(900);
        System.out.print("\fStep 2 : Keep your eyes onto the ball as the cups move\n");
        s1="\n\n      _____       _____\n     |     |     |     |\n     |     |     |     |\n     |     |     |     |\n                              _____\n                             |     |\n                             |  O  |\n                             |     |";
        System.out.print(s1);
        sleep(900);
        System.out.print("\fStep 2 : Keep your eyes onto the ball as the cups move\n");
        s1="\n\n      _____       _____       _____\n     |     |     |     |     |     |\n     |     |     |     |     |  O  |\n     |     |     |     |     |     |";
        System.out.print(s1);
        System.out.print("\n\nNext step : ");
        garb=br.readLine();
        System.out.print("\fStep 3 : Now you know where the ball is hiding ? Hit the right key and press enter\n\n");
        System.out.print(s);
        System.out.print("\n\n\nWhere is the ball ? : ");
        sleep(5000);
        System.out.print("3");
        sleep(900);
        System.out.print("  >CLICK< ");
        sleep(900);
        System.out.print("\n\nCorrect Answer");
        sleep(900);
        System.out.print("\n\nYipeee !");
        sleep(900);
        System.out.print("\n\nNext step : ");
        garb=br.readLine();
        System.out.print("\fNow you are all ready to begin. Press enter to return to main menu : ");
        garb=br.readLine();
        control();
    }
    static void help()throws IOException
    {
        System.out.print("\fWelcome to the help guide for Cups : The Game");
        System.out.print("\n\nThe game has virtually unlimited levels and its up to you when you forfeit.");
        System.out.print("\n\nA 2 second clip will show you the position of the ball hidden in the cups.\nThen the cups will be randomly shuffled.\n");
        System.out.print("\nYou'll have to keep track of the ball and when the shuffling is over,\nenter your response by entering the cups numbers 1,2,3 respectively for each cup.");
        System.out.print("\nTry to enter the correct answer, and get rewarded with points.");
        System.out.print("\n\nAs you cross levels, the game become tougher, with more speed of shuffling the cups,\nand more number of shuffles.");
        System.out.print("\n\nGood luck and try to beat them all");
        System.out.print("\n\nPress enter to continue : ");
        garb=br.readLine();
        control();
    }
}
         

Friday 16 December 2011

Libraries 3 : Mod function

Here's the replica of the widely used Mod Function (%). You may even use it in your program as :

// Rest part of the program is omitted
int m=39,n=7,result;
result=mod.cal(m,n);
System.out.print(result);

Java Program :

class mod
{
 static int cal(int number,int mod)
 {
     for(;;)
     {
         if(number>=mod)
         number=number-mod;
         else
         return number;
        }
    }
}

Thursday 15 December 2011

Utilities 25 : A nice maths trick

Hello everyone ! This nice program will play a nice maths trick on you, leaving you bewildered for a short time and curious for the rest period, to know its formula. Instructions will be provided by the program itself. Enjoy another nice program by ICSE Java.

Java Program :

import java.io.*;
class trick
{
  static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  static String garb;
  static int s;
  static void main()throws IOException
  {
      help();
      input();
      cal();
    }
    static void help()throws IOException
    {
        System.out.print("Welcome to Maths Magic.\n\nFor this trick, you need to think of a three digit number.");
        System.out.print("\n\nEnter when you are ready : ");
        garb=br.readLine();
        System.out.print("\fNow subtract the sum of digits from the original number.\n\nEx.: If you select 943, then subtract (9 + 4 + 3) from 943");
        System.out.print("\ni.e. 943 - (9 + 4 + 3) = 943 - 16 = 927.\n\nEnter when you are ready : ");
        garb=br.readLine();
    }
    static void input()throws IOException
    {
        s=0;
        System.out.print("\fNow enter any two of its digits, and I'll tell the third digit.\n\nEnter when you are ready : ");
        garb=br.readLine();
        try{
            System.out.print("\nEnter 1st digit : ");
            s=s+Integer.parseInt(br.readLine());
            System.out.print("\nEnter 2nd digit : ");
            s=s+Integer.parseInt(br.readLine());
        }
        catch(Exception e){
            System.out.print("\nError. Please try again : ");
            garb=br.readLine();
            System.out.print("\f");
            input();
        }
    }
    static void cal()
    {
        int i;
        for(i=0;i<=9;i++)
        if((s+i)%9==0)
        System.out.print("\nThe third digit is : "+i);
    }
}
        

Wednesday 14 December 2011

To calculate the Compound Interest (C.I.)

Question 71 : Write a program in Java to find out Compound Interest (C. I.) on the basis of inputs accepted from the user.

Java Program :

import java.io.*;
class compound_interest
{
 static void main()throws IOException
 {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     double p,r,t,a=0,n;
     System.out.println("\t\t*****Input Data*****\n");
     System.out.print("Enter the principal amount : Rs. ");
     p=Double.parseDouble(br.readLine());
     System.out.print("\nEnter the rate of interest (w/o % sign) : ");
     r=Double.parseDouble(br.readLine());
     System.out.print("\nEnter the time in yrs. : ");
     t=Double.parseDouble(br.readLine());
     System.out.print("\nHow many times a year is the interest calculated (Ex.: Half-yearly : 2 times) ? : ");
     n=Double.parseDouble(br.readLine());
     a=p*(Math.pow((1+(r/(n*100))),(t*n)));
     System.out.println("\n\t\t*****Results*****\n");
     System.out.println("Compound interest : Rs. "+(a-p));
     System.out.println("\nAmount : Rs. "+a);
    }
} 

Sunday 11 December 2011

Utilities 24 : Percentage grapher

This program will represent any number as a percentage of another number in a graphical way. Just compile and see.

Java Program :

import java.io.*;
class per_graph
{
 static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 static int v1,v2,p;
    static void input()throws IOException
    {
        System.out.print("Enter the value : ");
        v1=Integer.parseInt(br.readLine());
        System.out.print("\n"+v1+" is out of : ");
        v2=Integer.parseInt(br.readLine());
        cal();
    }
    static void cal()
    {
        p=(v1*100)/v2;
        display();
    }
    static void display()
    {
        int i;
        System.out.print("\f|");
        for(i=1;i<=100;i++)
        System.out.print("-");
        System.out.print("|\n|");
        for(i=1;i<=p;i++)
        System.out.print("=");
        for(i=1;i<=(100-p);i++)
        System.out.print(" ");
        System.out.print("|\n|");
        for(i=1;i<=100;i++)
        System.out.print("-");
        System.out.print("|\n\n\t\t\t\t\t"+p+" % out of 100 %");
    }
}
    

Thursday 8 December 2011

To check whether two numbers are amicable

Question 70 : Write a program in Java to check whether two numbers are amicable or not.

Amicable Number : If two numbers are such that the sum of the perfect divisors of one number is equal to the other number and the sum of the perfect divisors of  the other number is equal to the first number, then the numbers are called Amicable Numbers.

Example : 220 and 284.

Java Program :

class amicable
{
 static int a,b;
 static void input(int m,int n)
 {
     a=m;
     b=n;
     display();
    }
    static boolean check()
    {
        int s=0,i;
        for(i=1;i<a;i++)
        {
            if(a%i==0)
            {
                s=s+i;
            }
        }
        if(s==b)
        {
            s=0;
            for(i=1;i<b;i++)
            {
                if(b%i==0)
                {
                    s=s+i;
                }
            }
            if(s==a)
            return true;
            else
            return false;
        }
        return false;
    }
    static void display()
    {
        if(check())
        System.out.print("The numbers are amicable");
        else
        System.out.print("The numbers are not amicable");
}
}

Monday 28 November 2011

Utilities 23 : Number to Word Converter

Hello friends ! Today I am going to present a unique Number to Word Converter, made in Java. This will allow you to convert any number, long, short, very long, negative, decimal etc. to words in the Indian notation. For example :

Sample input : 132433

Sample output : One lakh thirty two thousand four hundred thirty three only.

This project is still under development, but I assure you very less and minor errors. In case of any error, please report it to us in the comments section.

To view the program, simply click on the link below :

View Here

To download the program, simply click on the link below : 


Friday 25 November 2011

To print the given pattern

Question 69 : Write a program in Java to print the given pattern : 

    *****
   *       *
  *         *
 *           *
*             *
 *           *
  *         *
   *       *
    *****

(Spacing in Blogger is not as in Java. Note that spacing inside the hexagon is in the order : 5, 7, 11, 13, 11, 7, 5)

Java Program :

class pattern_39
{
 static void print(int n)
 {
     int i,j,k,m=0;
     for(i=1;i<n;i++)
     System.out.print(" ");
     for(i=1;i<=n;i++)
     System.out.print("*");
     System.out.print("\n");
     for(i=1;i<n;i++)
     {
         for(j=1;j<(n-i);j++)
         System.out.print(" ");
         System.out.print("*");
         m=(n+(i*2-1));
         for(k=1;k<m;k++)
         System.out.print(" ");
         System.out.print("*");
         System.out.print("\n");
        }
        m=m-1;
        for(i=1;i<n-1;i++)
     {
         for(j=1;j<=i;j++)
         System.out.print(" ");
         System.out.print("*");
         for(k=1;k<(m-(i*2-1));k++)
         System.out.print(" ");
         System.out.print("*");
         System.out.print("\n");
        }
     for(i=1;i<n;i++)
     System.out.print(" ");
     for(i=1;i<=n;i++)
     System.out.print("*");
    }
}
         

Thursday 24 November 2011

To print the given pattern

Question 68 : Write a program in Java to print the given pattern : 

ABCDEFEDCBA
ABCDE  EDCBA
ABCD       DCBA
ABC             CBA
AB                   BA
A                         A

(Spacing in Blogger is not as in Java. Note that spacing is in the order : nil, 1, 3, 5, 7...)

Java Program :

class pattern_37
{
 static void pattern(int len)
 {
     int n=65,i,j,k;
     String j1="";
     for(i=len;i>=1;i--)
     {
         n=65;
         for(j=0;j<i;j++)
         System.out.print((char)(n+j));
         System.out.print(j1);
         j1=j1+"  ";
         for(k=i-1;k>=0;k--)
         {
             if(i==len && k==i-1)
             k=k-1;
            System.out.print((char)(n+k));
        }
         System.out.print("\n");
         if(i==len)
         j1=" ";
        }
    }
}

Monday 21 November 2011

Utilities 22 : Advanced Telephone E-Billing System

These days, I was busy with this program, so I had less time for blogging. But as faithful readers, you have the right to see how this program works. This is an E-Billing system, with many features. See the help topics on the program for help.

So I am going to share my whole Computer Science Project with you. It contains an additional five programs and total documentation for the E-Billing program.

For the whole project :



For only the E-Billing program :



Saturday 19 November 2011

To find the sum of even and odd numbers

Question 68 : Write a program in Java to find the sum of odd and even numbers between a range.

Java Program : 

public class sum_range
{
 int e,o,l,u;
 public sum_range(int lower_limit,int upper_limit)
 {
     l=lower_limit;
     u=upper_limit;
     e=0;
     o=0;
     sum();
     display();
    }
    public void sum()
    {
        for(int i=l;i<=u;i++)
        {
            if(i%2==0)
            even(i);
            else
            odd(i);
        }
    }
    public void odd(int a)
    {
        o=o+a;
    }
    public void even(int a)
    {
        e=e+a;
    }
    public void display()
    {
        System.out.print("The sum of even numbers : "+e);
        System.out.print("\nThe sum of odd numbers : "+o);
    }
}
    

Wednesday 16 November 2011

Utilities 21 : Java Simple Virus

Start it and it will keep going on.

Java Program :

class virus
{
 static void main()
 {
     for(;;)
     {}
    }
}

Wednesday 9 November 2011

To find the area of a rectangle

Question 67 : Write a program in Java to find the area and perimeter of a rectangle. Length and breadth to be taken as input from the user.

Java Program :

public class rectangle
{
 public static void main(int l,int b)
 {
     int a=0,p=0;
     a=l*b;
     p=2*(l+b);
     System.out.println("Area of the rectangle = "+a+" units");
     System.out.println("Perimeter of the rectangle = "+p+" units");
    }
}

Friday 4 November 2011

To print all the factors of a number

Question 67 : Write a program in Java to print all the factors of a number.

Java Program :

import java.io.*;
class factors
{
 static void main()throws IOException
 {
     BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
     int a,i;
     System.out.print("Enter the number : ");
     a=Integer.parseInt(buf.readLine());
     System.out.print("\n");
     System.out.print("The factors are : ");
     for(i=1;i<=a/2;i++)
     {
         if(a%i==0)
         System.out.print(i+",");
        }
        System.out.print(a);
    }
}

To find the solution of a quadratic equation

Question 67 : Write a program in Java to find the solution of a quadratic equation by taking the inputs of a,b,c from the user.

Java Program :

import java.io.*;
class quadratic
{
 static void main()throws IOException
 {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     int a,b,c;
     double d,e;
     System.out.println("The format of a quadratic equation is given as follows : ");
     System.out.println("\nax2 + bx + c = 0");
     System.out.println("\nEnter values for a,b,c as follows : ");
     System.out.print("\na = ");
     a=Integer.parseInt(br.readLine());
     System.out.print("\nb = ");
     b=Integer.parseInt(br.readLine());
     System.out.print("\nc = ");
     c=Integer.parseInt(br.readLine());
     d=((-1*b)+java.lang.Math.sqrt((b*b)-(4*a*c)))/(2*a);
     e=((-1*b)-java.lang.Math.sqrt((b*b)-(4*a*c)))/(2*a);
     System.out.print("\nEquation : "+a+"x2");
     if(b<0)
     System.out.print(b+"x");
     else
     System.out.print("+"+b+"x");
     if(c<0)
     System.out.print(c);
     else
     System.out.print("+"+c);
     System.out.print(" = 0");
     System.out.println("\n\nSolution : x = ( "+d+" , "+e+" )");
    }
}
   

Wednesday 2 November 2011

Utilities 20 : Old style calculator

This is a kind of an old style calculator. You have to enter a number then enter the sign then the number and so on. This isn't one of my best programs, but it doesn't pay to hide it.

Java Program :

import java.io.*;
class calculator_working_edition
{
 static void main()throws IOException
 {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     double a,b=0;
     String q;
     char o='a',opt='b';
     System.out.println("\t\t*****Calculator*****\n");
     System.out.print("Enter the number : ");
     a=Double.parseDouble(br.readLine());
     System.out.print("\n");
     loop1:while(opt!='Y')
     {
         System.out.print("Enter option : ");
         q=br.readLine();
         o=q.charAt(0);
         if(o=='=')
         break loop1;
         if(o!='!')
         {
            System.out.print("\nEnter the number : ");
            b=Double.parseDouble(br.readLine());
        }
         switch(o)
         {
             case '+':
             a=a+b;
             break;
             case '-':
             a=a-b;
             break;
             case '/':
             a=a/b;
             break;
             case '*':
             a=a*b;
             break;
             case '!':
             a=Math.sqrt(a);
             break;
             default:
             System.out.println("\nInvalid input");
            }
         System.out.print("\n");
        }
        System.out.println("\nThe result is : "+a+"\n");
        System.out.println("Thank you for using this short program");
    }
}
           
I will be working on a new calculator this time. So keep on a lookout for that one.

Tuesday 1 November 2011

Utilities 19 : How to print in style with Java

Here's a nice program to print anything you want in style. Help yourself to the surprise.

Java Program :

import java.io.*;
import java.util.Random;
class visual
{
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   static String s="",s1,a1="";
    static Random r=new Random();
 static void main()throws IOException
 {
     matrix("Welcome to the matrix");
     sleep(1000);
     input();
    }
    static void input()throws IOException
    {
        matrix("Enter any string : ");
        s=br.readLine();
        matrix(s);
    }
    static void matrix(String a)
    {
        int c,i,c1;
        for(c1=0;c1<a.length();c1++)
        {
            for(i=0;i<3;i++)
            {
            c=r.nextInt(123);
            if((c>=48 && c<=57) || (c>=65 && c<=90) || (c>=97 && c<=122))
            {
                System.out.print("\f"+a1+(char)c);
                sleep(70);
            }
            else{
                i=i-1;
               continue;
            }
        }
            a1=a1+a.charAt(c1);
            System.out.print("\f"+a1);
        }
        a1=a1+"\n\n";
 }
 static void sleep(int c)
 {
     try{Thread.sleep(c);}
     catch(Exception e){}
    }
}

Also, I am working on a new school project, to make a Telephone Billing System, and would be posting it soon after my project is over.

Saturday 29 October 2011

To print all the keyboard characters using ASCII code

Question 66 : Write a program in Java to all the keyboard characters using ASCII codes.

Java Program :

class all_ascii
{
 static void print()
 {
     for(int i=0;i<256;i++)
     System.out.print((char)i+" - "+i+"\n");
    }
}

To delete a value from an array

Question 65 : Write a program in Java to delete ONE value from an array.

Java Program :

class del
{
 int[] a;
 int n,p=-1;
 del(int[] arr,int num)
 {
     a=arr;
     n=num;
     search();
     del();
    }
    void search()
    {
        for(int i=0;i<a.length;i++)
        {
            if(a[i]==n)
            p=i;
        }
    }
    void del()
    {
        int b;
        if(p!=-1)
        {
            b=a[a.length-1];
            a[a.length-1]=a[p];
            a[p]=b;
            System.out.print("Array : { ");
            for(int i=0;i<a.length-1;i++)
            System.out.print(a[i]+" ");
            System.out.print("}");
        }
        else
        System.out.print("Error 404");
    }
}

Thursday 27 October 2011

To find the frequency of a word in a string

Question 64 : Write a program in Java to find the fifth highest number among a set of numbers taken as input from the user.

Java Program :

import java.io.*;
class high5
{
static int[] a  ;
  static void check()throws IOException
  {
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      int i,k,c,s;
      System.out.print("\fEnter number of inputs : ");
      c=Integer.parseInt(br.readLine());
      if(c<5)
      check();
      a=new int[c];
      for(i=0;i<c;i++)
      {
          System.out.print("\nEnter a number : ");
          a[i]=Integer.parseInt(br.readLine());
    }
    for(i=0;i<c-2;i++)
    {
        for(k=0;k<c-2;k++)
        {
            if(a[k+1]>a[k])
            {
               s=a[k];
               a[k]=a[k+1];
               a[k+1]=s;
            }
        }
    }
    display();
}
static void display()
{
System.out.print("\n5th highest element is : "+a[4]);
}
}

Wednesday 26 October 2011

Happy Diwali


Tuesday 25 October 2011

Constructor Overloading

Demonstration of constructor overloading.

Java Program :

class const_overload
{
    int a,b,c;
    const_overload()
        {
            a=12;
            b=60;
            c=a+b;
        }
    const_overload(int p,int q)
        {
            a=p;
            b=q;
            c=a+b;
        }
    void display()
        {
            System.out.print("Value : "+c);
        }
}

Monday 24 October 2011

Diwali Special

Sorry for being away all this time. This was all due to a faulty internet connnection. Meanwhile, when I was offline for these few days, I was busy developing small but efficient and enjoyable Java programs. Now on the occasion of the hindu festival called Diwali, I am providing all of them in a .zip file for you all. They are as follows :

1: Bank : A Java ATM which is very much error-free and has loads of options.
2: Cups : A game in which you have to find out the position of the ball hidden inside the cups as the cups move.
3: Game : A game in which you have to guess three numbers within a range and if you are lucky and the numbers match with those chosen by the computer, you get awarded with points. Has some difficulty levels.
4: KBC  : A game similar to the TV show Kaun Banega Crorepati hosted by Amitabh Bachchan. Almost similar to the 2011 edition.
5: Tic-Tac-Toe : A game where you play Tic-Tac-Toe with the computer. Keeps you involved in it.

Try them out, show them to your parents, or simply brag about that you have made them all. You'll find the README inside the .zip package. Unzip it and enjoy.

ICSE Java Download

Friday 21 October 2011

To find absolute value of an integer

Question 63 : Write a program in Java to find the absolute value of an integer.

Java Program :

class absolute
{
    static void cal(int a)
    {
        int b=a>0?a:-a;
        System.out.print("The absolute value of '"+a+"' : "+b);
    }
}

Thursday 20 October 2011

Utilities 18 : Time display

Here is again a free utility to the show current time.

Java Program :

import java.util.*;
class time_display
{
     static void time()
    {
        Calendar c=new GregorianCalendar();
        String apm;
        if(c.get(Calendar.AM_PM)==0)
        apm = "AM";
        else
        apm = "PM";
        System.out.print("\fDate : "+c.get(Calendar.DAY_OF_MONTH)+" / "+c.get(Calendar.MONTH)+" / "+c.get(Calendar.YEAR));
        System.out.print("\n\nTime : "+c.get(Calendar.HOUR)+" : "+c.get(Calendar.MINUTE)+" : "+c.get(Calendar.SECOND)+" "+apm);
        try{Thread.sleep(1000);}
        catch(Exception e){}
        time();
    }
}

Utilities 17 : Reflection

This will help you to find the reflected point in x-axis, y-axis, or origin.

Java Program :

import java.io.*;
class reflection
{
 static int x,y;
 static char m;
 static void main()throws IOException
 {
     input();
     cal();
    }
    static void input()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("A = (x,y)");
        System.out.print("\n\nx = ");
        x=Integer.parseInt(br.readLine());
        System.out.print("\ny = ");
        y=Integer.parseInt(br.readLine());
        System.out.print("\nReflection axis (x,y,o) : ");
        m=(char)br.read();
    }
    static void cal()
    {
        int c=0;
        if(m=='y' || m=='Y')
        x*=-1;
        else if(m=='x' || m=='X')
        y*=-1;
        else if(m=='o' || m=='O')
        {
            x*=-1;
            y*=-1;
        }
        else
        {
            System.out.print("\nInvalid input\n");
            c++;
        }
        if(c==0)
        display();
    }
    static void display()
    {
        System.out.print("\nThe reflected point A' = ("+x+","+y+")");
    }
}

ShareThis