1 CS1110 11 November Testing/Debugging. And Applications Read chapter 14, pp. 385–401 Prelim 2 Max 100 Median 82 Mean 78.3 Min 20 100 1 * 90-99 53 ***************************

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

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.
FIT FIT1002 Computer Programming Unit 19 Testing and Debugging.
File I/O and Exceptions File I/O Exceptions Throwing Exceptions Try statement and catch / finally clauses Checked and unchecked exceptions Throws clause.
1 Fall 2009ACS-1903 The break And continue Statements a break statement can be used to abnormally terminate a loop. use of the break statement in loops.
Comp 114 Foundations of Programming Instructor: Prasun Dewan.
1 CS100J 12 April 2007 Applications and Applets Read Chapter 16 of the text I never let my schooling interfere with my education. Mark Twain Learning makes.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Files and Streams CS 21a Chapter 11 of Horstmann.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
1 Fall 2007ACS-1903 for Loop Writing to a file String conversions Random class.
1 Fall 2008ACS-1903 for Loop Reading files String conversions Random class.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014
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.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 1 Chapter 8 Testing and Debugging.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
1 CS100J 2 December 2008 Applications and Applets Read Chapter 16 of the text We also look at html, since we need it to use applets. Top finalists from.
1 Intro to Java Week 12 (Slides courtesy of Charatan & Kans, chapter 8)
1 Recitation 09. Applications and Applets Java applications Those who took CS100J and CS100M in fall 2003 may not have had much experience with Java applications.
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.
1 CS Nov 2011 Applications and Applets Read Chapter 16 of the text We also look at html, since we need it to use applets. Top finalists from a real-life.
Cs2220: Engineering Software Class 6: Defensive Programming Fall 2010 University of Virginia David Evans.
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.
JAVA Practical Creating our first program 2. Source code file 3. Class file 4. Understanding the different parts of our program 5. Escape characters.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
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.
Computer Science 209 Software Development Handing Errors and Creating Documentation.
1 CS2111 is no longer required for affiliation with Computer Science How to install newest Java Royal Flush is better than Full.
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
CompSci 100E 18.1 Testing and Debugging Robert A Wagner.
1 CS100J 08 November 2005: Applications and Applets Java application; How to produce a stand-alone application, to be run without using DrJava or any such.
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
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 Royal Flush is better than Full House CS March 2009 Testing/Debugging Read chapter 14, pp. 385–401.
Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging using eclipse The Java API.
Programming for Interactivity Professor Bill Tomlinson Tuesday & Wednesday 6:00-7:50pm Fall 2005.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
Arguments to method main, Packages, Wrapper Classes, Characters, Strings CS2110, Recitation 2.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Recursion (Continued)
Appendix A Barb Ericson Georgia Institute of Technology May 2006
We also look at html, since we need it to use applets.
Logger, Assert and Invariants
CS1101X Programming Methodology
Testing and Debugging.
We also look at html, since we need it to use applets.
Creating and Modifying Text part 2
Important terms Black-box testing White-box testing Regression testing
Testing UW CSE 160 Spring 2018.
Important terms Black-box testing White-box testing Regression testing
Testing Recap Testing can find problems, but cannot prove your program works Since exhaustive testing is impossible, select test cases with maximum likelihood.
Java Intro.
CSE 143 Lecture 5 More ArrayIntList:
We also look at html, since we need it to use applets.
CSE 1020:Software Development
Barb Ericson Georgia Institute of Technology Oct 2005
CS100J 03 May 2005 Applications and Applets (Chapter 16 of the text)
Workshop for Programming And Systems Management Teachers
Defensive Programming
Presentation transcript:

1 CS November Testing/Debugging. And Applications Read chapter 14, pp. 385–401 Prelim 2 Max 100 Median 82 Mean 78.3 Min * *************************** *************************************** ************************ *************** ****** ** ** ** TAs can fix obvious mistakes. Real regrade request? Write note explaining the issues, attach to prelim, give to Gries or Lee before Thanksgiving. Check your score against CMS score, let us know if there is a mistake.

Recursion question 2 /** = "there is a path of male best friends from s to e". Precondition: s, e not null; s and e are male; */ public static boolean malePathTo (Person s, Person e) { } malePathTo(new Vector(), s, e);

Recursion question 3 /** = "there is a path of male best friends from s to e that does not contain a Person in list ig". Precondition: s, e, and ig are not null; s and e are male; s and e are not in ig. */ public static boolean malePathTo(Vector ig, Person s, Person e)

Recursion question 4 /** = "there is path of male best friends from s to e that does not contain a Person in list ig". Precond: s, e, and ig are not null; s and e are male; s and e are not in ig. */ public static boolean malePathTo(Vector ig, Person s, Person e) { if (s == e) return true; if (s.getMBF() == null) return false; ig.add(s); if (ig.contains(s.getMBF())) return false; return malePathTo(ig, s.getMBF, e); } base case: path of one node!!!!! make sure s is not looked at again Look at the def of the path above Note that this call satisfies all parts of the precondition of the specification.

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

6 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

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

8 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

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

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

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

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

13 Executing Java programs outside the DrJava Interactions pane The Java application public class C { … public static void main(String[] args) { … } … } A Java application needs a class with a method main that is defined like this. To start the application, method main is called. The parameter, an array of Strings, can be used to pass information into the program.

14 public class CLAS { … public static void main(String[] args) { … } … } > cd > dir (list of files) > java CLAS Terminal window (can type “java CLAS” in DrJava Interactions pane) Causes method call CLAS.main(null); to be executed

15 jar file (Java Archive file) (like tar file (Tape Archive file)) Contains (among other things) (1).class files (2)a “manifest”, which says which class has method main Manifest: A list of passengers or an invoice of cargo for a vehicle (as a ship or plane). Look in index of text for info on jar files and how to build them in the command-line window.

16 Standalone Java programs Suppose images.jar contains a Java application It has a class with a static procedure main, and its manifest names the class. Execute it by double clicking its icon in a directory. Execute it by typing java -jar images.jar in a terminal window (or DOS, or command-line window)

17 Inspecting jar files jar -tf images.jar type (list) File name of jar file List the contents of jar file images.jar

18 Creating a jar file 1.Navigate to the directory that contains the.class files. 2.Create a text file x.mf that contains one line (with a line-feed): Main-class: 3.In the directory, type: jar -cmf x.mf app.jar *.class Create Manifest File name of manifest file name of file to create expands to name all the.class files