TESTING AND DEBUGGING

Similar documents
More on variables, arrays, debugging

Program Development. Program Development. A Foundation for Programming. Program Development

I/O (cont) and Program Development. Standard Audio. A Foundation for Programming. Crash Course in Sound

1.3 Conditionals and Loops

Lecture 3: Loops. While Loops. While Loops: Powers of Two. While Loops: Newton-Raphson Method

A Foundation for Programming

1.3 Conditionals and Loops

Lecture 3: Loops. While Loops. While Loops: Newton-Raphson Method. While Loops: Powers of Two

2.3 Flow Control. Flow-Of-Control. If-Else: Leap Year. If-Else

! Sequence of statements that are actually executed in a program. ! Conditionals and loops: enable us to choreograph control flow.

3. Conditionals & Loops

3. Conditionals and loops

3. Conditionals & Loops

1.3 Conditionals and Loops. 1.3 Conditionals and Loops. Conditionals and Loops

1.3 Conditionals and Loops

You have seen abstractions in many places, lets consider them from the ground up.

Debugging [continued]

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Debugging [continued]

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

Repetition with for loops

A Walkthrough of the Learning Aids

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

PROGRAMMING STYLE. Fundamentals of Computer Science I

AP CS Unit 3: Control Structures Notes

SESSION 3 Programming Languages for Objects

THE JAVA FOR STATEMENT

Conditionals, Loops, and Style

1 Getting used to Python

Jerry Cain Handout #5 CS 106AJ September 30, Using JSKarel

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Boot Camp. Dave Eckhardt Bruce Maggs

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

Object Oriented Programming Exception Handling

CS 177 Recitation. Week 1 Intro to Java

CONDITIONAL EXECUTION

Conditionals, Loops, and Style. Control flow thus far. if statement. Control flow. Common branching statement Evaluate a boolean expression

Computer Science is...

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

CIS March 1, 2018

Laboratory 1: Eclipse and Karel the Robot

COMP-202 Unit 4: Programming with Iterations

Controls Structure for Repetition

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

CIS 110: Introduction to Computer Programming

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Visual Programming. Lecture 3: Loops, Arrays. Mahmoud El-Gayyar

The keyword list thus far: The Random class. Generating "Random" Numbers. Topic 16

Using Karel with Eclipse

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Topic 16. battle -they are strictly limited in number, they require fresh horses, and must only be made at decisive moments." -Alfred North Whitehead

Programming with Java

Primitive data, expressions, and variables

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

COMP 202 Java in one week

CS 101 Fall 2005 Midterm 2 Name: ID:

Java Bytecode (binary file)

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

CS1150 Principles of Computer Science Loops (Part II)

Loops. CSE 114, Computer Science 1 Stony Brook University

17 Hello world 18 Type: String: literal 19 Standard API: System: out.println() 20 Hello world 21 Statement 22 Statement: simple statements are ended w

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved. Using a Debugger WE5.

List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs ar

Java Review. Fundamentals of Computer Science

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

1007 Imperative Programming Part II

COMPUTER PROGRAMMING LOOPS

II. Compiling and launching from Command-Line, IDE A simple JAVA program

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

Math Modeling in Java: An S-I Compartment Model

Expressions & Flow Control

Chapter 7. Iteration. 7.1 Multiple assignment

Assignment Definition And General Feedback By Michael Panitz at Cascadia Community College (

The compiler is spewing error messages.

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

class objects instances Fields Constructors Methods static

Chapter 15 Debugging

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

Some Sample AP Computer Science A Questions - Solutions

Topic 6 Nested for Loops

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Testing and Debugging

GB Programming Challenges

Recap: Assignment as an Operator CS 112 Introduction to Programming

COMP 110 Project 1 Programming Project Warm-Up Exercise

Introduction to the Java Basics: Control Flow Statements

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Programming in the Small II: Control

Transcription:

TESTING AND DEBUGGING zombie[1] zombie[3] Buuuuugs zombie[4] zombie[2] zombie[5] zombie[0] Fundamentals of Computer Science I

Outline Debugging Types of Errors Syntax Errors Semantic Errors Logic Errors Preventing Bugs Have a plan before coding, use good style Learn to trace execution On paper, with print statements, using the debugger Explain it to a teddy bear Incremental development

Debugging Majority of program development time: Finding and fixing mistakes! a.k.a. bugs It's not just you: bugs happen to all programmers 3

Debugging Computers can help find bugs But: computer can't automatically find all bugs! Computers do exactly what you ask Not necessarily what you want There is always a logical explanation! Make sure you saved & compiled last change 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. Debugging had to be discovered. 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 There has never been an unexpectedly short debugging period in the history of computers. -Steven Levy 4

Preventing Bugs Have a plan Write out steps in English before you code Write comments first particularly before tricky bits Use good coding style Good variable names "Name variables as if your first born child" If variable is called area it should hold an area! Split complicated stuff into manageable steps () s are free, force order of operations you want Carefully consider loop bounds Listen to Eclipse (IDE) feedback 5

Finding Bugs How to find bugs Add debug print statements Print out state of variables, loop values, etc. Remove before submitting Use debugger in your IDE Talk through program line-by-line Explain it to a: Programming novice Rubber duckie Teddy bear Potted plant 6

Debugging Example Problem: For integer N > 1, compute its prime factorization 98 = 2 x 7 2 17 = 17 154 = 2 x 7 x 11 16,562 = 2 x 7 2 x 13 2 3,757,208 = 2 3 x 7 13 2 x 397 11,111,111,111,111,111 = 2,071,723 x 5,363,222,357 Possible application: Break RSA encryption Factor 200-digit numbers Used to secure Internet commerce 7

A Simple Algorithm Problem: For integer N > 1, compute its prime factorization Algorithm: Starting with i=2 Repeatedly divide N by i as long as it evenly divides, output i every time it divides Increment i Repeat 8

i N Output 2 16562 2 3 8281 4 8281 5 8281 6 8281 7 8281 7 7 8 169 9 169 10 169 11 169 12 169 13 169 13 13 14 1 1 Example Run 9

Buggy Factorization Program 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! 10

Debugging: Syntax Errors Syntax errors Illegal Java program Usually easily found and fixed 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; 11

Debugging: Semantic Errors Semantic error 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; % java Factors 98 Exception in thread "main" Legal but wrong Java program Run program to identify problem java.lang.arithmeticexception: / by zero at Factors.main(Factors.java:8) 12 Need to start at 2 since 0 and 1 cannot be factors.

Debugging: Semantic Errors 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; % java Factors 98 Indentation implies a block of code, but without braces, it s not. 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2... 13

Debugging: Even More Problems 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; % java Factors 98 2 7 7 % % java Factors 5 Need newline No output??? % java Factors 6 2 % Missing the 3??? 14

Debugging: Adding Trace Print Statement 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); % java Factors 5 TRACE 2 5 TRACE 3 5 TRACE 4 5 % java Factors 6 2 TRACE 2 3 i for-loop should go up to n! 15

Success? 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; System.out.println(); Fixes the lack of line feed problem. % java Factors 5 5 % java Factors 6 2 3 % java Factors 98 2 7 7 Fixes the "off-byone" error in the loop bounds. % java Factors 3757208 2 2 2 7 13 13 397 16

Except for Really Big Numbers... 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; System.out.println(); % java Factors 11111111 11 73 101 137 % java Factors 11111111111 21649 51329 % java Factors 11111111111111 11 239 4649 909091 % java Factors 11111111111111111 2071723-1 -1-1 -1-1 -1-1... 17

Correct, But Too Slow public class Factors public static void main(string [] args) long n = Long.parseLong(args[0]); for (long i = 2; i <= n; i++) while (n % i == 0) System.out.print(i + " "); n = n / i; System.out.println(); % java Factors 11111111 11 73 101 137 % java Factors 11111111111 21649 51329 % java Factors 11111111111111 11 239 4649 909091 % java Factors 11111111111111111 2071723 5363222357 18

Faster Version public class Factors public static void main(string [] args) long n = Long.parseLong(args[0]); for (long i = 2; i*i <= n; i++) while (n % i == 0) System.out.print(i + " "); n = n / i; System.out.println(); % java Factors 98 2 7 7 % java Factors 11111111 11 73 101 Missing last factor % java Factors 11111111111 21649 % java Factors 11111111111111 11 239 4649 % java Factors 11111111111111111 2071723 19

Fixed Faster Version public class Factors public static void main(string [] args) long n = Long.parseLong(args[0]); for (long i = 2; i*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(); Handles the "corner case" % java Factors 98 2 7 7 % java Factors 11111111 11 73 101 137 Need special case to print biggest factor (unless it occurs more than once) % java Factors 11111111111 21649 513239 % java Factors 11111111111111 11 239 4649 909091 % java Factors 11111111111111111 2071723 5363222357 20

Factors: Analysis How large an integer can I factor? % java Factors 3757208 2 2 2 7 13 13 397 % java Factors 9201111169755555703 9201111169755555703 digits (i <= n) (i*i <= n) 3 instant instant 6 0.15 seconds instant 9 77 seconds instant 12 21 hours * 0.16 seconds 15 2.4 years * 2.7 seconds 18 2.4 millennia * 92 seconds * estimated 21

Incremental Development Split development into stages: Test thoroughly after each stage Don't move on until it's working! Bugs are (more) isolated to the part you've just been working on Prevents confusion caused by simultaneous bugs in several parts 22

Summary Debugging Types of Errors Syntax Errors Semantic Errors Logic Errors Preventing Bugs Have a plan before coding, use good style Learn to trace execution On paper, with print statements, using the debugger Explain it to a teddy bear Incremental development Test, Test, Test!!