CS 120: Computer Science I

Laboratory 02
Editing, Compiling, Debugging

Overview

This lab will introduce you to the processes of creating, editing, compiling, and debugging Java programs in the Computer Science laboratory. The details may be slightly different if you are working on other machines, but the concepts should be similar. Please take your time, pay attention to what is happening, and feel free to experiment and ask questions. You might want to come back to the lab in a few days and spend some more time getting comfortable with the system. On this lab, I'd suggest working in pairs. Each pair need only submit one copy of the results, but you must declare that you worked together (their is no penalty this time). Details on how to do this inside the file you submit will be given later.

Getting Started

In this lab, we will go through most of the steps in creating and submitting a homework assignment, although some of the programs will be provided (already written). Thus, the first step is to go to the class homepage, and find HW01, which covers the process of compiling a java program. The first program, FirstProgram.java, is an applet. HW01 also gives a brief explanation of how to "run" an applet and provides a basic Web page template to display an applet. Applets are Java programs which can be embedded in Web pages. Applets are a bit fussy however, so most of our early programs will be Java applications, which are run in terminal windows. Go find HW01 and complete it, then return here to move on.

Normally, you will need to go through the following steps to create a program:

Editing

In lab 01 you used a text editor to create a file that you then submitted using the grade program. There are a variety of text editors installed on the Suns, and you may have access to others on computers in your room, home, or office. You many use any editor you like, but I recommend nedit. To use nedit to create your program, open a Terminal window, go to the directory where you want to create your program, and type nedit followed by the name of the program you want to create (given in the assignment) followed by an ampersand (&). This tells the computer to open an nedit window for the specified file, and the ampersand tells the computer to run the editor in the background so that you can type other commands in the same terminal window. If the file does not already exist, the editor may ask to confirm that you want to create it. The name of your file should be identical to the name given in the assignment, or else you will have problems turning it in with the grade program, and the Java compiler will complain also. A caveat of Java applications is that they must contain a method (function definition or subroutine) called main which begins (is declared) with
public static void main (String[] args) {
This should seem very odd now, just take it as a fact for a while...

Once you have the editor open, start typing in the program. Include all of the comments, since they are an important part of every program. Save your program regularly as you work, so that if the power goes out or the computer crashes you won't have lost all of your work. Don't forget that Unix is case sensitive!

There are several nice features for programmers in nedit. If Highlight Syntax is selected in the Preferences menu, and if nedit can tell what programming language you are using, it will use different colors and type styles to indicate comments, keywords, literals, and other items in your program. This may be distracting at first, but it will help you avoid making syntax errors, so I encourage you to use it.

Compiling

The compiler converts your Java source code (a name.java text file) into Java byte code (something like name.class) which is used by the Java virtual machine that runs your program. (Compilers for other languages usually generate machine code.) We will be using the Java Development Kit (JDK) version 1.1.5, which is available for free from Sun Microsystems. To compile your program, go to the Terminal window and type javac followed by the name of your program, including the .java extension. (See the javac man page for options if you are curious.) For example, if your file is named FirstProgram.java, you would type javac FirstProgram.java to compile it. If all goes well (it normally won't), the Java byte code will be placed in the file FirstProgram.class (in the same directory where you compiled the Java source code). In the terminal window, execute the Unix command ls -l (list directory in long format) to see if the class file is present. If you get errors, move on to debugging.

Debugging

Most likely, the compiler will print one or more error messages, indicating where it encountered problems with your program. Read the messages carefully, and try to figure out what part of the program caused the problem. Look for things like missing punctuation, misspelled words, comments or strings that don't begin and end correctly, etc. The messages often indicate the line number on which the error occured; the Statistics Line option in nedit will show you what line you are on.

For example, here are the messages when I misspelled a keyword (static) and forgot the double quote at the end of a string (Welcome to Java!):

If the compiler prints more than one error message, it's usually best to concentrate on the first few; later errors may be caused by earlier errors, and may go away. Keep in mind that an error may not be caused by the line shown in the error message; the compiler does its best to help you, but sometimes it guesses incorrectly. If you have many many errors, use the scroll bar on your Terminal window to scroll up to the first few.

When you think you've corrected the problems, save your source file, compile, and see what happens. Remember that debugging takes practice, and will get easier with time.
 

Running a Java Application

Eventually, javac won't print any error messages, and a .class file will be created in your directory. The class file's name will be taken from the name of the class defined in your program. (If you have multiple classes, multiple files will be created, but we won't worry about that right now.) When this happens, type java followed by the name of the class (without any extension) to run your program. For example, if your file contains the class mytest, the compiler will create the file mytest.class, and you would type java mytest to run it.

Remember, there are two stages to debugging; the first is correcting syntax errors so that your program will compile, and the second is correcting semantic errors so that your program produces correct output. As time goes on, you'll spend less and less time on syntax errors, and more and more time on semantic errors...

One way to get a better understanding of what your program is doing is to add statements to print important variables within your program, or to print messages like ``got here'' at specific points in the program. Test your program carefully to make sure it works correctly; I am pretty good at spotting programs that only work some of the time...

Once your program produces the correct output, you should use the grade program to submit it. If you're working as a team, one of you can logout and the other can login, and you can start working on the other program. In Java, you include comments in a program either like this

/* a multi-line
comment */
or, for partial line comments, anything on a line following two backslashes, // such as this is ignored.
Each program you create should have your names and other pertinent information in a comment at the top. The rest of this lab will be passed out and handed in on paper (so you're forced to type in the code yourself).

General Advice

Hang on - we're almost through the part of the course that can be most difficult and frustrating. Now that you've edited, compiled, debugged, and run one program, you're well on your way to writing more complicated programs.

We will probably have fewer of these ``formal'' labs in the future; instead we'll use the lab period as an opportunity to work on programs when I can be in the lab to offer advice and assistance.

Summary

Once you have edited, compiled, and debugged all of the programs in this lab, you'll be ready to start working on more realistic programming assignments. Take your time, write your code and comments carefully, be sure to ask questions when you get confused.


This page maintained by Tom Linton, linton@cs.moravian.edu.