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

Friday, 29 July 2011

How to run Java program


A First Simple Program
/*
This is a simple program Call this file “Example.java”.
*/
Class Example
{
//Your program begins with a call to main ().
Public static void main (String args[])
{
System.out.println (“This is a simple Java program “);
}
}


Entering the program
-The name of a source file is very important
-In Java, all code must reside inside a class. By convention ,the name of that class should match the name of the file that holds the program
-A source file use the .java filename extension

Compilling the Program
C:>\javac Example .java
-The javac compiler creates a file called Example .class, that contain the Bytecode version of the program
-To actually run program, you must use the Java interpreter, called java
C:>\java Example
-When java source code is compiled, each individual class is put into its own output file named after the class and using the .class extension
A closer look at the first program
-All Java application begin execution by calling main()
-A complex program will have dozens of classes, only one of which will need to have a main() method to get things started
-Java applets don’t have any main() method
-Java interpreter run only main() method
-Java interpreter run only the class that have a main method
-println() is a built in method
-System is a predefined class that provides access to the system,and out is the output stream that is connected to the console.

Features of java programming language


Compiled and interpreted
Platform – Independent
Object – Oriented
Robust and Secure
Distributed
Multithreaded
Dynamic and Extensible

Compiled and interpreted:-
                Usually a computer language is either compiled or interpreted. Java combines both these approaches thus making Java a two stage system.
First, Java compiler translates sources code into what is known as byte code instruction. Byte codes are not machine instructions and therefore, in the second stage. Java interpreter generates machine code that can be directly executed by the machine that is running the Java program.

Object- Oriented:
               Java is a true object – oriented language. Almost everything in Java is an object. All program code and data reside within object and classes.

Robust and Secure:
               Java is a robust language. It provides many safeguards to ensure reliable code. It has strict compile time and run time checking for data types. It is designed as a garbage – collected language relieving the programmers virtually all memory management problems. Java also incorporates the concept of execution handling which captures series errors and eliminates any risk of crashing the system.
Java system not only verifies all memory access but also ensure that no viruses are communicated with an applet. The absence of pointer in Java ensures that programs cannot gain access to memory locations without proper authorization.

Distributed:
               Java is designed as a distributed language for creating application on network. It has the ability to share both data and programs.

Multithreaded:
               Multithreaded means handling multiple tasks simultaneously. Java supports multithreaded programs. This means that we need to wait for the application to finish one task before beginning another. For example, we can listen to an audio clip while scrolling a page and at the same time down load and applet from a distant computer. This feature greatly improves the interactive performance of graphical applications.

Platform- Independent:
               The most significant contribution of Java over other languages is its portability. Java programs can be easily moved from one computer system to another anywhere and anytime. Changes and upgrades in operating systems, processors and system resources will not force any changes in Java programs. This is the reason why Java has become a popular language for programming on Internet which interconnects different kinds of systems worldwide. We can download a Java applet from a remote computer onto our local system via Internet and execute it locally. This makes the Internet an extension of the user’s basic system providing practically ultimate number of accessible applets and applications.

High Performance:         
               Java performance is impressive for an interpreted language, mainly due to the use of intermediate byte code. According to sun, Java speed is comparable to the native C/C++. Java architecture is also designed to reduce overheads during runtime. Further, the incorporation of multithreaded enhances the overall execution speed of Java programs.

Dynamic and Extensible:
Java is dynamic language; Java is capable of dynamically linking in new class libraries, method and object. Java can also determine the type of class through a query making it possible to either dynamically link or abort the program, depending on the response.
               Java programs support functions written in other language such as C and C++. These functions are known as native methods. This facility enables the programs to use the efficient functions available in these languages. Native methods are linked dynamically at runtime.
              



Thursday, 28 July 2011

Program to enter a 5*5 matrix and display it


class matrix
{
Public static void main (String arg [])
{
int two [] [] = new int [5] [5];
{
int i,j,k =0;
for (i= 0; i<5 ; i++) {
for (j=0 ; j<5; j++)
{
Two [i] [j] = k;
k++;
} }
for (i=0; k=5 ;i++)
{
for (j=0; j<5 ; j++)
{
System.out.print( two [i] [i] + “  “ );
}
System.out.println( );
}
}
}

Twitter Delicious Facebook Digg Stumbleupon Favorites More