CSC 160 Lab 1-1

Beginning Object-Oriented Programming

A Java program is a collection of classes and objects. Think of an object as a software model of things in the real world, like people, cars, chairs, airplanes, or buildings. Things in the real world may have properties or characteristics that can be regarded as data or information about that object. For example, a person may have a name, weight, hair color, eye color, date of birth, gender, etc. A chair may be characterized by the material out of which it is made, its purchase cost, the height of its seat off the floor, etc. Think of these characteristics as data fields of the object.

An object may have the ability to do different things. A person may know how to speak, how to write, how run, etc. An ability to do something is called a method of the object. A method is really a named list of instructions. (That is, a list of instructions that has been given an name).

A class is a description an object. You may think of the class a pattern for creating the object. You write a class by giving the class a name (called an identifier) and then listing all the data fields and methods that objects created from the class will have.

The simplest Java program will consist of one class, referred to as the main class of the program. The main class must have at least one method, called the main method of the class. The name or identifier of the main class may be anything, but the main method of the main class must be called main.

Using Netbeans

We are now ready to begin the journey that will take us to magical world inhabited by computer programmers. We will use the Netbeans IDE to create a simple Java program. The program will write the words

Hello World!

when it is executed. That is not much, but the journey of a thousand miles begins with a single step. In time, we will learn to write more sophisticated programs.

Go to the Windows start menu and find the Netbeans program and start it. Select the File menu, and then select New Project.

On the ensuing screen, select Java in the left Pane and Java Application on the right, and click Next. On the next screen,

Enter the name MyProg1 for the name of your project. You can use the Browse ... button to select the folder where your project will be saved. (I recommend that you bring a flash drive to lab and always store your work on the flash drive. Once you are done, backup your flash drive to your F drive on the College Network. You can also store your work on the C-drive in your own folder. Make sure that you always backup your work to your F drive when you are done.)

Leave the Create Main Class option checked so that Netbeans will create the main class for you. Notice that Netbeans will use the name of your project as the name of your main file.

Click Finish to indicate that you are done setting up your project. You will now be looking at a screen such as the one below:

There are four main parts to this screen : (1) the main menu and toolbar across the top, (2) the Projects, Files and Services pane at top left under the toolbar, (3) The Navigator pane at bottom left, and (4) The Edit window on the right under the toolbar.

The main menu and toolbar

The items on the menu that we will use most often are File to add new files to your project, or to save a file to disk, and to open an existing file; Edit to edit your files that are already open and Run to execute your programs.

The Projects, Files, and Services Pane


This pane has three tabs, or tabbed pages, headed by Projects, Files, and Services. By Selecting one of these tabs, you can see all your projects that are currently open in Netbeans, all the files in the current project, and the services available to your project.

The image on the right shows what you would see when the Projects page is selected. The code that you write, which will be translated by the compiler, is called source code. All source code must be in files whose name has a .java extension. In addition, Java source files that are stored in the same folder are called a package. The name of the package is always the same as the name of the folder. This program has a single package called myprog1. Inside that package is a single file named

The Navigator Pane

The Navigator pane, in the bottom left corner, helps you navigate through the various files, the classes in those files, and the data fields and methods in those classes. This is useful when you want to find a field or method of a class quickly. You simply locate it on the Navigaor pane and click on it, and the field or method will come up in the Edit pane.

The Navigator pane shown on the right displays the main method, which is the only method in the MyProg1 class.

The Edit Window

The Edit pane will occupy most of your time when you are developing your program. It display the code in the file you are currently editing. Here Netbeans has created a main class for us, called MyProg1, and has added a main method to our main class, as you can see below.

Package Statements, Comments, Classes, and Methods

A Java program may have comments. A comment is a part of the program that is completely ignored by the compiler. Comments exist in programs to help people reading those programs understand what the program is doing.

Java has both single-line and multi-line comments. A single-line comment begins with two slashes // and ends at the end of the line. You can see a single-line comment in the above figure on line 17. You must write the two slashes with no spaces between them.

A multi-line comment begins with the two characters /* and ends with */. You may not have any spaces between the two characters. The above image shows three such comments: lines 1-3, lines 7-10, and lines 13-15.

The package statement on line 5 tells the compiler what package the classes in this file belong to. This statement says that the classes in this file belong to the myprog1 package. The package statement must always be first in a file, not counting comments, which are ignored by the compiler. If there is no package statement, the classes in this file are placed in a default package, which has no name.

This file has a single class, called MyProg1, which begins at line 11 and ends at the right brace } on line 19. A class always begins with a keyword class, followed by the name of the class, followed by a left brace {

        public class MyProg1 {

Following the opening { of the class, there comes a list of all the fields and methods that belong to the class. This class has only one method, main which begins at line 16 and ends with the closing brace on line 18.

The single-line comment on line 17 tells the you, the software developer, where to write the instructions that will tell the method how to do what is is supposed to do.

Although comments are important, let us delete them from this example to save space, and to illustrate how to use the editor. Do this by selecting the comments with the mouse, selecting Edit from the main menu, and then selecting Delete off of the Edit menu, or you can just hit the Delete key on your keyboard. You should now have the following:

        package myprog1;

        public class MyProg1 
           public static void main(String[] args) 

Making the Method Do Something

The body of the main method, between the two braces { and } is empty: there are no instructions or statements to tell the method to do anything. We will now tell the method print a message

Hello World!
Carefully type a print statement betweent the two braces of the method, so that you have
       package myprog1;

       public class MyProg1 
          public static void main(String[] args) 
            System.out.println("Hello World!");
in the Edit Window. Save the file to disk, by using the File menu, and then Save or by clicking on the icon of the two floppy disks on the tool bar. Next, Select Run on the main menu, then Run File. The compiler will compile the program and build it, and then run it. When the program is done running, the IDE will create an Output window below the Edit window, and the output of the program will be displayed there:

The System class and out objects

Remember from lecture that a Java class can have fields and methods, and that an object can also have fields and methods. It is OK if you do not understand everything now, we will clear things up later. What we need to understand for now is that Java has a class called System: this class represents the computer as a whole. The System class has a data field called out, and this out object has methods called print, and methods called println, which can print data that is passed to them.

To access the out object of the System class, you write

and to access the println method of the the System.out object, you write
Finally, to make the System.out object use its println method to print the string "North Central College", you write
System.out.println("North Central College");
Note that in Java, every statement must be terminated by a semicolon.


(1) Modify the program so that the main method looks like

            public static void main(String[] args) 
and run the program. Second, modify the program to use print instead of println:
            public static void main(String[] args) 
Run the program. What can you conclude?

(2) Modify the program so the main method becomes

          public static void main(String[] args) 
             System.out.print("North\nCentral\nCollege\n\nNaperville IL\n");       
What can you say about the effect of the two character combination \n when embedded in a string?

(3) Write a program that uses a single print statement to draw the pattern of stars


End Game

This lab does not have to be turned in for grading.