Debugging concat(String227 other)mercer/Presentations/DebugConcat.pdf · ÔTÕ ÔuÕ ÔcÕ ÔsÕ...
Transcript of Debugging concat(String227 other)mercer/Presentations/DebugConcat.pdf · ÔTÕ ÔuÕ ÔcÕ ÔsÕ...
Debugging concat(String227 other)First run this test to show concat works if the BUFFER_SIZE is large enough
@Testpublic void testConcat() { char[] a1 = { 'a', 'b', 'c' }; char[] a2 = { 'D', 'E', 'F', 'G', 'H' }; String227 str1 = new String227(a1); String227 str2 = new String227(a2); // Concatenate str2 at the end of str1 when BUFFER_SIZE = 10 str1.concat(str2); assertEquals("abcDEFGH", str1.toString()); assertEquals(8, str1.length());}
Sunday, September 12, 2010
Concat when buffer is big enough
The buffer allows concatenations with no array growth
public void concat(String227 other) { int totalSpaceNeeded = this.length() + other.length(); if (totalSpaceNeeded < theChars.length) { // Enough room in the buffer--add other.theChars at end int rightIndex = n; for (int index = 0; index < other.length(); index++) { this.theChars[rightIndex] = other.theChars[index]; rightIndex++; } n += other.length(); } else combineInLargerArray(totalSpaceNeeded, other); }
Sunday, September 12, 2010
char[] a1 = { 'T', 'u', 'c', 's', 'o', 'n' };String227 str1 = new String227(a1);char[] a2 = {' ', 'A', 'r', 'i', 'z', 'o', 'n', 'a'};String227 str2 = new String227(a2);
Instance Variables when BUFFER_SIZE = 10
‘T’ ‘u’ ‘c’ ‘s’ ‘o’ ‘n’
theChars
6n
‘ ’ ‘A’ ‘r’ ‘i’ ‘z’ ‘o’ ‘n’ ‘a’
theChars
8n
a1
a2
Sunday, September 12, 2010
‘T’ ‘u’ ‘c’ ‘s’ ‘o’ ‘n’
theChars
6n
‘ ’ ‘A’ ‘r’ ‘i’ ‘z’ ‘o’ ‘n’ ‘a’
theChars
8n
a1
a2
Buffer (10) not big enough, get one array to store all characters
temp
private void combineInLargerArray(int totalSpaceNeeded, String227 other) { char[] temp = new char[totalSpaceNeeded];
Sunday, September 12, 2010
‘T’ ‘u’ ‘c’ ‘s’ ‘o’ ‘n’
theChars
6n
‘ ’ ‘A’ ‘r’ ‘i’ ‘z’ ‘o’ ‘n’ ‘a’
theChars
8n
a1
a2
temp ‘T’ ‘u’ ‘c’ ‘s’ ‘o’ ‘n’
// Place this array's characters into temp for (int i = 0; i < n; i++) { temp[i] = theChars[i]; }
Sunday, September 12, 2010
‘T’ ‘u’ ‘c’ ‘s’ ‘o’ ‘n’
theChars
6n
‘ ’ ‘A’ ‘r’ ‘i’ ‘z’ ‘o’ ‘n’ ‘a’
theChars
8n
a1
a2
temp ‘T’ ‘u’ ‘c’ ‘s’ ‘o’ ‘n’ ‘ ’ ‘A’ ‘r ‘i’ ‘z’ ‘o’ ‘n’ ‘a’
int rightIndex = 0; for (int i = n; i < totalSpaceNeeded; i++) { temp[i] = other.theChars[rightIndex]; rightIndex++; }
n += other.length();
Don’t forget to update n so the length() returns sum of characters
Sunday, September 12, 2010
Debugging
Run the unit test
There is a bug resulting in an ArrayIndexOutOfBoundsException
Sunday, September 12, 2010
Debugging: A necessary part of Software Development
Why use a debugger? To help fix errors
Eclipse has a "source level" debugger that can be used to:
Step through your own Java code one statement at a timePause a running program at any point (set a "breakpoint" and start debugging session) Examine the content of variables (all available are show in the Variables view)
Sunday, September 12, 2010
Three steps to debugging:1) Realize you have an error Good test cases and JUnit assertions can expose bugs 2) Locate the source of an error--finding the method is a good start - Set a breakpoint near the failing assertion
3) Fix the error - Step into the method (F5) - Step through the code (F5, or F6 to step over other methods) - Observe variables as they change - Observe the flow of statements - Stepping through code can help you see things that were not otherwise apparent
Sunday, September 12, 2010
Debug it now
Failure discovered in toString, but set a breakpoint at the concat message
Sunday, September 12, 2010
‘T’ ‘u’ ‘c’ ‘s’ ‘o’ ‘n’
theChars
6n
‘ ’ ‘A’ ‘r’ ‘i’ ‘z’ ‘o’ ‘n’ ‘a’
theChars
8n
a1
a2
temp ‘T’ ‘u’ ‘c’ ‘s’ ‘o’ ‘n’ ‘ ’ ‘A’ ‘r ‘i’ ‘z’ ‘o’ ‘n’ ‘a’
theChars = temp;
Forget to make theChars reference the larger array!
Sunday, September 12, 2010
‘T’ ‘u’ ‘c’ ‘s’ ‘o’ ‘n’
theChars
6n
‘ ’ ‘A’ ‘r’ ‘i’ ‘z’ ‘o’ ‘n’ ‘a’
theChars
8n
a1
a2
temp ‘T’ ‘u’ ‘c’ ‘s’ ‘o’ ‘n’ ‘ ’ ‘A’ ‘r ‘i’ ‘z’ ‘o’ ‘n’ ‘a’
14n
Sunday, September 12, 2010