Monday, 1 August 2011

The if decision structure


The if statement evaluates a condition and if the result is true then it runs the line of code that follows it. Here is an example of how to test if the value of a variable called I is equal to 5 :
public class Decisions
{
               public static void main( String [ ] args )

               {
               int i = 5 ;
               if ( i == 5)
                 System.out.println( “ I is equal to 5 “ );
               }
}
If you change i to 4 and run this program again you will see that no message is printed.
else
               we know that if the condition is true then the code directly afer the if statement is run. You can run code for when the condition is false by using the else statement

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

               {

               int i = 4 ;
               if ( i = = 5 )

               System.out.println ( “ i is equal to 5 “);

               else

               System.out.println( “i is not equal to 5 “ );
}
}

How to use Command-Line Arguments


Using Command-Line Arguments
Helps us to pass information into a program when it is run
class CommandLine
 {
public static void main (String args [ ] )
{
for (int i= 0; I < args.length ; i ++ )
System.out.println(“ args [ “ + i + “ ] ; “ + args [ i ]) ;
}
}

To execute a program, we write from the command prompt:

java CommandLine this is a test 100-1

Simple program to arrange the citynames in ascending order


Simple program to arrange the citynames in ascending order

class stasc
{
                    public static void main (String args [ ])
                    {
                                        int I, j;
                                        String st;
                                        for (i=0 ; I <args.length ; i++ )
                                        {
                                                            for(j=0 ; j < args.length-1; j ++)
                                                            {
                                                            if (args [ j].compareTo( args {j + 1] )>0)
                                                            {
                                                            st=args[ j ] ;
                                                            args [ j] =args [ j + 1]
                                                            args [ j + 1 ];
                                                            args [ j + 1 ] =st ;
                                                            }
                                                            }
                                        }
                    for(i=0; i< args.length; i++)
                    {
                                        System.out.println(args [ i] );
                    }
}
}
Output :
                    Java stasc xyz abc hello
                    abc
                    hello
                    xyz

arrange the numbers in descending order


class comm.
{
                    public static void main (String args [] )
                    {
                    int I,j,k ;
                    k= args.length;
                    String temp;
                    for(i=0; i<k-1 ; i++ )
                    {
                                        for (j=0;j<k;j++)
                                        {
                                        if(Integer.parseInt(args[ j ])<Integer.parseInt ( args [ j + 1 ]))
                                        {
                                        temp = args [j ];
                                        args [j] =args [ j+ 1];
                                        args[j + 1]= temp;
                                        }
                                        }
                    }
for(i=0 ;I <args.length; i ++)
{
                    System.out.println(args [ I ]);
}
}
}
Output:
                    java comm. 12 2 3 1 46
                    1

                    2
                    3
                    12
                    46

Twitter Delicious Facebook Digg Stumbleupon Favorites More