Wednesday, February 20, 2008

Need for Netbeans



Why is netbeans necessary??


"Necessity is the mother of all inventions", goes the old saying. Well my dear friend,its the same case here also. Although Java has become inseparably linked with the Internet, it is important to realise that java is first and foremost a programming language. Let us just take a ride down the history lane and look at the development of programming languages. We all know that it was C which first got the status of a "programmer's language". C was developed from 'B' and this fact is well known. However, C++ was also in the coming and it came! Why did this happen? Did the arrival of C++ prove C to be an incompetent language?

NO! As the complexity and demand grew, so did all the computer languages. So after C and C++, the stage was set for Java!! After installing jdk and jre, one can execute java applications at the command prompt. In this feature, it is important for the programmer to remember all commands and recollect them at the appropriate time. Isn't this all a little tedious? Don't we like everything at the click of a button? We always aim for more compact and steady platforms! Many paradigms of view.. god o god!

And all this brings us to confluence of IDE and java applications onto a single platform.. NETBEANS IDE!!! :D I know I seemed like it was the most obvious thing.. ha ha.. Well well,I just saved you a lot of work which would have taken you a lot of time to figure out.. All my blog readers.. consider yourselves blessed!! :D

Experiment Time



Netbeans is NOT the place where one can gain expertise in java! So if you are a novice, I say-"stick to routines". Just to make my point felt, I want to cite an example. Consider this program.



class Fun
{
public static void main(String[] args)
{
int count,i=0;
String words;
count=args.length;
System.out.println("Number of arguments= " +count);
while (i < count)
{
words=args[i];
i++;
System.out.println(i+ ":" + "Java is " +words+ "!");
}
}
}



Execute this program in jdk and at the command for compilation,give the following statement(let your filename be Fun!)

java Fun Robust Simple Secure Portable Multithread

The output of your program would be as follows:

Number of arguments=5
1: Java is Robust!
2: Java is Simple!
3: Java is Secure!
4: Java is Portable!
5: Java is Multithread!

Isn't this such a cool output!!! Now, you know the use of String[] args!!! If you were a starter, I sure can swear that you wouldn't have figured this out.. As far as my knowledge with netbeans goes, I don't think there is an option where I can provide arguments at run-time.. (If there is that option, I am yet to discover it then!!)




Observe


One of the main advantages of using IDE is that it makes life easy for a programmer.
For example,if I were to type a program which has a command to print a statement,one can observe that a window pops up as soon as "System." is typed. This window is as shown below.





This happens because the System class contains several class fields and methods and they cannot be instantiated. The facilities provided by the System class include standard input, standard output, and error output streams. By convention, this output stream is used to display information or messages that are intented to be attended by the user. "println" which is normally added at the terminal end belongs to the PrintStream. This class prints Java primitive values and object to a stream as text. Errors if present can be detected by calling the checkError() method. Additionally, this stream can be designated as "autoflush" when created so that any writes are automatically flushed to the underlying output sink when the current line is terminated. "println" just terminates the current line by inserting a line separator string(which does not mean \n).

Taking the next step


Before we proceed further,one must understand the essence of Beans. To better understand its significance,consider the following. Resistors,capacitors and inductors form the building blocks of a simple electronic circuit. All these components can be reused in complex/integrated circuits which probably yield more efficiency. This is because the functionality of every component is understood and it does not change with the system. So,every aspect of the component's behaviour can be documented. Unfortunately,until recently "write-once,run-anywhere" paradigm has not been highly successful in achieving the benefits of reusability and portability. Large applications grow high in complexity and it becomes very hard to enhance. Part of the problem is that ,until recently,there has not been a standard,portable way to write software component. This is where the role of a bean comes into play. As per standard definition, a bean is a "software component that has been designed to be reusable in a variety of environments". It may perform a simple operation such as spell check in a document or a complex one like forecasting the performance of a stock.


Netbeans is one such platform which also aids software development. In netbeans,it is important to realise that the platform recognizes and evaluates strictly in terms of a 'project'.


Time to learn:
Of the Java SE applications of netbeans,there is an application called 'Java project with existing sources wizard'. Click on File->New project. Click on java and choose the 'Java project with existing sources wizard'. Then,choose a project name and a project folder and click next. When adding the source package folder,add the folder which contains your java programs. [In a windows system,by default,all these java programs are present in C:\Program files\Java\jdk(version)\bin]Finally,click finish and your project is created.
NOTE: If you include this folder,then the whole folder becomes a project and your individual programs gain the status of a file.

If you have followed all the steps I have mentioned above,then its first part accomplished


Now,you can run any file present in your project. To open your file,click on your project folder which will open all the files tored in it. In my example,I am going to run a program which is stored as "Accurate.java" in my systen. To illustrate this,I am now going to run a file in my project. The screenshot is as shown below.







The program I have shown is a basic multithread program. Its code is



class NewThread implements Runnable
{
String name;
Thread t;
NewThread(String threadname)
{
name=threadname;
t=new Thread(this,name);
System.out.println("New thread: " + t);
t.start();
}
public void run()
{
try
{
for(int i=15;i>0;i--)
{
System.out.println(name + ":" +i);
Thread.sleep(200);
}
} catch (InterruptedException e)
{
System.out.println(name + "interrupted");
}
System.out.println(name + "exiting");
}
}
class Accurate
{
public static void main(String args[])
{
NewThread ob1= new NewThread("One");
NewThread ob2= new NewThread("Two");
try{
Thread.sleep(1000);
ob1.t.suspend();
System.out.println("Suspending thread one");
Thread.sleep(1000);
ob1.t.resume();
System.out.println("Resuming thread one");
Thread.sleep(1000);
ob2.t.suspend();
System.out.println("Suspending thread two");
Thread.sleep(1000);
ob2.t.resume();
System.out.println("Resuming thread one");
} catch (InterruptedException e)
{
System.out.println("Main thread interrupted");
}
System.out.println("Main thread exiting");
}
}



To run this file,we click on Build->"Complile Accurate.java" (F9). After succesful compilation,we can run the program by clicking Run->Run file->Run "Accurate.java" (or Shift+F6). After this,we can observe the output which is




New thread: Thread[One,5,main]
New thread: Thread[Two,5,main]
One:15
Two:15
One:14
Two:14
One:13
Two:13
One:12
Two:12
One:11
Two:11
Suspending thread one
Two:10
Two:9
Two:8
Two:7
Two:6
Resuming thread one
One:10
Two:5
One:9
Two:4
One:8
Two:3
One:7
Two:2
One:6
Two:1
Suspending thread two
One:5
One:4
One:3
One:2
One:1
Resuming thread one
Twoexiting
Main thread exiting
Oneexiting

Formatting

All programmers know that formatting of a code is very important in any code. This makes the program more clear,especially when the program is too lengthy. On another platforms,the programmer needs to take the extra effort to do this. However,netbeans simplifies everything for you. To make my point clear,consider the screenshot below.






I suggest you to follow the steps carefully to format your code.

1) Select your entire program with the right click of a mouse ( the picture will be as shown above).

2) Right-click and click on the option which reads "format". The shortcut is Alt+Shift+F.

Simple,isn't it?? Just by following these two simple steps,one can arrive at a correct formatted code!