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");
}
}

ShareThis