1 Royal Flush is better than Full House CS1110 11 March 31 Testing/Debugging. Also, about A6 Read chapter 14, pp. 385–401 Prelim 2 is Thursday eve, 14.

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
FIT FIT1002 Computer Programming Unit 19 Testing and Debugging.
Chapter 10 Introduction to Arrays
1 CS Sept. Customizing a class & testing Quiz 2 on Tuesday 15 Sept Purpose of a constructor (slide 5) Evaluating a new expression (slide 6) Fields;
1 CS100J 13 March 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
1 CS100J 14 March 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 CS100J 25 October 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
DEBUGGERS For CS302 Data Structures Course Slides prepared by TALHA OZ (most of the text is from
1 Functional Testing Motivation Example Basic Methods Timing: 30 minutes.
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Coding Methodology How to Design Code. © 2005 MIT-Africa Internet Technology Initiative Pay Attention to Detail When implementing or using APIs details.
A First Book of ANSI C Fourth Edition
CS1110 lecture 5 14 Sept 2010 Testing; the class Object; toString; static variables & methods Reading for this lecture: Testing with JUnit (Appendix I.2.4.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Debugging in Java. Common Bugs Compilation or syntactical errors are the first that you will encounter and the easiest to debug They are usually the result.
Introduction to Exception Handling and Defensive Programming.
Characters, Strings, Reading Files CS2110, Week 2 Recitation.
CS1110 lecture 5 8 Feb 2010 Testing; class Object; toString; static variables/methods Reading for this lecture: Testing with JUnit (Appendix I.2.4 & pp.
1 CS1110 lecture 4 9 Sept. Customizing a class & testing Classes: fields; getter & setter methods. Secs (p. 45) & 3.1 (pp. 105–110 only) Constructors.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 18 – Student Grades Application Introducing.
1 CS November Testing/Debugging. And Applications Read chapter 14, pp. 385–401 Prelim 2 Max 100 Median 82 Mean 78.3 Min * ***************************
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology Creating Classes part 2 Barb Ericson Georgia Institute of Technology June 2006.
1 CS2111 is no longer required for affiliation with Computer Science How to install newest Java Royal Flush is better than Full.
1 CS Nov 2009 Ragged arrays Reading for today: sec Reading for next time: chapter 16, applications and applets If you have a conflict with.
Recitation 3 2D Arrays, Exceptions. 2D arrays 2D Arrays Many applications have multidimensional structures: ●Matrix operations ●Collection of lists ●Board.
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 15 Lecture 15-2: testing ArrayIntList; pre/post conditions and exceptions reading:
CSC 213 – Large Scale Programming. Today’s Goal  Understand why testing code is important  Result of poor or no testing & embarrassment caused  Learn.
Today’s Topics O-Notation Testing/Debugging Data Structures Next Class: Writing correct programs:Column 4.
1 CS Nov 2011 Ragged arrays Reading for today: sec Reading for next time: Interfaces: Sec and corresponding ProgramLive material. Also,
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
1 Royal Flush is better than Full House CS March 2009 Testing/Debugging Read chapter 14, pp. 385–401.
1 CS April 2010 Ragged arrays Reading for today: sec Reading for next time: chapter 16, applications and applets Prelim 3: mean 69.3, median.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to programming in java Lecture 21 Arrays – Part 1.
JAVA GENERICS Lecture 16 CS2110 – Spring 2016 Photo credit: Andrew Kennedy.
1 CS Oct 2008 Arrays. Reading: Secs 8.1, 8.2, 8.3 Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long, and.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
OOP Tirgul 7. What We’ll Be Seeing Today  Packages  Exceptions  Ex4 2.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
Recursion (Continued)
Testing and Debugging.
Tonga Institute of Higher Education
CSE 143 Lecture 5 More ArrayIntList:
CS November 2010 Developing array algorithms. Reading:
CIS 110: Introduction to Computer Programming
CSE 1020:Software Development
Lecture 14: Testing Testing used to verify object behavior through designed test suites Can test Classes – “unit” testing Object interactions – “integration”
CS100J 10 April 2005 Rectangular arrays and ragged arrays. Secs. 9
Defensive Programming
Presentation transcript:

1 Royal Flush is better than Full House CS March 31 Testing/Debugging. Also, about A6 Read chapter 14, pp. 385–401 Prelim 2 is Thursday eve, 14 April. We will be contacting people about conflicts

2 Type of d is int[][] (“int array array”, “an array of int arrays”) To declare variable d: int d[][]. To create a new array and assign it to d: d= new int[3][4]; To reference element at row r column c: d[r][c] number of rows number of cols Two-dimensional arrays b b.length one-dimensional array d rectangular array: 5 rows and 4 columns

3 Testing: Read chapter 14. Bug: Error in a program. Testing: Process of analyzing, running program, looking for bugs. Test case: A set of input values, together with the expected output. Debugging: Process of finding a bug and removing it. Exceptions: When an error occurs, like divide by 0, or s.charAt[i] when i = – 1, Java throws an exception. A lot —generally too much— information is provided. Two ideas on test cases: 1. Black Box Testing: Develop test cases based on the spec. 2. White Box Testing: Look at the code; develop test cases so that each statement/expression is exercised in at least one test case.

4 Exceptions: When an error occurs, like divide by 0, or s.charAt[i] when i = – 1, Java throws an exception. 06 /** = String s truncated …. */ 07 public static String truncate5(String s) { 08 int b= 10 / 0; 09 if (s.length() <= 5) 10 return s; 11 return s.substring(0,5); 12 } ArithmeticException: / by zero at A4Methods.truncate5(A4Methods.java:8) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(….java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(….java:25) at java.lang.reflect.Method.invoke(Method.java:585) important part Turn on line numbering in DrJava. Preferences / Display Options call stack

5 Debugging a program Strategy 0: Find a simplest possible test case that exhibits the error. When an error occurs, you have to play detective and find it. That process is called debugging. The place where the bug is may be far removed from the place where an error is revealed. Strategy 1: put print statements, suitably annotated, at judiciously chosen places in the program. Strategy 3: Use the debugging feature of your IDE (Interactive Development Environment —yours is DrJava. Strategy 2: Use Java assert-statements at good places: assert ;

6 Assert statement Example: Use it to check preconditions: /** = “This Virus is the predecessor of v”. Precondition: v is not null */ public boolean isPredecessorOf(Virus v) { assert v != null; … } Use it to program “defensively”, and leave it in the program

7 Debugging a program public static HSV RGB2HSV(Color rgb) { … /**Set MAX, MIN to max and min of R, G, B */ double MAX= 0; double MIN= 0; if (R>G && R>B) {MAX= R; } if (G>B && G>R) {MAX= G;} if (B>R && B>G) {MAX= B;} if (R<G && R<B) {MIN= R; } if (G<B && G<R) {MIN= G; } if (B<R && B<G) {MIN= B;} When an error occurs, play detective and find it. Called debugging. The place where the bug is may be far removed from the place where an error is revealed. System.out.println("R " + R + ", G " + G + ", B ” + B + ", MAX " + MAX); If you just output the numbers without naming them, you will have trouble.

8 Debugging a program public static HSV RGB2HSV(Color rgb) { … /**Set MAX, MIN to max and min of R, G, B */ double MAX= 0; double MIN= 0; if (R>G && R>B) {MAX= R; } if (G>B && G>R) {MAX= G;} if (B>R && B>G) {MAX= B;} if (R<G && R<B) {MIN= R; } if (G<B && G<R) {MIN= G; } if (B<R && B<G) {MIN= B;} When an error occurs, play detective and find it. Called debugging. The place where the bug is may be far removed from the place where an error is revealed. assert R <= MAX && G <= MAX && B <= MAX; assert MIN <= R && MIN <= G && MIN <= B; These assert statements don’t check completely that MAX is the max and MIN the min.

9 public static HSV RGB2HSV(Color rgb) { … if (R>G && R>B) {MAX= R; } if (G>B && G>R) {MAX= G;} if (B>R && B>G) {MAX= B;} if (R<G && R<B) {MIN= R; } if (G<B && G<R) {MIN= G; } if (B<R && B<G) {MIN= B;} System.out.println("R " + R + ", G " + G + ", B ” + B + ", MAX " + MAX); > A4Methods.RGB2HSV(new java.awt.Color(255,255,128)) R 1.0, G 1.0, B 0.502, MAX 0.0 Look! MAX is 0 and not 1! call and output if conditions should be >=, not >

10 … if (Hi ==0){ R=(int)(v * 255.0); G=(int)(t * 255.0); B=(int)(p * 255.0); } if (Hi==1){ R=(int)(q * 255.0); G=(int)(v * 255.0); B=(int)(p * 255.0); } … int r= (int)Math.round(R); Error in HSVtoRGB. Not rounding properly System.out.println(“In HSVtoRGB. R is “ + R); System.out.println(“In HSVtoRGB. r is “ + r); Insert println statements.

Steganography: hide a message in an image 11 Message: Yea! P00 P01 P02 P03 P04 P05 P06 P07 P09 P10 P11 P12 P13 Beginning markers Y e A ! End markers (depends on design) (depends on design) The time you spend on procedures hide and reveal will depend partially on your debugging technique. Write both hide and reveal and then see whether they work will waste your time. It is best to write hide and test it thoroughly. THEN write reveal.

Debugging hide (and reveal) 12 In ImageArray: /** = pixel pix, in the form (red, green, blue).... */ public static String toString (int pix) In the past, students have not debugged hide properly. Therefore, we make it easier to debug properly by requiring that you write (and use): In ImageProcessor: /** = a String containing the first n pixels of the current image, 5 to a line, with annotation (i.e. something at the beginning to say what the string contains). */ public String getPixels(int n)