Kotebe Metropolitan University (KMU)
Object Oriented Programming (CoSc2082)
Lab One
Objectives
• To give introductory concept on Java Programming
• To show how to use Netbeans Development Environment (IDE).
• To demonstrate a simple java program.
Like any programming language, to develop programs using java, you need IDE. There are
several IDEs for java programming available for free like Netbeans, Eclipse, etc. The IDE that
we use for developing Java program for this course is Netbeans IDE. The Netbeans IDE holds
everything that you need to develop java program, It serve us editor, compiler, bytecode
interpreter etc. It has several features (that you discover as you work on it) that simplify java
programming more easily. Now let’s see how to use Netbeans in order to write and run a
program that display “hello world”.
1. Step 1: Install Netbeans IDE (it already installed in your computer) – only done once.
2. Step 2: Start Netbeans: double click on the Netbeans IDE start icon. You see the following
window, figure 1.1. This is called the start page. You will make your own Window in
future.
Figure 1.1. Netbeans Start Page.
3. Step 3: Create Project: on the menu at the top, click on File menu → New Project. A new
dialog box appears, called New Project Dialog Box, as shown in figure 1.2. You will make
your own dialog box in future as your capability improves.
In figure 1.2, click Next. You will get a new dialog box, figure 1.3, called New Java
Application dialog box. Make the following modification in figure 1.3.
3.1. Change Project Name field to your project name – for this lab - ‘Lab1’. You can give
any project name you want. But don’t forget to make self explanatory.
3.2. Change the Create Main Class field from ‘Lab1.lab1’ to ‘lab1.Welcome’ – this means
create a Main Class (a class that contain main method) called ‘Welcome’ inside a
1
project called Lab1. Then finally click Finish. You see a new window, called ‘Lab1
Netbeans’ window as shown in figure 1.4. This is a window that you make
everything.
Figure 1.2 New Project Dialog Box
Figure 1.3 New Java Application Dialog box
2
Figure 1.4 ‘Lab1 Netbeans’ window
In ‘Lab1 Netbeans’ window, identify the following window elements.
3.3. Menus at the top for example, ‘File’, ‘View’, ‘Run’, ‘Tools’ are those menus you use
frequently.
3.4. The toolbars below the Menus. The toolbars are shortcut to access the menu
elements. Identify toolbars for ‘Run’, ‘Save’, ‘Undo’, ‘Redo’ etc frequently used.
3.5. Also identify the following window elements: the ‘Project Explorer’ – top left,
‘Navigator Explorer’ – bottom left, ‘Editor Window’ – top right, ‘Output Window’ –
bottom right.
3.6. The project window enables you to follow up your projects. Now, you should see the
project that you create – ‘Lab1’ in this window. The Editor window enables you to
write code – you are going to write the code that display “hello world” in this
window. The output window enables you to see the output, and errors (if any) of
your program when run your application. For example, if you write the code that
display “hello world”, you should see “hello world” in the output window.
3.7. You or other students, may accidentally or deliberately delete the above window
elements, don’t surprise, you can get them back in by using ‘Window’ Menu. Practice
how to display/hide Editor Window, Navigator Window, Output Window and Project
Window. Most importantly, you can reset to the default window layout by Window
→Reset Window. This is very important if someone changes the default layout and
you face a challenge to return back to your layout.
3
4. Step 4: Write Code: You write code that accomplishes your logic in the Editor Window. In
the Editor Window, under main method, write the following code which allows you to
display “Hello World”.
System.out.println ("Hello World");
Do you face a challenge to remember this lengthy code? If so, Netbeans (and other IDEs)
helps you with their ‘Code Completion’, also called ‘Inteli-sense’ feature. This works by
pressing ‘Ctrl + Space’ during coding. Students are highly recommended to practice with
this feature which makes their life simple. Also don’t forget to save your program (not
only at the end but also during coding) by clicking File→Save (for single program) or
File→Save All (for multiple program) or by simply clicking on Save icon in the toolbar.
5. Step 5: Compile and Run the code: Once you finish coding, the next job is to compile, run
and see the output. To do so, you can use one of the following methods.
5.1. Right click on the project located in the Project Window that you want to compile/run
(in our example right click on ‘Lab1’), then click run. OR
5.2. Click on ‘Run’ Menu → ‘Run Main Project’ sub menu. This runs the main project. To
use this method, the project that you want to run must first set as main project. To
do so, click on ‘Run’ Menu → ‘Set Main Project’ sub menu → choose your project to
run. In this case ‘Lab1’. OR.
5.3. Simply click on the ‘Run’ icon in the toolbar. This run the project that is set as main
project. You can also run by selecting any project from a list by clicking on the arrow
in the ‘Run’ icon in the toolbar.
The above three steps compile and run your project. Now compile/run your project by
using one of the above techniques. You should see “Hello World” in the Output Window.
If you don’t see it, something goes wrong. Students should practice with all the above
activities until they display the correct output otherwise they face a challenge on the
coming laboratory activities. Now locate ‘lab1’ the project folder, ‘Welcome.java’ – the
source code file and ‘Welcome.class’ – the bytecode file inside your project folder.
6. Step 6: Copy Project (optional part): this step is important if you want to copy the project
from the computer in the lab to your private computer for more practice. Follow the
following steps.
6.1. All the projects that you develop in NetBeans are located by default at
‘Documents/NetBeansProjects’ path. Follow this path, you will get your project ‘Lab1’
in folder format. Copy ‘Lab1’ and paste it anywhere you want just like any other file.
6.2. To open your project, ‘Lab1’ in any other computer where NetBeans is installed. First
start the NetBeans Application. Then on the Start Window, see figure 1.1, click on File
→ Open Project, then a new dialog box, called ‘Open Project’ dialog box appear. In
the ‘Open Project’ dialog box, in the ‘Look In’ field, browse to the location where you
paste the copied project folder i.e., ‘Lab1’, then click on ‘Lab1’. And, finally, click
‘Open Project’. Now ‘Lab1’ should be opened in NetBeans in your computer.