Pageviews

Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Monday, 3 February 2014

UPDATE,DELETE Operation in PostgreSQL Database

by Unknown  |  in Java at  Monday, February 03, 2014
As discussed in previous post you had learnt about two basic operations on database i.e. INSERT,SELECT ,in this post I am going to discuss the another two basic operations of PostgreSQL database i.e. UPDATE and DELETE.Lets get started.

UPDATE operation in PostgreSQL Database:

In this operation,we will make use of UPDATE keyword as used in simple SQL language just like below:

             String sql;
             sql = "UPDATE TABLE_NAME set column_name = value  where id = value;";
             st.executeUpdate(sql);

The example for this can be:

              String sql;
             sql = "UPDATE NEW_GARAGE set age = 25 where id = 4;";
             st.executeUpdate(sql); 


Complete code snippet for Update Operation:


package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Update {
    public static void main(String args[])
    {
    Connection c = null;
    Statement st = null;
     {
        try {
            Class.forName("org.postgresql.Driver");
             c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/kanwar","postgres","osm");
             c.setAutoCommit(false);
              st = c.createStatement();
             
             String sql;
             sql = "UPDATE NEW_GARAGE set age = 25 where id = 4;";
             st.executeUpdate(sql);
             c.commit();
         ResultSet rs = st.executeQuery( "SELECT * FROM NEW_GARAGE;" );
         while ( rs.next() )
         {
             int id = rs.getInt("id");
             String name = rs.getString("name");
              int age = rs.getInt("age");
      System.out.println( "ID = " + id );
      System.out.println( "NAME = " + name );
      System.out.println( "AGE = " + age );
      System.out.println();
         }
         rs.close();
         st.close();
         c.close();
        }
   
catch ( Exception e )
{
    System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
}
          System.out.println("Operation done successfully");  
 }
}
} 
The output for this program goes here:

Update operation in Postgresql database


DELETE Operation in  PostgreSQL Database:

Delete operation in PostgreSQL database is executed the same query as in SQL language.We use DELETE keyword and the data which we want to delete.The syntax for Delete operation is as follow:

String sql = "DELETE from Table_Name where column_name=value;";
The example of this syntax ca be:

String sql = "DELETE from NEW_GARAGE where ID=2;"; 

Complete code snippet for DELETE operation is as below:


package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Delete {
    public static void main(String args[])
    {
      Connection c =null;
      Statement st = null;
        try
        {
           Class.forName("org.postgresql.Driver");
           c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/kanwar","postgres","osm");  
       
       c.setAutoCommit(false);
       System.out.println("Opened Database Successfully");
        st = c.createStatement();
        String sql = "DELETE from NEW_GARAGE where ID=2;";
        st.executeUpdate(sql);
        c.commit();
        ResultSet rs = st.executeQuery("SELECT * FROM NEW_GARAGE");
       
        while(rs.next())
        {
        int id = rs.getInt("id");
        String name = rs.getString("name");
        int age = rs.getInt("age");
       
        System.out.println( "ID = " + id );  
          System.out.println( "NAME = " + name );
          System.out.println( "AGE = " + age );    
      System.out.println();
      }
      rs.close();
      st.close();
      c.close();
      }
        catch(Exception e)
        {
        System.err.println(e.getClass().getName()+":"+ e.getMessage());
        System.exit(0);
       
        }
        System.out.println("Operation performed Successfully");
        }
        }
     
The output for this program goes here:

Delete operation in Postgresql database



Happy learning.Stay tuned.





Friday, 31 January 2014

SELECT,INSERT Operation in PostgreSql Database

by Unknown  |  in Java at  Friday, January 31, 2014
PostgreSql as discussed in earlier posts is an advanced database.Now in this post I am going to discuss the two basic database operations in Postgresql i.e. Insert,Select.The remaining two operations i.e. Update,Delete will be discussed in the next post.

1. Insert operation in Postgresql database

As the name suggests,this operation is basically used for Inserting values into your database,below is the command used to Insert data into database using Insert statement.

"INSERT INTO TABLE_NAME(COLUMN1,COLUMN2,COLUMN3)"+ "VALUES(1,'TROY',35);";
Here is the complete code snippet for the Insert operation:

package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Insert_operation {
    public static void main(String args[])
    {
    try
    {
       Statement st = null;
       Connection c = null;
       Class.forName("org.postgresql.Driver");
       c=DriverManager.getConnection("jdbc:postgresql://localhost:5432/kanwar","postgres", "osm");

c.setAutoCommit(false);  
 
    st = c.createStatement();
    String sql;
    sql = "INSERT INTO NEW_GARAGE(ID,NAME,AGE)"+"VALUES(1,'Tarsem',35);";
        st.executeUpdate(sql);
 
        sql = "INSERT INTO NEW_GARAGE(ID,NAME,AGE)"+ "VALUES(2,'Gurpreet',36);";
        st.executeUpdate(sql);
 
   sql = "INSERT INTO NEW_GARAGE(ID,NAME,AGE)"+ "VALUES(3,'Jaspreet',37);";
        st.executeUpdate(sql);
     
          sql = "INSERT INTO NEW_GARAGE(ID,NAME,AGE)"+ "VALUES(4,'Maninder',39);";
        st.executeUpdate(sql);
     
          sql = "INSERT INTO NEW_GARAGE(ID,NAME,AGE)"+ "VALUES(5,'Ranjeet',38);";
        st.executeUpdate(sql);
     
     
     
     
    st.close();
    c.commit();
    c.close();
    }
    catch(Exception e)
            {
                System.err.println( e.getClass().getName()+": "+ e.getMessage() );
                System.exit(0);
            }
 
    System.out.println("Records created successfully");
     
 
    }
}
 This way your records are successfully saved into the database.

Insert ,Select operation in postgresql database


2.Select Operation in Postgresql database:

Since the insert operation helps us to insert the records into database,and the select statement help us to select the respective records specified in the query.

You can simply select a record using Select statement just like below:

"SELECT * FROM TABLE_NAME;"

 Here is the complete code snippet for illustrating the use of Select operation


package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Select_operation {
    public static void main(String args[])
    {
     Connection c = null;
     Statement st = null;
 
      try
      {
       Class.forName("org.postgresql.Driver");
         c= DriverManager.getConnection("jdbc:postgresql://localhost:5432/kanwar" ,"postgres", "osm");
         c.setAutoCommit(false);
        st = c.createStatement();
        ResultSet rs = st.executeQuery( "SELECT * FROM NEW_GARAGE;" );
   
      while ( rs.next() )
      {
        int id = rs.getInt("id");
          String name = rs.getString("name");
          int age = rs.getInt("age");
     
           System.out.println( "ID = " + id );
          System.out.println( "NAME = " + name );
          System.out.println( "AGE = " + age );  
      System.out.println();
      }
      rs.close();
      st.close();
      c.close();
      }
     
   
   
      catch(Exception e)
        {
        System.err.println(e.getClass().getName()+": "+ e.getMessage());
        System.exit(0);
        }
        System.out.println("Operation performed successfully");
     
     
    }
}
Here is the output of the Select query:

Select operation in postgresql database

Happy learning.Stay tuned.


Wednesday, 22 January 2014

Introduction to JDBC(Java Database Connectivity)

by Unknown  |  in Java at  Wednesday, January 22, 2014
What is JDBC?

JDBC stands for Java Database Connectivity, which is a standard Java API for database-independent connectivity between the Java programming language and a wide range of databases.

The JDBC library includes APIs for each of the tasks commonly associated with database usage:


  •  Making a connection to a database
  •  Creating SQL or MySQL statements
  •  Executing that SQL or MySQL queries in the database
  •  Viewing & Modifying the resulting records





JDBC Environment setup

Before connecting your code to the database,make sure that you have done following setup:

1. Core JAVA Installation
2. Any Database Installation

Apart from the above you need to setup a database which you would use for your project.

Creating JDBC Application:

JDBC application creation can be broadly divided into some easy and small steps.

Step 1: Importing required packages

import java.sql.*;

Step 2: Register JDBC driver

It basically initializes a driver so that you can open a communication channel with the database.Following is the code snippet to achieve this:

Class.forName("org.postgresql.Driver"); //I am using Postgresql database.


Step 3: Open a Connection

This step requires a method i.e. DriverManager.getConnection() method to be create a Connection object which represents a physical connection with the database.

con = DriverManager.getConnection(DB_URL,USER,PASS);

Step 4 : Executing a query

This step will help you to build a statement that will build and submit as a SQL statement to the database.

stmt = conn.createStatement();
String sql;
sql = "SELECT id, first, last, age FROM Students";
ResultSet rs = stmt.executeQuery(sql);

Step 5: Extracting data from result set

This step will help you in fetching data from database.You can use the appropriate Result.getXYZ()


while(rs.next())
{ //Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
//Displaying values
System.out.println("ID:"+ id);
System.out.println(",Age :"+ age);

Step 6: Closing the environment

Its a good habit to close all database resources before program exits.

rs.close();
stmt.close();
con.close();



This post was just a simple introduction about JDBC,in the next post we will discuss about the new and powerful database i.e. PostgreSQL.

Saturday, 18 January 2014

How to check JDK version which is installed on your computer

by Unknown  |  in Java at  Saturday, January 18, 2014
Here’s a quick guide to show the use of “java -version” command to check the current JDK version that is installed on your computer.
1. Open your Command prompt in Windows or terminal in *nix.
2. Type “java -version“.

In this case,JDK "1.7.0_04" is installed.

Sunday, 7 July 2013

Event handling in Java

by Unknown  |  in Java at  Sunday, July 07, 2013
Event handling is the heart of successful applet programming.Applets are event driven programs.Event is an object that describes a change of state.This change of state is generated by the user interacting with the Graphical User Interface(GUI) by pressing a button,entering a character via keyboard,dragging the mouse,selecting an item in Combo box,selecting an item in a list. Mostly,the events to which our applet will respond are generated by the user. These events are passed to our applets in a variety of ways. There are several types of events. The most commonly handled events are generated by the following:


  • Mouse
  • Keyboard 


and various controls, such as a push button,submit buttons or login buttons.Event handling in Java is supported by the java.awt.event package.

Event handling
Table 1:   Main Event classes in java.awt.event package


Event Listener Interfaces:

Event Listener receives notifications of events.Listeners are created by implementing one or more interfaces.Whenever an event occurs,the event source invokes the appropriate method defined by the listener and provides an event object as its argument.The commonly used listener interfaces are:

Event handling

Table 2: Interface classes for Event handling
Source code for creating a simple Form in Java

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

class Form1 extends Frame
{
int i;
Label lbl1,lbl2,lbl3,lbl4,lbl5,lbl6,lbl7,lbl8,lbl9;
TextField  txt1,txt2,txt3,txt4,txt5,txt6,txt7,txt8,txt9;
GridLayout gl1;

Form1()
{
gl1=new GridLayout(12,2);
setLayout(gl1);
lbl1=new Label("First Name");
txt1=new TextField(8);
lbl2=new Label("Second Name");
txt2=new TextField(8);
lbl3=new Label("Last Name");
txt3=new TextField(8);
lbl4=new Label("Father's Name");
txt4=new TextField(8);
lbl5=new Label("Father's Occupation");
txt5=new TextField(8);
lbl6=new Label("Mother's Name");
txt6=new TextField(8);
lbl7=new Label("Mother's Occupation");
txt7=new TextField(8);
lbl8=new Label("LL.No");
txt8=new TextField(8);
lbl9=new Label("Mobile.No");
txt9=new TextField(8);

add(lbl1);
add(txt1);

add(lbl2);
add(txt2);

add(lbl3);
add(txt3);

add(lbl4);
add(txt4);

add(lbl5);
add(txt5);

add(lbl6);
add(txt6);

add(lbl7);
add(txt7);

add(lbl8);
add(txt8);

add(lbl9);
add(txt9);

}
public static void main(String arg[])
{
try{
Form1 obj=new Form1();
obj.setSize(500,400);
obj.setTitle("Personal details");
obj.setVisible(true);
}
catch(NullPointerException ae)
{
}
}
}

The output of the program is as below:

Event handling




We had used Text Editor to write our code,after this we will move onto IDE(Integrated Development Environment).We had used Text editors till now just to perfect our coding part,because in IDE most part of the code is written automatically by the IDE.













Playing audio in Java Applet

by Unknown  |  in Java at  Sunday, July 07, 2013
Applets provide GUI,which is preferred by all users.Java applet supports another good feature of playing music files within the applet.Here are the easy steps,you need to follow in order to run your music files in your Applets.

1.Firstly create a Applet,or you can use the applet code used in the previous post.Then you need to declare the audio you like to output from your Applet using keyword 'AudioClip'.The syntax will be like this;

AudioClip variable_name;

2.Now within the start(),you need to declare your AudioClip using its variable and define its path,where it is located.The syntax will be;

sound=getAudioClip(getCodeBase(),"AudioFile_Path.wav");       //sound-variable name

3.If the audio which you want to play lies within the folder,where you are writing the code,then you need not to specify the path,just simply write its name in " ".But,if you are importing your audio from another drive or folder,then you need to specify its path.

4.In the next step,we need to play our audio clip using play().

Source code for playing audio in Java Applet:

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

public class applet4 extends Applet
{
AudioClip soundFile1;
Image p;
public void init()
{
p=getImage(getDocumentBase(),"6.jpg");
}
public void start()
{
soundFile1 = getAudioClip (getCodeBase()," 123.wav ");
soundFile1.loop();
soundFile1.play();
}
public void paint(Graphics g)
{
g.drawImage(p,10,10,this);
g.drawString("Music Player",20,20);
}
}

Then you need to compile your java file like this:

Applet

The output will be like this:

Applet music



The work is all done,now you can play your audio files in your Applets.You will not be able to hear anything from this applet,since Java is not installed on our blog,but you will definately hear audio in your device.  The most important point to remember is that,you can only play .wav files in Applets,if you want to play another format,then you can convert it first,then you can play.

Display images in Java Applets

by Unknown  |  in Java at  Sunday, July 07, 2013
Displaying background images is the another powerful functionality provided by Java Applets.You can choose any of your favorite wallpapers as your background screen in Applets.So,here are some easy steps to display background images in Applets.


  • Firstly create your Applet.
  • In the declaration section,you need to declare a variable for your image,like;


Image variable_name;

In the init(),you need to initialize your image and define its path,where it is located in you system,using the following syntax;

p=getImage(getDocumentBase(),"Image_path.jpg");

If your image is located within the folder,where you are writing the codes,then you need not to specify the path of the image just need to mention its name,but if your image located in the another folder/drive,then you need to specify the path within the double quotes(" ").

Now we need to invoke our image in the paint(),where your image will be displayed using below syntax;

g.drawImage(p,10,10,this);                                        //g-object of Graphics class.

Here is the complete code for displaying a image in your Applet.

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

public class applet4 extends Applet
{
Image p;
public void init()
{
p=getImage(getDocumentBase(),"4.png");
}
public void paint(Graphics g)
{
g.drawImage(p,10,10,this);
}
}

Applet


The output of the applet is :





Now you are all done with the coding part,now you can display your defined images as background image in your Applet.

Tuesday, 25 June 2013

Graphics in Java Applets

by Unknown  |  in Java at  Tuesday, June 25, 2013
Graphics class in Applets helps us to draw different shapes in Applets,for drawing these shapes each shape has its own method.These methods are as follows:

Most Common Graphic class methods:


  • drawString(String Str,int x,int y): This method is used to draw the required String.
  • drawOval(int x,int y,int width,int height): It is used to draw Oval of the specified width and the height.
  • drawRect(int x,int y,int width,int height): It is used to draw Rectangle of the specified height and width.
  • drawLine(int x1,int y1,int x2,int y2): It is used to draw line between the points(x1,y1) and (x2,y2).
  • drawArc(int x,int y,int width,int height,int start angle,int arc angle ): It is used to draw an arc of the specified angle.
  • drawImage(Image img,int x,int y,Image observer): It is used to draw an Image of the specified parameters.
  • fillOval(int x,int y,int width,int height): It is used to fill the Oval with specified Color and the parameters specified.
  • fillRect(int x,int y,int width,int height): It is used to fill the Rectangle with specified Color and the parameters specified.
  • fillArc(int x,int y,int width,int height): It is used to fill the Arc with specified Color and the parameters specified.
  • setColor(Color c): This method is used to set the Color specified by the User.
  • setFont(Font font): This method is used to set the Font type and size according to the specified parameters.


Now,to implement these functions,we had created a simple program which will not use these functions fully,but will cover most of them.

WAP to implement the use of Graphics class in Java Applets:

import java.awt.*;
import java.applet.*;
public class Applet3 extends Applet
{
Thread t;                                                                                           //Thread is declared
AudioClip soundFile1;

public void start()
{
soundFile1 = getAudioClip (getCodeBase()," 123.wav ");                 //Audio file

soundFile1.loop();


soundFile1.play();

}
public void paint(Graphics g)
{
g.setFont(new Font("VERDANA",2,50));
g.drawString("Keep Smiling!!!",50,50);
g.setColor(Color.yellow);
g.fillOval(70,200,230,230);
g.setColor(Color.black);
try

{
g.drawOval(170,320,55,55);
for(int i=0;i<30;i+=2)
{
g.setColor(Color.black);
g.drawOval(110,250,30,i);
g.drawOval(210,250,30,i);
t.sleep(50);
g.setColor(Color.yellow);
}
g.setColor(Color.black);
g.drawOval(110,250,30,30);

g.drawOval(210,250,30,30);

g.drawArc(100,230,50,50,0,180);//main arc
g.drawArc(101,231,50,50,0,180);
g.drawArc(102,232,50,50,0,180);
g.drawArc(200,230,50,50,0,180);//main arc
g.drawArc(201,231,50,50,0,180);
g.drawArc(202,232,50,50,0,180);

g.fillOval(110,250,30,30);
g.fillOval(210,250,30,30);
}
catch(InterruptedException e)
{
}
}
}

This is the output of our program:



Here is the Applet design:





Note: We had also used Audio file in our Applet,using Audio in Applet will be discussed in later posts.You just need to see the Graphic methods used.




Sunday, 23 June 2013

Applets in Java

by Unknown  |  in Java at  Sunday, June 23, 2013
Applets: Applets are Window-based programs.They are a special type of Java programs that are designed for the transmission over the Internet,Applets can run on Web browsers as well as they can be used for developing Console based applications.From the Applets,we enter into the GUI(Graphical User Interface) environment.

Applets initialization and Termination methods:

1.init(): it is the first method to be called.In init(),our applet will initialize variables and perform other startup activities.

2.start():The Start() method is called after init().It is also called to restart an applet after it has been stopped,start can be called more than once during the life of an applet.

3.paint(): This method is called each time our applet's output must be redrawn

4.stop(); This method is used to suspend any child threads created by the applet and to perform any other activities required to put the applet in safe state.

5. destroy(); This method is called when the applet is no longer required.It is basically used to perform the Shutdown operations required of the applet.

Now we are going to make our first applet,to build your own applet you can try the below code,for executing Applet you must have a Java browser.

WAP in JAVA to print String in Applet

import java.applet.*;
import java.awt.*;
public class Applet1 extends Applet                             //Applet class is inherited
{
public void paint(Graphics g)
{
g.setFont(new Font("VERDANA",2,30));                        //Method to change Font size and type
g.drawString("Coding Garage!!!",150,150);                     //Method to draw String
}
}

As our applet run in browser so we need to provide a HTML file which will make it run in browser.So,here is the HTML code we need to write in our Text editor and save it in .html extension.

<html>
<body>
<applet code="Applet1.class" width="300", height="300">
</applet>
</body>
</html>

How to run Applets in Java?

1.Firstly we need to compile our code,after compiling Filename.class file will be created which we will use in .html file.
2.Secondly,we need to write the following command i.e on the cmd to run our Applet

appletviewer Filename.html

3.You can also directly open Filename.html to open your Applet.



Applets

Here is the output of our first Applet:

Applets

  

Thursday, 20 June 2013

Wrapper Classes in Java

by Unknown  |  in Wrapper class at  Thursday, June 20, 2013
Wrapper Classes:The primitive data types in Java such as Int,Char,Float etc has a class dedicated to it.Sometimes it is required to create an Object for these data types.The classes which provided this functionality in Java are known as 'Wrapper classes' in Java,because basically they 'wrap' the data type of the class to an Object.The wrapper classes are present in Java.lang package.

Advantages of Wrapper Classes in Java:


  • They provide mechanism to wrap primitive data types,so that primitives can do the activities reserved for the objects like being added to Array lists,Hash sets,Hash Maps etc.
  • They provide utilities functions for primitives like converting them to and from String objects,converting them to various bases like Binary,Hexadecimal and comparing various objects.
  • They are used basically for Type Casting.


Following statements illustrate the use of Wrapper classes:

int x=10;
Integer y=new Integer(x);

The first statement declares an int variable x,initialized with value 10,whereas the second statement creates an object for Integer y.The object initializes and reference to the object is assigned to the variable y.

In the above two statements,we had wrapped the data type Integer and now we are going to unwrap it,it is illustrated by following statements:

int k=y.intValue();
System.out.println(k*k); // Result is100



In the above codes,we had used Wrapping and Un-Wrapping of data types in Java.Each data type has a wrapper class associated to it,the picture shows the list

Wrappler class



The wrapper classes perform wrapping and un-wrapping of data types,here is the program of implementing wrapper classes in Java.

WAP to implement Wrapper classes in Java

class wrappler
{
public static void main(String args[])
{
byte grade=2;                                               //Byte data type
int marks=50;                                               //Integer data type
float pric=5.6f;                                             //Float data type
double rate=50.6;                                        //Double data type

Byte g1=new Byte(grade);                          //Object created for Byte data type
Integer m1=new Integer(marks);                // Object created for Integer data type
Float f1=new Float(pric);                            //Object created for Float data type
Double r1=new Double(rate);                      //Object created for Double data type

System.out.println("Values are");
System.out.println("Byte object g1:"+g1);
System.out.println("Integer object m1:"+m1);
System.out.println("Byte object f1:"+f1);
System.out.println("Double object r1:"+r1);

}
}

The output for the program is:

Wrapper class




Wrapper classes are not basically used in programs these days,because Type Casting is also supported by Java.But still,we can use Wrapper class according to our requirement.















Wednesday, 12 June 2013

How to create User defined packages in Java

by Unknown  |  in Java at  Wednesday, June 12, 2013
Packages can be defined as a method to put Classes,Methods,variables and Interfaces together in a same pack.This grouping is basically done on the basis of functionality.In simpler words,packages act as a container for classes.Packages are an important part of Java,each packages has its own functionality,packages provides following advantages:
  • Classes of a particular package of the other program can be easily reused.
  • Two different packages can have same class name.
  • Packages provides us ways to hide classes from accidental use.

Java supports two types of Packages:

  1. Java API(Application Programming Interface) Packages.
  2. User defined Packages.


1.Java API Packages:The API packages which are already defined in Java are called Java API packages,they are also known as built in packages.For Ex:Java.lang, Java.util, Java.io.

2.User defined packages: Java also supports a functionality that the users can define their own packages.Now the problem arises,that how we can create and run user defined packages in Java.In order to create new user defined package you need to follow  the below steps:

  • Firstly,you need to create a New folder,give it the name,you want to give to your package.
  • Then you need to create a new class outside that folder and then compile the package,remember that you need to compile only the package,you should not run it,because it does not contain any main method.
  • Now create new program whatever you want to create.
  • Then import the package you had created and then compile and run the program.

package Mine;                                                   //New package declared by user
public class packs
 {
public void display()
{
System.out.println("Welcome to World of Java");
}
}                                            

Now create a program outside the Package folder,the following examples shows how we had create a program outside the 'Mine' package folder and then we need to import our package using 'import' keyword,lets see how;

import Mine.*;                                                      //Package is imported
class dort{
public static void main(String args[])
{
packs d=new packs();                                              // Object of Package class is created      
d.display();                                                             //Method of our Package is accessed
}
}

The output goes here:

Packages



















Thursday, 6 June 2013

Execute Java code using Command line Arguments

by Unknown  |  in Java at  Thursday, June 06, 2013
Command Line arguments is one of the simple ways to execute Java code.In this method,we write out code in a simple Text editor say Notepad,only few things are different in Command line argument method as compared to codes written in IDEs.Command line is recommended for beginners only as it makes them to perfect that method and further they can move onto IDEs.Then the code written in Text editor is saved with .Java extension and is executed by Java compiler.To illustrate this we are providing a simple example of adding 2 nos.

WAP to add 2 numbers using Command line arguments


class add

{
public static void main(String args[])
{
int i,j,k;
i=Integer.parseInt(args[0]);
j=Integer.parseInt(args[1]);
k=i+j;
System.out.println("Sum is "+k);
}
}


The Output goes here:


Command line arguments



Tuesday, 4 June 2013

Java-Introduction and Features

by Unknown  |  in Java at  Tuesday, June 04, 2013

Java



Java is one of the most powerful programming language.Java is an Object Oriented Programming(OOP) language which works on Objects.Objects are real world entities which are closely related to reality and it helps us to program real world problems.Java's key features are:

1.Platform Independence:   Java is a platform independent language i.e. the execution of the program is not dependent on the type of OS environment provided,it makes it possible to process the program at any type of environment variables.

2.Security:  When we convert the code from one machine to the other,the code is first checked for viruses,if it is affected,the code will not be executed otherwise it will be executed.

3. Simple and Small:  It is simple language as it uses the features of other languages such as C and C++.Java removes complexity as it does not use Pointers,Storage classes,Multiple Inheritance.

4. Distributive:   Java is a highly distributive language i.e. the program can be designed to run on Computer Networks.Java provides a large collection of library of classes for communicating using various protocols such as HTTP,FTP etc.

5.Highly Portable:  The portability of the Java is basically provided by Platform Independence.Java uses different sizes of data types.We can easily port our code from one system to the other. 



Proudly Powered by Blogger.