Debugging concat(String227 other)mercer/Presentations/DebugConcat.pdf · ÔTÕ ÔuÕ ÔcÕ ÔsÕ...

12
Debugging concat(String227 other) First run this test to show concat works if the BUFFER_SIZE is large enough @Test public 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

Transcript of Debugging concat(String227 other)mercer/Presentations/DebugConcat.pdf · ÔTÕ ÔuÕ ÔcÕ ÔsÕ...

Page 1: Debugging concat(String227 other)mercer/Presentations/DebugConcat.pdf · ÔTÕ ÔuÕ ÔcÕ ÔsÕ ÔoÕ ÔnÕ theChars n 6 Ô Õ ÔAÕ ÔrÕ ÔiÕ ÔzÕ ÔoÕ ÔnÕ ÔaÕ theChars

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

Page 2: Debugging concat(String227 other)mercer/Presentations/DebugConcat.pdf · ÔTÕ ÔuÕ ÔcÕ ÔsÕ ÔoÕ ÔnÕ theChars n 6 Ô Õ ÔAÕ ÔrÕ ÔiÕ ÔzÕ ÔoÕ ÔnÕ ÔaÕ theChars

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

Page 3: Debugging concat(String227 other)mercer/Presentations/DebugConcat.pdf · ÔTÕ ÔuÕ ÔcÕ ÔsÕ ÔoÕ ÔnÕ theChars n 6 Ô Õ ÔAÕ ÔrÕ ÔiÕ ÔzÕ ÔoÕ ÔnÕ ÔaÕ theChars

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

Page 4: Debugging concat(String227 other)mercer/Presentations/DebugConcat.pdf · ÔTÕ ÔuÕ ÔcÕ ÔsÕ ÔoÕ ÔnÕ theChars n 6 Ô Õ ÔAÕ ÔrÕ ÔiÕ ÔzÕ ÔoÕ ÔnÕ ÔaÕ theChars

‘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

Page 5: Debugging concat(String227 other)mercer/Presentations/DebugConcat.pdf · ÔTÕ ÔuÕ ÔcÕ ÔsÕ ÔoÕ ÔnÕ theChars n 6 Ô Õ ÔAÕ ÔrÕ ÔiÕ ÔzÕ ÔoÕ ÔnÕ ÔaÕ theChars

‘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

Page 6: Debugging concat(String227 other)mercer/Presentations/DebugConcat.pdf · ÔTÕ ÔuÕ ÔcÕ ÔsÕ ÔoÕ ÔnÕ theChars n 6 Ô Õ ÔAÕ ÔrÕ ÔiÕ ÔzÕ ÔoÕ ÔnÕ ÔaÕ theChars

‘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

Page 7: Debugging concat(String227 other)mercer/Presentations/DebugConcat.pdf · ÔTÕ ÔuÕ ÔcÕ ÔsÕ ÔoÕ ÔnÕ theChars n 6 Ô Õ ÔAÕ ÔrÕ ÔiÕ ÔzÕ ÔoÕ ÔnÕ ÔaÕ theChars

Debugging

Run the unit test

There is a bug resulting in an ArrayIndexOutOfBoundsException

Sunday, September 12, 2010

Page 8: Debugging concat(String227 other)mercer/Presentations/DebugConcat.pdf · ÔTÕ ÔuÕ ÔcÕ ÔsÕ ÔoÕ ÔnÕ theChars n 6 Ô Õ ÔAÕ ÔrÕ ÔiÕ ÔzÕ ÔoÕ ÔnÕ ÔaÕ theChars

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

Page 9: Debugging concat(String227 other)mercer/Presentations/DebugConcat.pdf · ÔTÕ ÔuÕ ÔcÕ ÔsÕ ÔoÕ ÔnÕ theChars n 6 Ô Õ ÔAÕ ÔrÕ ÔiÕ ÔzÕ ÔoÕ ÔnÕ ÔaÕ theChars

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

Page 10: Debugging concat(String227 other)mercer/Presentations/DebugConcat.pdf · ÔTÕ ÔuÕ ÔcÕ ÔsÕ ÔoÕ ÔnÕ theChars n 6 Ô Õ ÔAÕ ÔrÕ ÔiÕ ÔzÕ ÔoÕ ÔnÕ ÔaÕ theChars

Debug it now

Failure discovered in toString, but set a breakpoint at the concat message

Sunday, September 12, 2010

Page 11: Debugging concat(String227 other)mercer/Presentations/DebugConcat.pdf · ÔTÕ ÔuÕ ÔcÕ ÔsÕ ÔoÕ ÔnÕ theChars n 6 Ô Õ ÔAÕ ÔrÕ ÔiÕ ÔzÕ ÔoÕ ÔnÕ ÔaÕ theChars

‘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

Page 12: Debugging concat(String227 other)mercer/Presentations/DebugConcat.pdf · ÔTÕ ÔuÕ ÔcÕ ÔsÕ ÔoÕ ÔnÕ theChars n 6 Ô Õ ÔAÕ ÔrÕ ÔiÕ ÔzÕ ÔoÕ ÔnÕ ÔaÕ theChars

‘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