Testing and debugging A short interlude 17-Apr-19.

Slides:



Advertisements
Similar presentations
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Advertisements

23-May-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
Polymorphism. Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[]) { double.
15-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
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.
19-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
19-Aug-15 JUnit tests for output. Capturing output System.out.print and System.out.println usually “print” to the screen, but you can change that OutputStream.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
CSC 204 Programming I Loop I The while statement.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
CSE 143 Lecture 23 Polymorphism; the Object class read slides created by Marty Stepp and Ethan Apter
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.
Overriding toString()
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Week 14 - Monday.  What did we talk about last time?  Inheritance.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Catie Welsh April 18,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
Exceptions throw new RuntimeException(“bad things happened”); The above line is the simplest possible way of throwing an exception. In Java, exceptions.
Searching.
Streams & File Input/Output (I/O)
Lecture 12 Inheritance.
USING ECLIPSE TO CREATE HELLO WORLD
Phil Tayco Slide version 1.1 Created Oct 30, 2017
Computer Science 209 Testing With JUnit.
Programming Language Concepts (CIS 635)
I/O Basics.
Exceptions 10-Nov-18.
Polymorphism 11-Nov-18.
Handling Exceptions.
Polymorphism 15-Nov-18.
CSE 143 Lecture 22 The Object class; Polymorphism read
Polymorphism and access control
Polymorphism 28-Nov-18.
Testing and debugging A short interlude 2-Dec-18.
CSE 143 Lecture 22 The Object class; Polymorphism read
Effective Programming
Building Java Programs
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 214 – Computer Science I More on Linked Lists
Effective Programming
Barb Ericson Georgia Institute of Technology Oct 2005
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Polymorphism 15-Apr-19.
Searching.
Polymorphism 21-Apr-19.
Special instance methods: toString
Chapter 11 Inheritance and Polymorphism Part 2
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
CSE 143 Lecture 23 Polymorphism; the Object class read
Chapter 11 Inheritance and Polymorphism Part 1
slides created by Ethan Apter
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CMPE212 – Reminders Assignment 2 due next Friday.
Review for Midterm 3.
CS 240 – Advanced Programming Concepts
Presentation transcript:

Testing and debugging A short interlude 17-Apr-19

Print statements An old-fashioned, but still very useful, debugging technique is putting in print statements However, it’s a nuisance to remove them again (then add them again, then remove them again...) Here’s a helpful technique: boolean debugging = true; debug("Some debugging message..."); private void debug(Object obj) { if (debugging) System.out.println(obj); }

Debugging output methods, I JUnit testing should not produce any output that you have to look at--it should be fully automatic First, consider whether you can separate the output method into one method that computes the output, and another method that actually prints it Then you can test the former method Since a String can contain newlines, you are not restricted to single-line output Separation of concerns is good practice in any case

Debugging output methods, II You can also change (maybe temporarily) where the output goes PrintStream usualSystemOut = System.out; // save old stream System.setOut(someOtherPrintStream); // use new stream System.setOut(usualPrintStream); // change it back You can write to a file, then test whether the file has the right contents Here's a more sophisticated approach: Use a ByteArrayOutputStream to capture the output in a byte array Use the ByteArray's toString() method to examine the output Complete sample code is on the next slide

Program to capture output public static void main(String[] args) { PrintStream originalOut = System.out; OutputStream os = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(os); System.setOut(ps); System.out.print("Hello, output!"); System.setOut(originalOut); System.out.println("I got: [" + os.toString() + "]"); } I got: [Hello, output!]

toString() In general, you want to avoid unnecessary methods in your classes, but... ...It’s usually a good idea to override toString() Having a toString() method often simplifies debugging, because you can see what your objects really are Don’t use your toString() methods to compare objects in your JUnit tests, unless you are very sure you will never change the string representation of your objects For JUnit testing (especially with assertEquals), you should have a good version of public boolean equals(Object o) The default equals method tests identity, not equality, and this is probably not what you want

Review: Overriding methods To override a method is to write a method that has the exact same signature as an inherited method The names of parameters are not part of the signature, so they can be different if you really want You cannot override a method with a less public method (but it can be more public if you want) You cannot throw any checked exceptions that the inherited method doesn’t throw In Java 5, you should precede your method with @Override Examples: public String toString() { ... } public boolean equals(Object o) { ... } // Notice type of parameter!

The End