Pages

Friday, December 10, 2010

Session 1 - JAVA Programming Guide Online

Today, I am going to show you how to program in JAVA. First of all, you will have to install JAVA SDK from:
http://www.oracle.com/technetwork/java/javase/downloads/index.html

Click on JDK. This will lead you to page where you will have to select your operating system. Now, for programming in JAVA, you do not need to worry about operating systems because JAVA is a neutral language so, a program made in JAVA can be run equally on any other operating system where JAVA Virtual Machine is installed. Windows, Linux (almost all distros), and Mac OS X, all have built in JAVA Virtual Machine and so a JAVA program, built on Windows can be run on Linux as well as on Mac OS X without reprogramming or reconfiguring it.
After selecting your operating system, JAVA SDK will start downloading. Finish the setup process and now, open Netbeans.org and click on 'Download' button. This will lead you to selecting the type of NetBeans suite. You may select JAVA or you may also select 'All'. Click on 'Download' beneath it.
Having assumed that you have now completed the setup process, I will now proceed towards teaching you to program JAVA.
Here's labelled NetBeans Screen.

For now, you only need to know this much. I will explain further as we proceed towards the course.
Now go to File>New Project. Follow the directions below.
After this, if nothing goes wrong, you will see the following window. Note the highlighted areas.
To familiarise you with icons, here's a short note of it:
Class File Icon


Package Icon


Project Folder Icon


Project Icon
 
Notice that since we created a new project BloggerJAVATutorials, NetBeans automatically created a new JAVA class file called BloggerJAVATutorial.java. I do not think we will need that and so, lets delete it.
For deleting, right click on the class file and select 'Delete'.
Now, you must safely delete the file in order to avoid conflicts. This is not important now because we do not have any other files except this. You will understand the importance of this when you will learn the auxillary classes. For now, checking 'Safe delete' is a good habit to have.
Notice the change in color of package icon. Now it is in grayscale. This indicates that now the package is empty. Rightclick on it and select New>JAVA Class...
Now, we are going to make a program called Silly. It just accepts data from user and suffixes "is a silly name" to it. By this, you will learn how to accept data from user. So, you will have to type 'Silly' in Class name. Click Finish to create the java class file.
Notice that the class name in:
public class Silly {

}
is same as the class file name. This has to be same or else the program will not run.
Type in the following code: This

import static javax.swing.JOptionPane.*;
public class Silly {
public static void main(String[] args){
String name = showInputDialog("Please type in a name");
System.out.println(name + " is a silly name.");
}
}

The first line is a reference to JAVA library. This tells JAVA which library to use. In this, JOptionPane is used which is used to display input and output dialog boxes, one of which is used here. We have used showInputDialog in the fourth line to display a dialog box to user asking him his name. The last statement prints out the name typed in previously with 'is a silly name' suffix. 

In this lesson, you learnt how to configure NetBeans to run JAVA and also how to use JOptionPane to ask data from user. Practice more to achieve perfection.

Ads

Look into BLOG ARCHIVE for more posts.