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

Similar documents
Midterm Examination (MTA)

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Methods

2.2 - Making Decisions

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

ICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Arrays

Chapter 5 Lab Methods

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

Chapter 3 Selections. 3.1 Introduction. 3.2 boolean Data Type

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. File Input and Output

1. A company has vans to transport goods from factory to various shops. These vans fall into two categories: 50 items 150 items

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

while (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */

Lab Exercise 1. Objectives: Part 1. Introduction

Decisions: Logic Java Programming 2 Lesson 7

CEN 414 Java Programming

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Claremont McKenna College Computer Science

Lab Exercise 1 Using EGit and JUnit

Debugging. CSE 2231 Supplement A Annatala Wolf

CMSC 202. Exceptions

Lab 8 - Vectors, and Debugging. Directions

Java Programming Constructs Java Programming 2 Lesson 1

Conditional Execution

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

Chapter 5 Lab Methods

Chapter 2 Elementary Programming

Chapter 4: Control Structures I

Lecture 8 " INPUT " Instructor: Craig Duckett

Chapter 1 Lab Algorithms, Errors, and Testing

Warm up Exercise. What are the types and values of the following expressions: * (3 + 1) 3 / / 2.0 (int)1.0 / 2

Last Class. While loops Infinite loops Loop counters Iterations

RANDOM NUMBER GAME PROJECT

Building Java Programs

1.00 Lecture 2. What s an IDE?

Example Program. public class ComputeArea {

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Chapter 2. Elementary Programming

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

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Topic 11 Scanner object, conditional execution

Object Oriented Programming. Java-Lecture 6 - Arrays

SESSION 3 Programming Languages for Objects

Exceptions Handeling

EXCEPTION HANDLING. // code that may throw an exception } catch (ExceptionType parametername) {

BlueJ Demo. Topic 1: Basic Java. 1. Sequencing. Features of Structured Programming Languages

Motivations. Chapter 3: Selections and Conditionals. Relational Operators 8/31/18. Objectives. Problem: A Simple Math Learning Tool

Chapter 3. Selections

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Assignment 2.4: Loops

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Oct Decision Structures cont d

Garfield AP CS. User Input, If/Else. Most slides from Building Java Programs. Thanks, Stuart Regesand Marty Stepp!

Elementary Programming

Full file at

Topic 11 Scanner object, conditional execution

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

What did we talk about last time? Examples switch statements

CS110 Programming Language I. Lab 6: Multiple branching Mechanisms

Building Java Programs

Loops. CSE 114, Computer Science 1 Stony Brook University

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

TESTING AND DEBUGGING

Supplement: Case Study: Sudoku. For Introduction to Java Programming By Y. Daniel Liang

H212 Introduction to Software Systems Honors

CS 112 Introduction to Programming

Building Java Programs

Date: Dr. Essam Halim

Introduction to Computation and Problem Solving

CS 112 Introduction to Programming

COMP 110/401 APPENDIX: INSTALLING AND USING ECLIPSE. Instructor: Prasun Dewan (FB 150,

Building Java Programs

In this lab we will practice creating, throwing and handling exceptions.

Building Java Programs

Web-CAT submission URL: CAT.woa/wa/assignments/eclipse

Flow of Control: Loops. Chapter 4

CSIS 10A Assignment 4 SOLUTIONS

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

Java Errors and Exceptions. Because Murphy s Law never fails

CS 112 Introduction to Programming

CS 112 Introduction to Programming

Lesson 7 Part 2 Flags

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG

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

Building Java Programs

Building Java Programs

Building Java Programs

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

9. Java Errors and Exceptions

There are several files including the start of a unit test and the method stubs in MindNumber.java. Here is a preview of what you will do:

COMP102: 181. Menu. More while loops. Admin: Test. Peter Andreae

What two elements are usually present for calculating a total of a series of numbers?

Software and Programming 1

Programming Exercise 7: Static Methods

Topics. The Development Process

Following is the general form of a typical decision making structure found in most of the programming languages:

Transcription:

WIT COMP1000 Testing and Debugging

Testing Programs When testing your code, always test a variety of input values Never test only one or two values because those samples may not catch some errors Always test "interesting" values» Values that show up in the code» For example, boundary values that change if/else behavior WIT COMP1000 2

"Interesting" Values HTTP://XKCD.COM/376/ WIT COMP1000 3

Also: Be Nice to Your Computer CRASH EVERYT HING HTTP://XKCD.COM/371/ WIT COMP1000 4

Also Also: Be Diligent! Computer! HTTP://XKCD.COM/242/ WIT COMP1000 5

One Last Warning HTTP://XKCD.COM/292/ WIT COMP1000 6

Example Bug: Microsoft, 12/31/2008 Early this morning we were alerted by our customers that there was a widespread issue affecting our 2006 model Zune 30GB devices (a large number of which are still actively being used). The technical team jumped on the problem immediately and isolated the issue: a bug in the internal clock driver related to the way the device handles a leap year. That being the case, the issue should be resolved over the next 24 hours as the time change moves to January 1, 2009. We expect the internal clock on the Zune 30GB devices will automatically reset tomorrow (noon, GMT). By tomorrow you should allow the battery to fully run out of power before the unit can restart successfully then simply ensure that your device is recharged, then turn it back on. If you re a Zune Pass subscriber, you may need to sync your device with your PC to refresh the rights to the subscription content you have downloaded to your device. WIT COMP1000 7

Zune Bug Excerpt // days is the number of days since 1980, e.g., 10000 or 365 or 10592 // year is the current year if (IsLeapYear(year)) { if (days > 366) { days -= 366; year += 1; 366 and 367 seem like good values to test WIT COMP1000 8

General Advice Always check for common errors» Using = instead of ==» Using > or < instead of <= or >=» Other off-by-one errors» Integer division Always test your code» Use lots of values to test, including ones that change the behavior of the code WIT COMP1000 9

Localize Errors When you don't get the output you expect, DO NOT just change code randomly Narrow down where the problem is by checking values throughout the program» Use output statements at key points to check current variable values» Also use output statements to verify which branch of ifelse statements are taken WIT COMP1000 10

Example (with Errors) import java.util.scanner; public class ClassExamples { public static void main(string[] args) { Scanner input = new Scanner(System.in); double radius, height, volume; System.out.print("Enter the radius of the cone: "); radius = input.nextdouble(); System.out.print("Enter the height of the cone: "); height = input.nextdouble(); volume = 1 / 3 * Math.PI * radius * radius * height; System.out.println("The volume is: " + volume); WIT COMP1000 11

Error Localized Scanner input = new Scanner(System.in); double radius, height, volume; System.out.print("Enter the radius of the cone: "); radius = input.nextdouble(); System.out.print("Enter the height of the cone: "); height = input.nextdouble(); System.out.println("radius is " + radius); System.out.println("height is " + height); //volume = 1 / 3 * Math.PI * radius * radius * height; //volume = 1 / 3 * Math.PI; volume = 1 / 3; System.out.println("The volume is: " + volume); Integer division! WIT COMP1000 12

Simple Testing and Debugging When your code doesn't produce the correct output, don't just randomly change things Localize the error by using output statements to track variable values, branch conditions, and other interesting areas of your code Always check for common mistakes (look very hard, because they are easy to overlook) WIT COMP1000 13

Debugging Exercise public static void main(string[] args) { Scanner input = new Scanner(System.in); int answer = 3; int guess; System.out.print("Enter a guess between 1 and 10: "); guess = input.nextint(); if ((guess <= 1) (guess >= 10)) { System.out.println("Invalid guess!"); System.exit(0); if (guess == answer) { System.out.println("Wrong! Try again."); else { System.out.println("You got it!"); WIT COMP1000 14

Answer public static void main(string[] args) { Scanner input = new Scanner(System.in); int answer = 3; int guess; System.out.print("Enter a guess between 1 and 10: "); guess = input.nextint(); if ((guess < 1) (guess > 10)) { // used <= and >= instead of < and > System.out.println("Invalid guess!"); System.exit(0); if (guess == answer) { System.out.println("You got it!"); // swapped two output messages else { System.out.println("Wrong! Try again."); WIT COMP1000 15

WIT COMP1000 16 JUnit Testing Another widely used type of testing is known as unit testing The idea is to write additional code, called unit tests, that will automatically test your program with certain inputs to ensure that they produce the correct output» Of course, you must verify that your unit tests are correct! JUnit is a very commonly used (and open source!) unit testing framework for Java» The tests you've been in running in Eclipse all semester!» Take some time to look at the test code to see that it's just more Java

Debugging Tools Debuggers help you to quickly find and identify errors in your code Allow you to:» Step through your code one line at a time» View current values of all variables as the program progresses» Set breakpoints that will stop the code at certain places in your code Should NOT be used in place of proper testing and analysis, but rather as an assistive tool WIT COMP1000 17

Eclipse Debugger Eclipse has a built in debugger that has all the normal debugging functionality Set breakpoints by double clicking on the left of a line of code in the light blue vertical bar Start the program with debugging (F11, the bug button, or go to the Run menu and click Debug) The program will execute like normal until you come to a breakpoint When a breakpoint is encountered, the program will stop and Eclipse will ask you to switch to the Debug perspective (which you should do) WIT COMP1000 18

Eclipse Debug Perspective Once debugging, you can see the value of all variables in the upper right window (make sure you are looking at the Variables tab) Use the Play/Resume (F8) and Step Over (F6) buttons to navigate through the code» Play/Resume starts the program running again, and it will continue until another breakpoint is encountered or the program ends» Step Over executes only the next line of code and then stops There are two other options you'll use in the future» Step Into (F5) executes the next line of code, and goes into a method if that line is a method call» Step Return (F7) starts the program running again, and it continues until the current method call finishes, then stops again WIT COMP1000 19

Debugging Example public static void main(string[] args) { Scanner input = new Scanner(System.in); int answer = 3; int guess; System.out.print("Enter a guess between 1 and 10: "); guess = input.nextint(); if ((guess <= 1) (guess >= 10)) { System.out.println("Invalid guess!"); System.exit(0); if (guess == answer) { System.out.println("Wrong! Try again."); else { System.out.println("You got it!"); WIT COMP1000 20

Steps Start by putting a breakpoint at the first if statement» Double click in the blue bar on the left of the line» It should add a blue dot to indicate that there is a breakpoint there now» You can double click on the dot to remove the breakpoint Hit F11 to start debugging, and enter a value of 1 in the console Eclipse will then stop automatically at the if statement» Make sure to switch do the Debug perspective Examine the guess variable and see that it is 1 Click the Step Over (F6) button to execute the if statement WIT COMP1000 21

Steps The program jumps into the if code block» This code should only be used when the guess was invalid, but 1 is a valid guess!» We now know to look very hard at the if condition because it must be wrong» In this case (as it often is), the problem is an off by one error cause by using <= instead of < Fix the if statement, then stop debugging and start the program again with debugging Next, set breakpoints at the two output statements that tell the user if their guess was correct or not» Examine the values of guess and answer» You'll see that the wrong output is printed when they are the same! WIT COMP1000 22

Debugging Exercise public static void main(string[] args) { Scanner input = new Scanner(System.in); double length, width, area; System.out.print("Enter positive rectangle length: "); length = input.nextdouble(); System.out.print("Enter positive rectangle width: "); width = input.nextdouble(); if ((length < 0) && (width < 0)) { System.out.println("Invalid measurements!"); System.exit(0); area = length * width; System.out.println("The area is: " + area); WIT COMP1000 23

Steps Run the program and find the following bugs:» The program does not print an error message if either measurement is zero» The program does not print an error message if only one measurement is negative There must be something wrong around the if statement Set a breakpoint on the if statement Use the Step Over button to execute one statement at a time WIT COMP1000 24

Steps You'll see that it skips the error message on several occasions when they should be executed» Verify this by looking at the values of length and width in the variables section of the debugger That means that the error must be in the if condition Try running with interesting boundary conditions such as -1, 0, and 1» As you test, pay attention to the conditions and change it when you note buggy behavior In particular, both conditions should be <= instead of < and it should be instead of && WIT COMP1000 25

Take Home Points Testing your code is vital, and it takes time to learn how to do it well Use simple testing and debugging techniques such as adding output statements throughout your code» Of course, be sure to remove them once you have fixed any problems! Use debuggers, like the Eclipse debugger, to trace through code in order to find errors» Set breakpoints near lines you want to check to skip ahead to those areas of the code WIT COMP1000 26