CS 150, Fall 2017
Lab 9 - Debugger Practice by Fixing Program Bugs
Monday - October 23rd, 2017
Objectives of this Lab
- Fix seven bugs in R9.java,
- while simultaneously learning how to use the debugger in Eclipse, and
- review several different types of Java programming errors.
Getting Started
Create a Java project called R9 and create a class of the same name in the project. Then copy the following code into R9.java.
import java.util.Scanner;
// Author: Jess Cobb
// Assignment: Lab 9
// Class: CS150
// Email: jesscobb@rams.colostate.edu
// Date: 23 October 2017
public class R9 {
public static void main(String[] args) {
// Declare and initialize variables (error 1).
int i0 = Integer.parseInt(args[0]);
int numPrimesBelow50 = 15;
int[] primesBelow50 = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43};
String s0 = "", s1 = "";
String[] sArray = new String[4];
int i1 = 0, counter = 0, h = 0, j = 0, maxInt = 0;
int[] maximumArray = {-5, 5, 65, -22, 88, 79, -66, -1, 0};
Scanner keyboardInput = new Scanner(System.in);
// Print value of i0 to console.
System.out.println("i0 = " + i0);
// Print primes to console (error 2).
System.out.println("Primes below 50: ");
for (int i = 0; i < numPrimesBelow50; i++) {
System.out.print(primesBelow50[i] + " ");
} // end for
System.out.println();
// Prompt and initialize Strings with values (error 3).
System.out.print("s0: ");
s1 = keyboardInput.nextLine();
System.out.print("s1: ");
s1 = keyboardInput.nextLine();
// Print Strings to console.
System.out.println("s0 = " + s0);
System.out.println("s1 = " + s1);
// Print length of String at index 2 of sArray (error 4).
System.out.println("Length = " + sArray[2].length());
// Increment i1 six times by 500,000,000 (error 5).
while (counter < 6) {
i1 += 500000000;
counter++;
} // end while
// Print the resulting value of i1 to the console.
System.out.println("i1 = " + i1);
// Using a for loop, find the maximum integer in maximumArray (errors 6 & 7).
maxInt = maximumArray[0];
for (int i = 0; i < maximumArray.length; j++) {
if (maxInt > maximumArray[i])
maxInt = maximumArray[i];
else
continue;
}
// Print maximum integer in maximumArray to console.
System.out.println("maxInt = " + maxInt);
// Close Scanner object.
keyboardInput.close();
} // end main
} // end class R9
Lab Requirements
The goal of today's lab is for you to become familiar with the debugger in Eclipse. You will also gain first-hand exposure to the following types of exceptions: ArrayIndexOutOfBoundsException, NumberFormatException, and NullPointerException. To get you started today, your TA will walk you through how to use the debugger in Eclipse as well as help you fix the first three errors in this program. You will need to fix the remaining four errors yourself by using the debugger as well as considering general sources of error in a program.
In the code, you have been given hints about the locations of the seven program errors. For example, error 1 can be found somewhere in the following nine lines of code, but exists before the comment hinting at error 2. The rest of the program errors follow this same pattern. For error 4, you may not change the statement that prints the length of a String to the console. However, how you fix this error, once you understand where the problem is, is up to you.
Additionally, after this lab when you visit help desk, do not be surprised if we ask you to use the debugger to find the problem line/s of code instead of reading your entire program and finding the error for you. The debugger is a tool we want you to become familiar with, because it truly can save you time pinpointing an error in future programs you write.
- Have your TA verify you fixed all seven program bugs, then take the Canvas quiz for today's lab.
- Even if you do not finish today's lab, you still must complete the first question of the Canvas quiz.
- NOTE: In order to facilitate better comprehension of how to use the Eclipse debugger, the TAs will not begin to enter the lab completion codes until 30 minutes after the start time of your lab.