PCPerformanceTools.com
Optimize the Windows Registry and Repair common Computer errors Automatically.

 
  
Article: How to Write a simple Java Program home
 

 Writing Java
Writing a Simple Java Program
Article Added December 10th, 2006

 

Java is a simple and secure language that has revolutionized the programming for the Web. It is portable, object oriented, distributed and architecture neutral.

These features make it the first choice for web programs, server side applications and cell phone applications.
This article explains how to write a simple object oriented program using the Java language syntax. We will create a class with its attributes and methods using Java

Let us take the class Product. It has the following attributes and behaviors.

Product
productCode

productName

productRate

ShowDetail()

We have to identify the attribute, provide variable names for them and a test value for each variable.
Attribute Variable name Test value
Code productCode 2001
Product name productName Lima Beans
Product rate productRate 23.00

Let us proceed systematically towards compiling the program. A Java program is written using a simple text editor.

• Declare the class.
The Java class is enclosed between the curly braces {}. All the methods and data members are defined inside a class. Therefore, we have

public class Product
{

}

• Defining variables
Syntax
<data type> <variable name>

Each attribute is defined as data member as per the syntax above. The semicolon “;” is used to terminate a statement. Use indention while coding to make it more readable.
Now we will define variables of the class.

public class Product
{
Int productCode;
String productName;
Float productRate;
}

• Declaring the methods of the class

Syntax
<access_detail> <type> <Method_name> ([argument_list])
{
……
……
}

<access_detail> (public or private) This specified where the method could be accessed. Public means that the method can be accessed from another class. If it is private, the method can be accessed only from the current class.
<type > It is the data type of the value returned by the method. If no value is returned void is used.
<argument_list> It is the set of variables that are passed to a method.

Applying this to our example we have:-

public class Product
{

Int productCode;
String productName;
Float productRate;

public void ShowDetails( )
{
// here the code for displaying product details will come.
}
}

• Variable initialization.

We have to supply a value to the variables defined. This will be done by using a constructor. A constructor is a special method used to initialize data members of a class. It has the same name as the class and is executed when an object of the class is created. It does not have a return type.
We will initialize the data members of the product class with the test values.

public class Product
{
Int productCode;
String productName;
Float productRate;

public Product ( )
{
productCode = 2001 ;
productName=”Lima Beans” ;
productRate=23.00 ;
}

public void ShowDetails( )
{
// here the code for displaying product details will come.
}
}

• Display the test values

The display device and the keyboard are the resources of the computer. Java has a class called System that has all the methods required to work with these resources. System, one of the most useful classes provided by Java, has a standard interface to common system resources.
The out Object , encapsulated inside the System class, represents the standard output device. It contains the println() Method. The println() method is used to display data on the screen.
We will use these to write code to display the test values.

public class Product
{
Int productCode;
String productName;
Float productRate;

public Product ( )
{
productCode = 2001 ;
productName=”Lima Beans” ;
productRate=23.00 ;
}

public void ShowDetails( )
{
System.out.println (“The Product Code is “+productCode);
System.out.println (“The Product Name is “+productName);
System.out.println (“The Product Rate is $“+productRate);

}
}

• Code the main ( ) method

In Java, application the main () method should be the first to be executed.

Syntax
public static void main(String args[])
{

}

It should exist in a class that has been declared as public. The name of the file in which the code has been written should be the same as the name of the class that has the main method.
In it, we will invoke the ShowDetails ( ) method after creating a class object.
Now we will code the main method.

public class Product
{

Int productCode;
String productName;
Float productRate;

public Product ( )
{
productCode = 2001 ;
productName=”Lima Beans” ;
productRate=23.00 ;
}

public void ShowDetails( )
{
System.out.println (“The Product Code is “+productCode);
System.out.println (“The Product Name is “+productName);
System.out.println (“The Product Rate is $“+productRate);

}
public static void main (String args[])
{
Product productObject= new Product ( );
productObject.ShowDetails ( );
}
}

Now we have the program ready. It has to be saved with extension java. We name this file Product.java.
Now we have to compile the program to convert it into bytecode. It creates a file with the same name but a class extension. This .class file runs on a Java Virtual Machine, which interprets and runs the program on any operating system. This makes Java programs platform independent.
To compile a Java program the syntax is:-
javac Product.java

A Product.class file will be created. When the code is compiled and is error free, it can be executed using the command

java Product

The output will be

The Product Code is 2001
The Product Name is Lima Beans
The Product Rate is $23.00

This was a simple Java program.

 

 
All Rights Reserved Copyright 2005, 2006  PCPerformanceTools.com  How to Write Java Programs