CSE1100-13-Debugging1.pdf
Document Details
Uploaded by IncredibleHarmonica
Full Transcript
Midterm results are available on Weblab & Brightspace! 1 Midterm results Introduction to Programming (IP) 2 Midterm Grades 49% at least 6.0 → contributed to bonus 4 # Assignment...
Midterm results are available on Weblab & Brightspace! 1 Midterm results Introduction to Programming (IP) 2 Midterm Grades 49% at least 6.0 → contributed to bonus 4 # Assignment Average score 1 MC operators 77% 2 MC types 70% 3 MC testing 42% 4 MC equality 47% 5 MC heap & stack 34% 6 Binding 32% 7 Pattern builder 85% 8 Bus Stop Advertisements 65% 9 Directory 64% 5 # Assignment Average score 1 MC operators 77% 2 MC types 70% 3 MC testing 42% 4 MC equality 47% 5 MC heap & stack 34% 6 Binding 32% 7 Pattern builder 85% 8 Bus Stop Advertisements 65% 9 Directory 64% 6 # Assignment Average score 1 MC operators 77% 2 MC types 70% 3 MC testing 42% 4 MC equality 47% 5 MC heap & stack 34% 6 Binding 32% 7 Pattern builder 85% 8 Bus Stop Advertisements 65% 9 Directory 64% 7 8 # Assignment Average score 1 MC operators 77% 2 MC types 70% 3 MC testing 42% 4 MC equality 47% 5 MC heap & stack 34% 6 Binding 32% 7 Pattern builder 85% 8 Bus Stop Advertisements 65% 9 Directory 64% 9 10 # Assignment Average score 1 MC operators 77% 2 MC types 70% 3 MC testing 42% 4 MC equality 47% 5 MC heap & stack 34% 6 Binding 32% 7 Pattern builder 85% 8 Bus Stop Advertisements 65% 9 Directory 64% 11 12 # Assignment Average score 1 MC operators 77% 2 MC types 70% 3 MC testing 42% 4 MC equality 47% 5 MC heap & stack 34% 6 Binding 32% 7 Pattern builder 85% 8 Bus Stop Advertisements 65% 9 Directory 64% 13 Binding = associating method definition and method call – Static binding is when the compiler decides which method definition should be used for a method call. This depends on the static type of the receiver. – Dynamic binding is when the runtime/JVM decides which method definition should be used for a method call. This depends on the dynamic type of the receiver. 14 Often made mistake: binding is the process of determining the type Other mistake: no example 15 General Remarks When answering an open question: Answer the full question. If it says, "provide an example", do so. Be concise: if you add things that are incorrect, points will be deducted, even if you also mention correct things. Be precise: statements that are only partially true will not lead to full marks 16 X-axis: practice material on WebLab Y-axis: actual score of mid-term 17 Review opportunity The answers and your spec test scores are on Weblab If you have questions about your grade: There will be lab session Wednesday You can queue there specifically for questions about the midterm 18 Debugging Introduction to Programming (IP) 19 What is a bug? 21 What is a bug? 22 Find the mistake… public static void main(String[] args) { // Calculate 1+2+3+4+5 int sum = sumOfRange(1, 5); System.out.println(sum); } public static int sumOfRange(int from, int to) { int sum = 0; for (int i = from; i < to; i++) { sum = sum + i; } return sum; } 23 Testing helps! @Test public void sumOfRangeTest() { assertEquals(15, sumOfRange(1, 5)); This test fails! } But how do we find the exact location of the mistake? 24 Solution 1: print-statements public static int sumOfRange(int from, int to) { int sum = 0; for (int i = from; i < to; i++) { sum = sum + i; System.out.println("i is now " + i); System.out.println("sum is now " + sum); } return sum; } Looking at the console output tells you how the code was executed. 25 Output i is now 1 sum is now 1 i is now 2 sum is now 3 i is now 3 sum is now 6 i is now 4 sum is now 10 26 Solution 2: The Debugger “playing your program in slow motion” What happens if you click this icon? – Apparently the same as if you would click “run”… 27 Setting breakpoints Double click in the margin If you now run “debug”, the VM will pause the program just before executing the statement marked with the breakpoint. 28 Inspecting values Eclipse’s debug view 29 Eclipse’s debug view Continue executing – Until the next breakpoint or if there is none, until the end Stopping the execution Stepping into – Look what is happening inside a method call Step over – Step over the statement 30 Breakpoint on entire class 31 “Watchpoint”: breakpoint on attribute 32 ADVANCED BREAKPOINTS 33 Right click breakpoint 34 Breakpoint on method Debugger will suspend execution when method is called 35 Breakpoints with number of hits After executing the statement, method, class, attribute x times → stop execution 36 Conditional breakpoints 37 INTELLIJ TIPS 38 Some useful shortcuts Generate code (ALT + INS) Autoformat code (CTRL + ALT + L) Search (SHIFT, SHIFT) Jump to method (CRTL + CLICK) Main Method ("psvm" or "main" + TAB) Println ("sout" + TAB) 39