1 Debugging. 2 A Lot of Time is Spent Debugging Programs Debugging. Cyclic process of editing, compiling, and fixing errors. n Always a logical explanation.

Slides:



Advertisements
Similar presentations
Chapter 15 Debugging. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Debugging with High Level Languages.
Advertisements

Topic 17 assertions and program logic
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 7: Errors 1 Based on slides created by Bjarne Stroustrup.
JAVA BASICS SYNTAX, ERRORS, AND DEBUGGING. OBJECTIVES FOR THIS UNIT Upon completion of this unit, you should be able to: Explain the Java virtual machine.
A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.
Debugging Introduction to Computing Science and Programming I.
Nov 10, Fall 2006IAT 8001 Debugging. Nov 10, Fall 2006IAT 8002 How do I know my program is broken?  Compiler Errors –easy to fix!  Runtime Exceptions.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS305j Introduction to ComputingNested For Loops 1 Topic 6 Nested for Loops "Complexity has and will maintain a strong fascination for many people. It.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Chapter 1: Introduction To Computer | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 1 Introduction To Computers.
The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
Coding Methodology How to Design Code. © 2005 MIT-Africa Internet Technology Initiative Pay Attention to Detail When implementing or using APIs details.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Jun 16, 2014IAT 2651 Debugging. Dialectical Materialism  Dialectical materialism is a strand of Marxism, synthesizing Hegel's dialectics, which proposes.
1.3 Conditionals and Loops Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · October.
The Java Programming Language
JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
Problem of the Day  Why are manhole covers round?
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Working with arrays (we will use an array of double as example)
CS101 Computer Programming I Chapter 4 Extra Examples.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
CSC Programming I Lecture 6 September 4, 2002.
1 Debugging and Syntax Errors in C++. 2 Debugging – a process of finding and fixing bugs (errors or mistakes) in a computer program.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Decisions, Decisions, Decisions Conditional Statements In Java.
School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging.
ERRORS. Types of errors: Syntax errors Logical errors.
1.3 Conditionals and Loops Copyright 2004, FoxTrot by Bill Amend
The for loop.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Error Handling Tonga Institute of Higher Education.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Computer Science 320 A First Program in Parallel Java.
Jeopardy $100 VariablesErrorsLoops Classes and Objects Program Structure $200 $300 $400 $500 $400 $300 $200 $100 $500 $400 $300 $200 $100 $500 $400 $300.
The if-else statement. Introducing the if-else statement Programming problem: Re-write the a,b,c-formula program to solve for complex number solutions.
CS 112 Introduction to Programming Loop Examples; Variable Scoping; Nested Loops; Yang (Richard) Yang Computer Science Department Yale University 208A.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Last Revision. Question1 Novice Java programmers often write code similar to, class C { public int x;... }... C[] a = new C[10]; for(int i = 0; i < a.length;
Midterm preview.
Testing and Debugging UCT Department of Computer Science Computer Science 1015F Hussein Suleman March 2009.
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Chapter 10 – Exception Handling
Loops.
Testing and Debugging.
Topic 5 for Loops -Arthur Schopenhauer
Repetition-Counter control Loop
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Java Fix a program that has if Online time for Monday’s Program
Topic 17 assertions and program logic
Java Fix a program that has if Online time for Monday’s Program
Chapter 15 Debugging.
Exceptions 19-Feb-19.
Exceptions 7-Apr-19.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Suggested self-checks:
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Chapter 15 Debugging.
CHAPTER 6 Testing and Debugging.
Chapter 15 Debugging.
Presentation transcript:

1 Debugging

2 A Lot of Time is Spent Debugging Programs Debugging. Cyclic process of editing, compiling, and fixing errors. n Always a logical explanation. n What would the machine do? You will make many mistakes as you write programs. It's normal. “ If I had eight hours to chop down a tree, I would spend six hours sharpening an axe. ” — Abraham Lincoln “As soon as we started programming, we found out to our surprise that it wasn't as easy to get programs right as we had thought. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs. ” — Maurice Wilkes

Types of Bugs Syntax errors Mildly annoying The compiler catches these. Example: Semicolon missing at the end of a statement Semantic errors Tricky and moderately/severely annoying Mistake in how your code implements an algorithm Example: Curly-braces missing for a multi-statement while-loop Performance Bugs Very tricky and can be the most annoying Might need to change the underlying algorithm and rewrite large parts of your program 3

Using the Debugger in Dr. Java (DEMO) 4

Code Example of the Three Types of Bugs (Self-Study) 5

6 Debugging Example Factor. Given an integer N > 1, compute its prime factorization. Application. Break RSA cryptosystem (factor 200-digit numbers). 3,757,208 = 2 3  7  13 2  = 2  = 17 11,111,111,111,111,111 = 2,071,723  5,363,222,357

7 Debugging Example Factor. Given an integer N, compute its prime factorization. Brute-force algorithm. For each factor i = 2, 3, 4, …, check if N is a multiple of i, and if so, divide it out /8

8 Debugging Example Programming. A process of finding and fixing mistakes. n Compiler error messages help locate syntax errors. n Run program to find semantic and performance errors. public class Factors { public static void main(String[] args) { long N = Long.parseLong(args[0]) for (i = 0; i < N; i++) { while (N % i == 0) System.out.print(i + " ") N = N / i } this program has many bugs! as long as i is a factor, divide it out check if i is a factor

9 public class Factors { public static void main(String[] args) { long N = Long.parseLong(args[0]); for (int i = 0; i < N; i++) { while (N % i == 0) System.out.print(i + " "); N = N / i; } Debugging: Syntax Errors Syntax error. Illegal Java program. n Compiler error messages help locate problem. Goal: no errors and a file named Factors.class. syntax (compile-time) errors need to declare variable i need terminating semicolons

10 public class Factors { public static void main(String[] args) { long N = Long.parseLong(args[0]); for (int i = 0; i < N; i++) { while (N % i == 0) System.out.print(i + " "); N = N / i; } Debugging: Semantic Errors Semantic error. Legal but wrong Java program. n Run program to identify problem. n Add print statements if needed to produce trace. % javac Factors.java % java Factors Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Factors.main(Factors.java:5) oops, no argument

11 public class Factors { public static void main(String[] args) { long N = Long.parseLong(args[0]); for (int i = 0; i < N; i++) { while (N % i == 0) System.out.print(i + " "); N = N / i; } Debugging: Semantic Errors Semantic error. Legal but wrong Java program. n Run program to identify problem. n Add print statements if needed to produce trace. % javac Factors.java % java Factors 98 Exception in thread "main" java.lang.ArithmeticExeption: / by zero at Factors.main(Factors.java:8) need to start at 2 because 0 and 1 cannot be factors

12 public class Factors { public static void main(String[] args) { long N = Long.parseLong(args[0]); for (int i = 2; i < N; i++) { while (N % i == 0) System.out.print(i + " "); N = N / i; } Debugging: Semantic Errors Semantic error. Legal but wrong Java program. n Run program to identify problem. n Add print statements if needed to produce trace. % javac Factors.java % java Factors … infinite loop! indents do not imply braces

13 public class Factors { public static void main(String[] args) { long N = Long.parseLong(args[0]); for (int i = 2; i < N; i++) { while (N % i == 0) { System.out.print(i + " "); N = N / i; } Debugging: The Beat Goes On Success. Program factors 98 = 2  7 2. n But that doesn't mean it works for all inputs. n Add trace to find and fix (minor) problems. % java Factors % % java Factors 5 % java Factors 6 2 % need newline ??? no output ??? missing the 3

14 public class Factors { public static void main(String[] args) { long N = Long.parseLong(args[0]); for (int i = 2; i < N; i++) { while (N % i == 0) { System.out.println(i + " "); N = N / i; } System.out.println("TRACE: " + i + " " + N); } Debugging: The Beat Goes On Success. Program factors 98 = 2  7 2. n But that doesn't mean it works for all inputs. n Add trace to find and fix (minor) problems. % java Factors 5 TRACE 2 5 TRACE 3 5 TRACE 4 5 % java Factors 6 2 TRACE 2 3 Aha! Print out N after for loop (if it is not 1)

15 public class Factors { public static void main(String[] args) { long N = Long.parseLong(args[0]); for (int i = 2; i < N; i++) { while (N % i == 0) { System.out.print(i + " "); N = N / i; } if (N > 1) System.out.println(N); else System.out.println(); } Success. Program seems to work. Debugging: Success? "corner case" % java Factors 5 5 % java Factors % java Factors % java Factors

16 Performance error. Correct program, but too slow. public class Factors { public static void main(String[] args) { long N = Long.parseLong(args[0]); for (int i = 2; i < N; i++) { while (N % i == 0) { System.out.print(i + " "); N = N / i; } if (N > 1) System.out.println(N); else System.out.println(); } Debugging: Performance Error % java Factors % java Factors % java Factors % java Factors very long wait (with a surprise ending)

17 public class Factors { public static void main(String[] args) { long N = Long.parseLong(args[0]); for (int i = 2; i <= N/i; i++) { while (N % i == 0) { System.out.print(i + " "); N = N / i; } if (N > 1) System.out.println(N); else System.out.println(); } Performance error. Correct program, but too slow. Solution. Improve or change underlying algorithm. Debugging: Performance Error % java Factors % java Factors % java Factors % java Factors fixes performance error: if N has a factor, it has one less than or equal to its square root

18 Debugging Programming. A process of finding and fixing mistakes. 1. Create the program. 2. Compile it. Compiler says: That’s not a legal program. Back to step 1 to fix syntax errors. 3. Execute it. Result is bizarrely (or subtly) wrong. Back to step 1 to fix semantic errors. 4. Enjoy the satisfaction of a working program! 5. Too slow? Back to step 1 to try a different algorithm.