Thursday, November 25, 2010

This is a simple applet program

//importing the packages
import java.applet.*;
import java.awt.*;

//this will include the class file for running in the browser
/*<applet code="FirstApplet.java" width=100 height=100 ><
/applet >*/

//main function begins here
public class FirstApplet extends Applet{

public void paint(Graphics g){
//it will prints the string in the applet viewer
g.drawString("Welcome in Java Applet.",40,20);
}
}


Save the above program as FirstApplet.java

compile the program by using javac FirstApplet.java

execute the program by

appletviewer FirstApplet.java or appletviewer FirstApplet.html

the out put of the above program will be




Wednesday, October 27, 2010

Method in java

A java method is a series of statements that perform some repeated task. Instead of writing ten lines of code we can put those ten lines in a method and just call it in one line. It is like a shortcut.

Java methods are similar to functions or procedures in other programming languages.

on the other hand, a Java method is a set of Java statements which can be included inside a Java class.

The syntax of the java method is as follows

access-specifier return-type(data-type) method-name(arguments)
{
statement1;
statement2;//body of the function or method
statement3;
}

access-specifier will be public, private or protected

return-type is the one the data type. such as int, float,etc...

method-name is any user defined name.

arguments are the value we need to pass to the function

The example program
/**
*
* @author prabhu
*/
public class addition {
//variable declaration
int a,b,c;

public void add()
{
a=10;
b=20;
c=a+b;
System.out.println("a+b="+c);
}

public static void main(String ar[])
{
//creating object
addition add=new addition();

//calling the method in the class using the object
add.add();
}


}

simple java program

This is a simple java program

/*

This is a simple java program
Save this file as simple.java

*/


public class simple
{
//the program begins here
public static void main(String ar[])
{
System.out.println("welcome to java program");
}
}

the output of the above program is

welcome to java program