Tuesday, 9 April 2013

Write a java program to display two strings bouncing in opposite directions.


/*
<applet code="BouncingWords" width="200" height="200">
</applet>*/ 

import java.io.*;
import java.applet.*;
import java.awt.*;


public class BouncingWords extends Applet
{         
                        static int i=0,j=1200,flag=0;
                        Thread th=new Thread();  //Defining Thread.
           
                        public void paint(Graphics g)
                        {                     

                                    if(flag==0)
                                    {
                                                if(i!=j)
                                                {
                                                             i++;
                                                             j--;
                                                 }
                                                else
                                                             flag=1;
                                    }
                                    else
                                    {
                                                if(i!=0&&j!=1200)
                                                {
                                                             i--;
                                                             j++;
                                                }
                                                else
                                                            flag=0;
                                    }
                                    g.drawString("Nothing is ",i,200); //Calling drawstring function
                                    g.drawString("Impossible",j,350);
                                    try
                                    {                                 
                                                th.sleep(5);
                                    }
                       
                                    catch (Exception e)
                                    {
                                    }
                                    repaint();  //Calling repaint method.

                        }
}

Output:

C:\>javac BouncingWords.java

C:\>appletviewer BouncingWords.java

Write a java program which accepts a word from command line and print each character of word on one line.



class Ass1
{
                        public static void main(String args[])
                        {
int len=args[0].length();
for(int i=0;i<len;i++)
                                    {
                                                System.out.println(args[0].charAt(i));
                                    }
}
}
Output:
E:\test>javac ass1.java
E:\test>java Ass1
E:\test>java Ass1 java
j
a
v
a