1 Royal Flush is better than Full House CS1110 24 March 2009 Testing/Debugging Read chapter 14, pp. 385–401.

Slides:



Advertisements
Similar presentations
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
1 Various Methods of Populating Arrays Randomly generated integers.
Chapter 10 Introduction to Arrays
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.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
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.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
CSC – Java Programming II Lecture 9 January 30, 2002.
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.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
A First Book of ANSI C Fourth Edition
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
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.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
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.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Java SE 8 for Programmers, Third Edition
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
CS 112 Department of Computer Science George Mason University CS 112 Department of Computer Science George Mason University Final Review Lecture 14.
VB and C# Programming Basics. Overview Basic operations String processing Date processing Control structures Functions and subroutines.
Chapter 2: Java Fundamentals
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
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.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 18 – Student Grades Application Introducing.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
1 CS November Testing/Debugging. And Applications Read chapter 14, pp. 385–401 Prelim 2 Max 100 Median 82 Mean 78.3 Min * ***************************
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
1 CS2111 is no longer required for affiliation with Computer Science How to install newest Java Royal Flush is better than Full.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Recitation 3 2D Arrays, Exceptions. 2D arrays 2D Arrays Many applications have multidimensional structures: ●Matrix operations ●Collection of lists ●Board.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
1 CS Nov 2011 Ragged arrays Reading for today: sec Reading for next time: Interfaces: Sec and corresponding ProgramLive material. Also,
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.
1 For more info: CS100J 08 February 2005 In 1968, the Defense Department hired Bolt Beranek and Newman (BBN) of Boston.
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.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Introduction to programming in java Lecture 21 Arrays – Part 1.
JAVA GENERICS Lecture 16 CS2110 – Spring 2016 Photo credit: Andrew Kennedy.
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.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Testing and Debugging.
Creating and Modifying Text part 2
CIS 110: Introduction to Computer Programming
CS100J 10 April 2005 Rectangular arrays and ragged arrays. Secs. 9
For more info: CS100J 13 September 2005 In 1968, the Defense Department hired Bolt Beranek and Newman (BBN) of Boston.
Defensive Programming
Presentation transcript:

1 Royal Flush is better than Full House CS March 2009 Testing/Debugging Read chapter 14, pp. 385–401

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

A 2-dimensional array b P00 P01 P02 P03 P10 P11 P12 P13 P20 P21 P22 P23 Same array in row-major order (rmo) c P00 P01 P02 P03 P10 P11 P12 P13 P20 P21 P22 P23 You can see that b[i][j] is same as c[i * (no of columns) + j]

4 Manipulating jpg files ImageGUI Subclass of JFrame. Has buttons, panels, etc. Has methods that are called when a button is clicked. Contains ImagePanel variables for original and current images ImagePanel JPanel: contains an image. Its method paint draws the image. RmoaImage Contains an array of pixels of an image, in row-major order. Has methods for getting, setting a pixel of the image, ImageProcessor Contains RmoaImage variables for the original and current image. Has the methods for manipulating an image

5 Hiding a character in a pixel k is represented by 107 Hide k in this pixel: R: 254 G: 119 B: 034 R: 251 G: 110 B: 037

6 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.

7 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

8 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 ;

9 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

10 Debugging a program public static HSV RGB2HSV(Color rgb) { … /**Figure out MAX and MIN* 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.

11 Debugging a program public static HSV RGB2HSV(Color rgb) { … /**Figure out MAX and MIN* 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.

12 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 >

13 … 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 some println statements.