Thursday 10 January 2013

Learn Basic Java in 10 Minutes


What is Java?

You can find tons of definition about Java on web; Answer is simple “Programming Language to develop any kind of application with Object Oriented approach and it can run anywhere”.

Sample application
For example, you need to develop an application to track your daily expenses on your PC. The application can be developed by Java by the following manner.

Minute 1 Session
First download and install Java Development Kit (JDK) from official Oracle site. The JDK is the software environment where you can compile and run your Java programs.  Download here.

Minute 2 Session
Know the commands to compile and run a Java program.  Before that you need to set the path to point your Java compiler on your development location. Assume that you have installed JDK on your C:\Program Files\Java\jdk1.7.0 drive. You need to run the following line of code in your command prompt before accessing Java compiler.
SET PATH = C:\Program Files\Java\jdk1.7.0\bin;
Open start menu and type cmd on your Windows machine to open command window.  Now open your folder where your Java programs are saved.

For example: D:/MyJava.
Cd  D:/MyJava
D:MyJava > SET PATH = C:\Program Files\Java\jdk1.7.0\bin;

Note that “jdk1.7.0\bin” folder contains Java compiler (javac) and Java interpreter (java) applications.
Now you are ready to run a sample Java program on your machine. Unlike like regular hello world application, we are going to run a practical program to track daily expenses.

Minute 3 Session
Just learn about simple Java program and know the basic steps to compile and run a program.
A Java class is a blueprint of an object. An object in an instance of a class.

Step1: Identify Java classes
Step2: Develop Java classes
Step3: Develop main class
Step4: Compile all classes
Step5: Run main class

Main class is a Java class which has interpreter calling method named main like below:

public void main(String args[]){ // main method to run a Java program
// Code to call other objects/classes
}

Minute 4 Session
In our case, we need to develop an application for daily expense tracking. The following classes may satisfy these requirements.
ExpenseTracker
ExpenseTrackerMain

Minute 5 Session
Develop ExpenseTracker Java class.

public class ExpenseTracker {

public void addExpense(String expenditure, double amount){
System.out.print(“Expenditure ”+ expenditure+”  “+amount+” added on ”+new java.util.Date());
}

}

Save this file as ExpenseTracker.java in your working folder.

Minute 6 Session
Develop ExpenseTrackerMain class to run this application.

public class ExpenseTrackerMain {

public static void main(String as[]){
ExpenseTracker tracker = new ExpenseTracker();
String expen = JOptionPane.showInputDialog(null, “Expenditure”);
String amount = JOptionPane.showInputDialog(null, “Amount”);
Double doubleAmount = Double.parseDouble(amount);
tracker.addExpense(expen, doubleAmount);
}

}

Minute 7 Session
Compile your application.  Just open your working directory where you saved these .java files and follow the below steps.
Open command prompt and go to your working directory.

Type SET PATH = C:\Program Files\Java\jdk1.7.0\bin; // recall Minute 2 session
Type javac *.java
This command will compile both ExpenseTracker and ExpenseTrackerMain Java files. This action will generate .class files inside your working directory.

Minute 8 Session
Run your application.
Open command prompt and go to your working directory.
Just type java ExpenseTrackerMain in your command window.

Minute 9 Session
Your first Java program is done.
What is next?
Just add some methods in ExpenseTracker Java file, call those methods in ExpenseTrackerMain class, compile and run again.

Minute 10 Session
Similar to Expense Tracker application develop Task Tracker application by using the following code snippet.

public void addTask(String task, Status status){
System.out.print(“Task ”+ task +”   added on ”+new java.util.Date()+”.  Status of this task is :”+status);
}

One of the best ways to develop your Java knowledge is by Oracle’s Java Certification program. There are numerous vendors available to teach Java in the world but there are few vendors are having good knowledge and latest updates.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.