CSC 216 F09/eclipsedebug

From Expertiza_Wiki
Jump to navigation Jump to search

Formatting Resources

Formatting Help Guide from MetaWiki

Debugging in Eclipse, Problem Solving Exercises

The Problem

This exercise will attempt to help students learn how to use the debugging tools in the Eclipse IDE, as well as serve as a good introduction to some basic debugging techniques. There are various example pieces of code that are flawed in some respect, and the error must be found using the Eclipse tools. Because the amount of time it takes in order to use the Eclipse tools for such small exercises, it would honestly be faster to attempt to find the error by eyeballing it. Because of this, this is not a game or a competition, but rather a learning exercise regarding problem solving skills.

Participants and props

Hopefully, all students with laptops will participate in this exercise.

The 'props' consists of the various code examples that have various errors in them. These examples may be given to the students via a .doc file on the website, so that all students with a laptop will be able to access them. The code may be copied and pasted directly into an Eclipse .java file. All students should have been previously instructed on how to create a project and Java class in that project using Eclipse, therefore creating a project and copying a file to the class will serve as further reiteration of this concept.

There is no 'answer' to the pieces of code, but rather the fact that the code works properly is the answer.

Doing the Exercise

The exercise must be done after learning how to create projects and classes in Eclipse, as well as after learning about how to use the Eclipse Debugging tools.

In order to do this exercise, students will be given access to a file containing various examples of code that each have an error somewhere inside of them. In order to correct the error, students will be encouraged to utilize Eclipse's built in debugging tools. The error are simple enough to find by eyeballing it, but the purpose of this exercise is not only to learn how to debug in a time efficient manner, but also learn how to use the Eclipse debugging tools.

Students will be instructed to create a new Java project called: "Eclipse Debugging". Students must also create a class inside of this project called: "Debug.java".

After each exercise has been completed, students will merely copy and paste the next exercise directly into the "Debug.java" class. This way, multiple classes do not have to be created, and because each exercise is a class with the name "Debug.java", there will be no errors.

Here are a few examples of example exercises that can be given:

public class Debug {

	public static void main(String[] args){
		/*
		 * Given two arrays of integers with the same size and the
		 * same number of elements, add them together by adding the
		 * first element of the first array with the first element of
		 * the second array, and so on.
		 */
		
		int[] a = new int[7];
		int[] b = new int[7];
		int[] c = new int[7];
		a[0]=4;a[1]=1;a[2]=3;a[3]=2;a[5]=4;a[6]=1;
		b[0]=2;b[1]=5;b[2]=5;b[3]=1;b[5]=2;b[6]=2;
		
		for(int i = 0; i <= a.length; i++){
			c[i]=a[i]+b[i];
			System.out.print(c[i] + " ");
		}
		//accepted: 6 6 8 3 0 6 3
	}
}
public class Debug {

	public static void main(String[] args){
		/*
		 * Given a String, print the first x and
		 * last x letters of the String, given x.
		 * 
		 * For example, given "bumblebee", and x = 3,
		 * "bumbee" would be printed.
		 */
		Debug tester = new Debug();
		String printer;
		
		printer = tester.cutOff("Applebees",2);
		System.out.println(printer);  //accepted:  Apes
		printer = tester.cutOff("Second",2);
		System.out.println(printer);  //accepted:  Send
		printer = tester.cutOff("Masterful",2);
		System.out.println(printer);  //accepted:  Maul
		
		
	}
	
	public String cutOff(String original, int x){
		String newString;
		newString = original.substring(1,x) +
			original.substring(original.length() - x, original.length());	
		return newString;
	}
}

After giving the next exercise, student will be given a short amount of time, because of how easy it will be to debug these, to complete them. Then, students will be encouraged to share how they found the error and solved the problem, and which Eclipse tool they used to do accomplish this.

The best way to learn how to do something in coding is to do it and fail until you do it right, therefore the best way to teach it is to allow students to attempt various exercises.


Brent W Austin