


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Lab; Class: COMPUTER SCIENCE I; Subject: Computer Science and Engineering ; University: University of Nebraska - Lincoln; Term: Spring 2005;
Typology: Lab Reports
1 / 4
This page cannot be seen from the preview
Don't miss anything!



1. Lab Objectives Following the lab you should be able to: Describe the differences between syntax, semantic (logic), and runtime errors. Debug a simple program using print statements. Use the Integrated Debugger to debug a simple Java program. 2. Prior to the laboratory Review the laboratory handout Read “The Integrated Debugger” at http://www.jgrasp.org/tutorials/07_Debugger.pdf Read an “Introduction to Program Testing & Debugging”, http://www.engin.umich.edu/class/eecs100/Assignments/introDebug.pdf Read “The 10 Truths of Debugging”, http://www.duke.edu/web/cps001/notes/Debugging.pdf 3. Topics Topics Covered in Lab Types of errors: 1) syntax 2) semantic 3) runtime Basic debugging techniques Java compiler messages source code review print statements debugger 4. Activities/Exercises 1. Exploring the Integrated Debugger functionality. 2. Using print statements to debug a program. 3. Extra practice debugging Java programs. Activity 1: Exploring the Integrated Debugger functionality Download CountCharacters.java from the course website. The CountCharacters class prints the number of vowels, blank characters, and the number of numeric characters contained in a string entered from the keyboard. Compile and execute CountCharacters.java using the string: o Spring Semester Year 2005 Record the results of executing CountCharacters on your worksheet (Activity 1, Question 1). Set breakpoint #1 in the main() method on the line: inputValue = count.getInput(); Set breakpoint #2 in the main() method on the line: count.processString(inputValue); Set breakpoint #3 in the p rocessString() method on the first line of the for loop: currentString = inputString.substring(i,i+1); Make sure the debug option is selected under the Compiler command in the CSD window. Run the program by clicking on the ladybug button. (Run debugger on current file.)
When the first breakpoint is reached, record which line will be executed next on your worksheet. (Activity 1,Question 2) It will be highlighted in the editor. You will need to enter a string in order for the program to continue. Go to the execution console window in the bottom panel and enter the following string: “1 fish, 2 fish, red fish, blue fish”, then press the enter key. (Don’t enter the quote marks, only the string within the quotes.) Now, click the Step Over button on the debug panel on the left. (The button with the blue arrow pointing downward.) This will allow your program to run to the next breakpoint without stepping into the getInput() method. Record which line will be executed next on your worksheet. (Activity 1, Question 3). Like before, it will be highlighted in the editor. Click the Step Into button: (The button with the curved blue arrow pointing to the right). This will take you into the processString() method of the class. Notice that the string you entered at the prompt can now be seen in the variable window by the variable named inputString. To continue, click on the Resume button with the blue forward symbol (Resume thread selected thread or thread group) to resume execution to the next breakpoint. When the program reaches the third breakpoint, record the value of the variables on your worksheet. (Activity 1, Question 4) The variable names and values are listed under arguments and locals in the bottom panel of the debug tab. Ignore the id and type information also listed there. Record only the values of the variables. Click on the Resume button several times. This breakpoint is inside of a for loop so it will stop at the breakpoint each time the for loop executes. Observe how the variable values change between breakpoints. Remove the breakpoint by clicking on it and click on the Resume button to allow the program to run to completion. The final output should now be printed in the bottom JGrasp panel. Activity 2: Using print statements to debug a program Download SimpleProgram.java and TestSimple.java from the course website.
There are four syntax errors in the two programs. Use the compiling error messages to find and correct the errors in the programs. Record the errors on the worksheet (Activity 2, Question 1). The main() method is in TestSimple.java. The program needs to be run from this class. There are also two semantic errors in SimpleProgram.java , so they do not work correctly. Your next task is to correct the semantic errors. What the program is supposed to do is take a string as its input from the keyboard, and echo it back to the console with a blank character between each character of the input string. It uses the variable: String blank to insert the space between each character in the string. Part 1 : Modify SimpleProgram so that it works correctly (echoes the input string to the console with a blank character between each character of the input string). For example, if the input string is “Today is Tuesday”, the output string should be: o T o d a y i s T u e s d a y The debugging of this program should be done with the use of System.out.println statements. In order to understand what is wrong with the program you need to understand the values of the variables in the method modifyString(). Add one statement to print the value of the variable i and one to print the value of the variable newString. o System.out.println(“ i = “ + i ); o System.out.println(“newString = “ + newString);
o Always start with the first compile error. o When you fix a bug, look for similar bugs in similar parts of the program o Don't ignore bugs "until later" (keep records) o Read and think before typing o Always keep a "clean" copy of your program o Keep a language reference manual at hand o Explain your problem to someone else Why does the compiler sometimes flag the wrong line of code when reporting a syntax error? If a program compiles successfully, do you think it is bug-free? Why or why not?