OOP Lab 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 32

Department of Computing

CS 212: Object Oriented Programming

Class: BESE-10(AB)
Lab01: Familiarity with NetBeans IDE & Basic Programming in
Java

Date: 22nd January, 2020


Time: 9:00am- 12:00pm & 2:00pm-5:00 pm

Instructor:
Ms. Hania Aslam

CS 212: Object Oriented Programming Page 1


Learning Objectives

The purpose of this Lab is to familiarize students with the programming environment that they will be
using throughout this course for software development purposes. This lab introduces the basics of
NetBeans IDE and demonstrates how IDE software facilitates the process of programming.

After completing this lab you will be able to use different features of Netbeans IDE. Your first activity
will be creating a simple "Hello World" program using Java. As you carry out the steps in this lab, it is
important to think about what you are seeing and to understand what's going on. The more attentive you
are here the fewer headaches you'll have later!

Lab Activity Outcomes:

 The students will know how to download, install and use NetBeans IDE.
 Further they will learn how IDE software facilitates the process of programming.
 The students will write their first java program and do further simple exercises.

Basic Lab Instructions!

 Talk to your classmates for help.

 You may want to bring your textbook to future labs to look up syntax and examples.

 Stuck? Confused? Have a question? Ask a TA/Lab Engineer for help, or look at the book or past
lecture slides.

 Before you leave today, make sure to check in with one of the Lab Engineers/TAs in the lab to
get credit for your work.

Using an IDE – A Smarter Way To Code:


An IDE is more than just a text editor: it’s a set of integrated tools that assist you with the typical
development workflow of coding, testing and debugging, profiling, compiling, running, and deploying
applications. With NetBeans IDE, it is easy to effectively write your code, manage and build large
software projects, and maintain control of multiple file revisions in a team environment.

Introduction to Netbeans
NetBeans IDE is a free, open source, popular, integrated development environment used by many
developers. Out of the box, it provides built-in support for developing in Java, C, C++, XML, and HTML
applications. NetBeans IDE is available for Windows, Mac, Linux and Oracle Solaris. The NetBeans
platform and IDE are free for commercial and non-commercial use, and they are supported by Sun
Microsystems.

CS 212: Object Oriented Programming Page 2


Please perform the following steps if NetBeans IDE is not already installed on your system.

Steps to Download Java and NetBeans:

1. Download the JDK wi=th NetBeans IDE Java SE bundle

 Select the Accept License Agreement radio button.


 Select the Download link next to the latest version that matches your Operating System
(Such as Windows or Macx64).
 Save and then execute the installer.

Windows OS:

CS 212: Object Oriented Programming Page 3


2. Select Next.

CS 212: Object Oriented Programming Page 4


3. Take the defaults. Make sure JDK is listed, and then select Next

4. Leave Check for Updates option as checked. Then click Install to proceed with the
installation process.

CS 212: Object Oriented Programming Page 5


5. Uncheck the option to Contribute to the Netbeans project. Then click on Finish.

6. Click on the NetBeans IDE icon to launch IDE .

Creating A Sample NetBeans Project:

Making a New Project:


CS 212: Object Oriented Programming Page 6
1. From the File menu, select New Project option.

2. From the Categories menu select Java. From the Projects menu select Java Application.
Then press Next.

CS 212: Object Oriented Programming Page 7


3. The Project Name typed was HelloWorld. The NetBeans generated the rest of the values
(including the Create Main Class). Click on Finish when done. It will take a few seconds as the
project is created.

Here is a new blank project.

4. We will now fill in some code to make the program do something. Replace line 18(inside main
function) with the following:

CS 212: Object Oriented Programming Page 8


System.out.println("Hello World!");

Now we have a new program as shown below:

Running a NetBeans Project

CS 212: Object Oriented Programming Page 9


1. From the Run menu, select Run Project. You can press the keyboard shortcut F6, if you prefer.
Additionally, you may press the green arrow to Run your project.

As shown above, the program output appears inside the Output panel in NetBeans

CS 212: Object Oriented Programming Page 10


A popup window about Usage Statistics may appear when you start NetBeans. If you would like to
participate in anonymous information being sent to NetBeans to help them improve the program, click
on I Agree. Otherwise, you may wish to say No, Thank You.

Debugging Your Programs


1. Let’s make a more complicated program. We will make a New Java Application. The Project
Name will be called Counter. Click on Finish when done.

CS 212: Object Oriented Programming Page 11


2. Now you have a new Java Program created with a skeleton code for you to fill in:

3. At line 19 insert the following code :

CS 212: Object Oriented Programming Page 12


int i=0;

i++;
i++;
i++;

System.out.printf(“%d\n”,i);

The file should look like the one shown below:

4. If we Run the program (Press F6), we see the following output:

The value of variable i is displayed on the screen. But how did it become the value of 3?

Let us debug the program to see what happens to the variables at each stage as the program runs.

5. Select the location the source code that you would like the program to stop at.

CS 212: Object Oriented Programming Page 13


In this case we selected in the source code at line 21.

6. From the Debug menu, select Run to Cursor (or Press the keyboard shortcut F4).

The debugger stopped the program. We can inspect the variables. Make sure the Variables tab in the
lower right part of the window is selected.

CS 212: Object Oriented Programming Page 14


Notice at the variable i, has the value of 0 at this point.

7. From the Debug menu, select Step Over (easier to press F8). The debugger will step over this
instruction to the next instruction.

As you write more complex programs, you may need to use Step Into (to see inside of a method) option.

The value of i now becomes 1.

8. Press F8 again and i becomes 2.

9. If we select line 25 of the program, and select from the debug menu to Run to Cusor (F4).

CS 212: Object Oriented Programming Page 15


It shows that indeed, i has the value of 3, when it will be displayed to the console.

10. If you want to stop the debugger, you can select the Red Stop button.

Formatting Your Source Code

You can have NetBeans automatically format the source code for you.

1. From the Source menu, select Format.

CS 212: Object Oriented Programming Page 16


It will automatically indent, put spaces in between variables and assignments, and fix other formatting
issues.

Consider the code sample, before formatting is applied to it:

After selecting Format option from the source menu, code takes the following form i.e. indentation and
spacing is fixed:

Printing Your Source Code

1. From the File menu, select Print.

CS 212: Object Oriented Programming Page 17


Note that the preview is missing the line numbers!!

2. Let us add them. Select Print Options

CS 212: Object Oriented Programming Page 18


3. Select Line Numbers and Wrap Lines. Then press OK.

CS 212: Object Oriented Programming Page 19


4. Now the preview shows the line numbers and the text wrapped on the page. Press Print to
Print your file.

Exporting / Importing
If you want to move your project to another machine, you can Export it.

1. From the File menu, select Export Project, To ZIP.

CS 212: Object Oriented Programming Page 20


2. Select Browse to specify a location to save the zip file.

CS 212: Object Oriented Programming Page 21


3. Specify the location to save and then press Export.

4. To import the project back on a new machine (or if you used the export as a backup and wish
to restore back the original), from the File menu, select Import Project, From ZIP.

CS 212: Object Oriented Programming Page 22


5. The top Browse button will let you select the ZIP file that contains your project. The bottom
Browse button selects the folder that contains where your NetBeans projects are stored.
Leave the bottom Folder location alone. Select from the top Browse button where you
need to find the ZIP file.

CS 212: Object Oriented Programming Page 23


6. Press Import once things are set properly.

If the project already exists it will replace it(overwrite it). Do this only if you are restoring to replace
the current project files with the files contained in the ZIP file. Otherwise you can Change Import
Folder to place the files in a new folder. Otherwise press Cancel to avoid the import sequence.

Viewing Multiple Class Files:


1. The project has an expandable/collapsible tree listing of the files in your project.

CS 212: Object Oriented Programming Page 24


2. Click on the + symbol to expand it.

3. Double click on the file name to open it.

The symbols next to the file name mean:

Lab Exercises:
1. Create a new project Lab1 in Netbeans IDE as follows.

CS 212: Object Oriented Programming Page 25


Add the following code at line # 19(inside main function) inside your newly created
Lab1.java file:

So that your entire file looks like this:

CS 212: Object Oriented Programming Page 26


Apply debugging process on your code as explained above in the manual. Your debugging
process should start at line#19 and should continue till line#24. Explain the necessary steps
involved and paste screenshots of the result at each stage of the debugging process in the space
provided below.

CS 212: Object Oriented Programming Page 27


Explanation +Screenshot(1)

1. Select line#19
2. Select Debug > Run to Cursor(or F4). It will debug the code at line#19.

3. Select Debug > Step over(or press F8). It will debug the code at next line(line#20).

CS 212: Object Oriented Programming Page 28


Explanation +Screenshot(2)

4. Select Debug > Step over(or press F8). It will debug the code at next line(line#21).

5. Select Debug > Step over(or press F8). It will debug the code at next line(line#22).

CS 212: Object Oriented Programming Page 29


Explanation +Screenshot(3)

6. Select Debug > Step over(or press F8). It will debug the code at next line(line#23).

7. Select Debug > Step over(or press F8). It will debug the code at next line(line#24).

CS 212: Object Oriented Programming Page 30


2. Create another project Activity1 in Netbeans IDE and replace everything inside
Activity1.java file with the following code:

package activity1;

/**
*
* @author seecs
*/
public class Activity1
{
public static void main(String[] args)
{
System.out.println("BESE-9");
System.out.println("Section-A");
System.out.println("Course-OOP");

Once you have pasted the entire code inside Activity1.java file, format the above given code and
paste a screenshot of your formatted code below:

CS 212: Object Oriented Programming Page 31


CS 212: Object Oriented Programming Page 32

You might also like