NetBeans IDE 6

Document Sample
NetBeans IDE 6 Powered By Docstoc
					Laboratory 2:
   Course Code: CPCS-202              Course Name: Programming I

Introduction: The purpose of this Lab. is to familiarize you with the
environment you will be using throughout this course. It reviews the
architecture of a computer and discusses the difference between
machine code and high-level programming languages. Finally, you will
see how to compile and run your first Java program, and how to
diagnose errors that may occur when a program is compiled or
executed.

What Is Program?
Programs are sequences of instructions and decisions that the computer
carries out to achieve a task. A computer must be programmed to
perform tasks. Different tasks require different programs.



The Anatomy of a Computer
   Computer Understand Only Machine Level language.
   The CPU reads machine instructions from memory.
     In High Level Language is a language which is very nearer to
      English language in which the programmer expresses the idea
      behind the task that needs to be performed in an English type
      language some of the high level languages are Java, C++, etc.
     Compiler translates the high-level description into machine
      instructions for a particular processor.



Objective: This lab teaches you the following topics.
   To understand the activity of programming
   To learn about machine code and high-level programming
    languages
   To learn the environment you will be using for programming in
    Java. Introduce the basics of NetBeans IDE.
   To become familiar with your computing environment and your
    compiler
   To compile and run your first Java program from command
    prompt as well as from netbeans IDE.




                                  1
Java programming language specification:

          Java Byte Code: Intermediate representation for Java
           programs
          Java Compiler: Transform Java programs into Java byte
           code
          Java Interpreter: Read programs written in Java byte code
           and execute them
          Java Virtual Machine (JVM): Runtime system that
           provides various services to runni
          ng programs
          Java Programming Environment: Set of libraries that
           provide services such as GUI (Eclipse, NetBeans), data
           structures, etc.
          Java is case sensitive. You must be careful about
           distinguishing between upper- and lowercase letters.




                                       2
To compile and run this program, you need to have installed JDK and added a
line to your path statement referring to the directory of where it was install

How to set class path for running java application from command
prompt (DOS), follow the following steps.
Step 1: Start computer  right click and click properties




                                      3
Step 2: click  advanced system setting




Step 3: click  environment variables …




                               4
Step 4: Click  New button (User Variable)




Step 5: Type as shown in the figure the variable name and
Variable value:




Step 6: click (New System variable) and type the following the
variable name and Variable value:




                               5
Example I. Hello Program ( Using command prompt + Note Pad Editor)
                1. type this file into notepad or something


class Hello {
  public static void main (String[] args) {
    System.out.println("Welcome to the world of Java
Programming.");
  }
}

                2) save it as Hello.java(class name + .java)
                3) drop to a command prompt (start Run  cmd)
                4) type javac Hello.java




                5) type java Hello




                6) then watch the magic

To compile and run this program, you need to have installed JDK and added a
line to your path statement referring to the directory of where it was install.

You have now written and completed your first Java program. Looking back
to the above program, you should notice the following. First, Java is case-
sensitive. The commands have to be written like they are above



                                        6
    Welcome to NetBeans IDE!



    This tutorial provides a very simple and quick introduction to the
    NetBeans IDE workflow by walking you through the creation of a simple
    "Hello World" Java console application. Once you are done with this
    tutorial, you will have a general knowledge of how to create, build, and
    run applications in the IDE.
    This tutorial takes less than 10 minutes to complete.
    After you finish this tutorial, you can move on to the NetBeans IDE
    learning trails. The learning trails provide comprehensive tutorials that
    highlight a wider range of IDE features and programming techniques for
    a variety of application types. If you do not want to do a "Hello World"
    application, you can skip this tutorial and jump straight to the learning
    trails.

    Before You Begin
 To write your first program, you'll need to have the following software
 installed on your system:
 The J2SE(TM) Development Kit (JDK), version 5.0 or compatible.

    NetBeans IDE 6.0.

  To download go to the link below and choose "JDK 6 Update 4 with
 NetBeans 6.0.1"
http://java.sun.com/javase/downloads/index.jsp


    Setting Up the Project
    To create an IDE project:
       1. Start NetBeans IDE.
      2. In the IDE, choose File > New Project, as shown in the figure
         below.




                                       7
3. In the New Project wizard, expand the Java category and select
   Java Application as shown in the figure below. Then click Next.




                               8
4. In the Name and Location page of the wizard, do the following (as
   shown in the figure below):
  o   In the Project Name field, type Hello.
  o   In the Create Main Class field, type Hello.
  o   Leave the Set as Main Project checkbox selected.




5. Click Finish.




                                  9
 The project is created and opened in the IDE. You should see the
 following components:
 The Projects window, which contains a tree view of the components of
   the project, including source files, libraries that your code depends on,
   and so on.
   The Source Editor window with a file called Hello open.
   The Navigator window, which you can use to quickly navigate between
    elements within the selected class.




                                     10
Adding Code to the Generated Source File




Because you have left the Create Main Class checkbox selected in the
New Project wizard, the IDE has created a skeleton class for you. You
can add the "Hello World!" message to the skeleton code by replacing
the line:
             // TODO code application logic here

with the line:
                 System.out.println("Hello World!");

Save the change by choosing File > Save.

The file should look something like the following:
/*
 * Hello.java
 *
 * Created on Sep 7, 2007, 6:44:16 PM
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



                                  11
package hello;

/**
 *
 * @author Sonya Bannister
 */
public class Hello {

    /**
      * @param args the command line arguments
      */
    public static void main(String[] args) {
             System.out.println("Hello World!");
    }

}




Compiling the Source File




                              12
To compile your source file, choose Build > Build Main Project from the
IDE's main menu.
You can view the output of the build process by choosing Window >
Output > Output.
The Output window opens and displays output similar to what you see in
the following figure.




If the build output concludes with the statement BUILD SUCCESSFUL,
congratulations! You have successfully compiled your program!

If the build output concludes with the statement BUILD FAILED, you
probably have a syntax error in your code. Errors are reported in the
Output window as hyper-linked text. Click such a hyper-link to navigate
to the source of an error. You can then fix the error and once again
choose Build > Build Main Project.
When you build the project, the bytecode file Hello.class is generated.
You can see where the new file is generated by opening the Files
window and expanding the
Hello World App/build/classes/Hello node as shown in the
following figure.




Now that you have built the project, you can run your program.




                                  13
Running the Program

From the IDE's menu bar, choose Run > Run Main Project.
The next figure shows what you should now see.




Congratulations! Your program works!
You now know how to accomplish some of the most common
programming tasks in the IDE.




                                 14

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:5
posted:11/16/2011
language:English
pages:14