Part of your lab today will be a quiz on UNIX commands. You are not allowed to use any references (notes, books, online, person sitting next to you). You will have ten to fifteen minutes. After you finish, you should read over the remainder of this page. The lab TAs will come around to grade your quiz, check your Prelab, and take attendance.
Open an xterm window. The quiz will be handed out to you.
In this lab you will be given several Java programs containing syntax errors (found by the compiler), logic or runtime errors (not found by the compiler; the program compiles and runs but does not function correctly), or both.
For the rest of the semester you will use XEmacs to edit and compile your Java programs. You need to go through the same setup process you learned in Lab 4:
Start X-Win32.
Open an xterm window. Before logging in, enable X11 tunneling.
Press enter to log in. Check that your setup worked by typing
echo $DISPLAY
Create a lab5 subdirectory of your cps001 directory, change into the lab5 directory, and copy files as shown below (you need to execute each of the copy commands separately).
cp /afs/acpub.duke.edu/project/web/cps001/code/debug/*.java .
cp /afs/acpub.duke.edu/project/web/cps001/code/debug/*.html .
You should now have the following .java and .html files in your cps001/lab5 directory:
HelloWorldDebug.java HelloWorldDebug.html
CountDebug.java CountDebug.html
DecisionTreeDebug.java DecisionTreeDebug.html
AddTimeDebug.java AddTimeDebug.html
The .java files you just copied contain errors (syntax, logic, or both). The first file we'll debug is HelloWorldDebug.java. Open this file in in XEmacs. If you can't remember how to use XEmacs, refer to Lab 4. Fill in your name, section number and date in the comment section at the top of the program in the space provided. Put this information at the top of every program, text file, and HTML page you write!. Try to compile this program. If it does not compile, it has some syntax errors and you need to correct them. Be careful of cascading errors! Often, the first syntax error in your program causes other spurious syntax errors. To avoid this trap, practice fixing one error at a time - fixing the first syntax error, then recompiling the program, etc.
Note: The objective of this lab is learning to identify and correct errors in the Java programs you will write in future labs! For this reason, the lab TAs will not in most cases answer questions or provide assistance on compile and runtime problems. Here's some advice though ... remember to change the compile command to match the program you are currently testing! (Compile command: javac NameOfFile.java) This is explained in Lab 4. An incorrect compile command can cause errors (you will see a message beginning with javac Usage:) or you may end up compiling the file you were previously editing instead of the one you are currently working on.
When the program compiles, you should run the applet (you should use appletviewer). Be careful: a program that compiles is not necessarily a program that is correct. Does the program do what it is supposed to? Does the program look like it is supposed to? If a program compiles and runs, but does not produce the correct output, then the program has a logic error. The first step to finding logic errors is to consider test cases to verify that your programs behaves like you think it should. Be sure to test all the possible different cases your program may encounter; one portion of the program may perform just fine while another one does not.
Once you are confident HelloWorldDebug.java is correct, repeat the above process for CountDebug.java and DecisionTreeDebug.java. For each .java file, you need to edit the corresponding .html file and fill in your name, section number and date.
The program you will debug in this section is called
AddTimeDebug.java. Debugging this program will be more challenging
than the programs from Part One. AddTimeDebug.java adds two
time values using only the Java operators mod
(%) and div (/). You should be familiar with
mod and div computations from your Prelab.
Here is a quick overview:
| Command | Explanation |
|---|---|
| % | Modulus calculates the remainder when dividing two integers. Example: 190 % 60 = 10 |
| / | In integer division, the remainder is truncated. Example: 190 / 60 = 3 |
The input and output for AddTimeDebug.java are as follows:
Input: Two times specified in minutes and seconds; these are variables of type int which take on values between 0 and 59, inclusive.
Output: The sum of these times, specified in hours, minutes and seconds. Hours is a variable of type int. Can you figure out what the range of possible values is from the input specifications?
For Part Two of this lab, and some future assignments, you will be asked to turn in test data that verifies your program works. To create test data for your program, you should think of all the possible different cases your program may encounter. For this program, you should create a file in your editor called TestAddTime.txt that contains test data for the following cases:
You should make a single line in your file for each test case. Each line should contain four numbers: the first time in minutes and seconds followed by the second time in minutes and seconds. There should be a tab separating the two times on a single line. For example, the first line of your file might look like the following:
0 0 0 0
Once you have thought of values for each test case, save your file TestAddTime.txt. These will be the values with which you test your program. Before continuing, you should try to identify what part of the code corresponds to each test case. You will have to enter the times into your compiled program manually, but this is just to let your grader know what values you had used to make sure your code works.
Now try to compile the code. If it does not compile, check for syntax errors and correct them. Run the code and test for logic errors using your test cases. At this point, you should know which case causes the incorrect output from your program. You should now fix the logic error before continuing. To fix the error, do not rewrite the program, but rather make as few changes as possible to correct the program.
Submit your .class, .java and your .html files for the five programs you just debugged, along with your .txt file of test cases. The program files you submit should be the correct and fully functional versions! Here is the command:
submit_cps001 lab5/sec# *.java *.class *.html *.txt
You may want to verify that you submitted all the files correctly.