Professional Documents
Culture Documents
Lab Session 1: Introduction To Java Netbeans: Netbeans Ide - The Smarter and Faster Way To Code
Lab Session 1: Introduction To Java Netbeans: Netbeans Ide - The Smarter and Faster Way To Code
On this download page you see different download bundles. And for Java development only, we can
choose either Java SE or Java EE. We’d recommend you to choose Java EE which supports
comprehensive Java development (Java EE includes Java SE).
CSS 1021
Object Oriented Programming
BS 2020
Department of CS and SE
So, click the Download button in the column Java EE to download NetBeans installer for Java EE
development. The file name of the installer program is something like netbeans-8.2-javaee-
windows.exe (on Windows).
Click on the installer file to start installing NetBeans IDE. You will be asked to install GlassFish and
Apache Tomcat server:
In this tutorial, you don’t need any server. However, you will need them later so let check both, and
click Next.
In the next screen, check ‘I accept the terms in the license agreement’:
CSS 1021
Object Oriented Programming
BS 2020
Department of CS and SE
Click Next.
In the next screen, choose the installation directory and JDK version for the IDE:
And click Install to start installing NetBeans with GlassFish and Tomcat servers. Wait until the setup
complete:
Click Finish.
CSS 1021
Object Oriented Programming
BS 2020
Department of CS and SE
Now you can start NetBeans IDE from the start menu. The splash screen appears:
Create Your First Java Project
Now, let’s create a Java project using NetBeans IDE. Go to menu File > New Project…
Under the New Project dialog, choose Java application as shown in the following screenshot:
CSS 1021
Object Oriented Programming
BS 2020
Department of CS and SE
Click Next to advance to the next step. In the New Java Application screen, type Project Name, specify
Project Location and the main class:
In the Project Name area at the top, type a Name for your Project. Notice how the text at the bottom
changes to match your project name (in the text box to the right of Create Main Class). Note that we
check the option Create Main Class to generate the main class for the application. Here we specify the
package name net.codejava before the class name HelloWorld.
CSS 1021
Object Oriented Programming
BS 2020
Department of CS and SE
The default location to save your projects appears in the Project Location text box. You can change this,
if you prefer. NetBeans will also create a folder with your project name, in the same location. Click the
Finish button and NetBeans will go to work creating all the necessary files for you.
When NetBeans returns you to the IDE, have a look at the Projects area in the top left of the screen (if
you can't see this, click Window > Projects from the menu bar at the top of the software):
Click the plus symbol to expand your project, and you'll see the following:
Now expand Source Packages to see your project name again. Expand this and you'll see the Java file that
is your source code.
This same source code should be displayed to the right, in the large text area. It will be
called HelloWorld.java. If you can't see a code window, simply double click HelloWorld.java in your
Projects window above. The code will appear, ready for you to start work.
You should see the following components, (as shown in the figure above):
Projects window: Top left, contains a tree view of the components of the project, including
source files, libraries that your code depends on, and so on.
Source Editor: Central area, contains files, where most of your work will be done, currently with
a Java source file called Main open.
Navigator: Lower left, useful for quickly navigating between elements within the selected class.
CSS 1021
Object Oriented Programming
BS 2020
Department of CS and SE
The method main() is the main entry to a Java application. All Java programs start from
the main() method. Now, let’s type some code in this method to print “Hello World Java!” on the screen:
If there’s no red marks like this, the code is fine and we’re ready to run the program.
Notice that when you press Ctrl+Space, the editor shows you multiple ways of completing the code at the
cursor, as well as related documentation:
You see, it prints “Hello World Java!”. Congratulations, you have successfully created and run your first
Java program with NetBeans IDE. You can modify the program and run again to experiment more.
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 hyperlinked text. You double-click such a
hyperlink to navigate to the source of an error. You can then fix the error and once again
choose Run | Build Project.
When you build the project, the bytecode file HelloWorld.class is generated. You can see where the new
file is generated by opening the Files window and expanding
the HelloWorld/build/classes/helloworld node.
10 factorial *= i;
11 }
12 System.out.println("The Factorial of " + n + " is " + factorial);
13 }
14}
Single-stepping through the program and watching the values of internal variables and the outputs
produced is the ultimate mean in debugging programs - because it is exactly how the computer runs your
program!
CSS 1021
Object Oriented Programming
BS 2020
Department of CS and SE
Java Basics
Primitive Types
c = a + b;
System.out.println("c = a + b = " + c );
c += a ;
System.out.println("c += a = " + c );
c -= a ;
System.out.println("c -= a = " + c );
c *= a ;
System.out.println("c *= a = " + c );
a = 10;
c = 15;
c /= a ;
System.out.println("c /= a = " + c );
CSS 1021
Object Oriented Programming
BS 2020
Department of CS and SE
a = 10;
c = 15;
c %= a ;
System.out.println("c %= a = " + c );
}
}
Exercises
1. Write a program in Java to implement the formula: Area = Length * Width and use arbitrary
values to find the area of a rectangle.
2. Write a program to calculate perimeter of a square.
3. Suppose a student has achieved 76 marks in English, 88 in Maths, 94 in Physics and 85 in
History. Write a code to find the percentage of the student.