SlideShare a Scribd company logo
1 of 77
Program Correctness
and Efficiency
Following Koffmann and
Wolfgang Chapter 2
Chapter 2: Program Correctness and Efficiency 2
Outline
• Categories of program errors
• Why you should catch exceptions
• The Exception hierarchy
• Checked and unchecked exceptions
• The try-catch-finally sequence
• Throwing an exception:
• What it means
• How to do it
Chapter 2: Program Correctness and Efficiency 3
Outline (continued)
• A variety of testing strategies
• How to write testing methods
• Debugging techniques and debugger
programs
• Program verification: assertions and loop
invariants
• Big-O notation
• What it is
• How to use it to analyze an algorithm’s efficiency
Chapter 2: Program Correctness and Efficiency 4
Program Defects and “Bugs”
• An efficient program is worthless if it breaks or
produces a wrong answer
• Defects often appear in software after it is
delivered
• Testing cannot prove the absence of defects
• It can be difficult to test a software product
completely in the environment in which it is used
• Debugging: removing defects
Chapter 2: Program Correctness and Efficiency 5
Major Categories of Defects
• Syntax and other in-advance errors
• Run-time errors and exceptions
• Logic Errors
Chapter 2: Program Correctness and Efficiency 6
Syntax Errors
• Syntax errors: grammatical mistakes in a program
• The compiler detects syntax errors
• You must correct them to compile successfully
• Some common syntax errors include:
• Omitting or misplacing braces, parentheses, etc.
• Misplaced end-of-comment
• Typographical errors (in names, etc.)
• Misplaced keywords
Chapter 2: Program Correctness and Efficiency 7
Semantic Errors
• Semantic errors: may obey grammar, but violate
other rules of the language
• The compiler detects semantic errors
• You must correct them to compile successfully
• Some common semantic errors include:
• Performing an incorrect operation on a primitive type value
• Invoking an instance method not defined
• Not declaring a variable before using it
• Providing multiple declarations of a variable
• Failure to provide an exception handler
• Failure to import a library routine
Chapter 2: Program Correctness and Efficiency 8
Run-time Errors or Exceptions
• Run-time errors
• Occur during program execution (run-time!)
• Occur when the JVM detects an operation that it
knows to be incorrect
• Cause the JVM to throw an exception
• Examples of run-time errors include
• Division by zero
• Array index out of bounds
• Number format error
• Null pointer exceptions
Chapter 2: Program Correctness and Efficiency 9
Run-time Errors or Exceptions (continued)
Chapter 2: Program Correctness and Efficiency 10
Logic Errors
• A logic error is programmer mistake in
• the design of a class or method, or
• the implementation of an algorithm
• Most logic errors
• Are not syntax or semantic errors: get by the compiler
• Do not cause run-time errors
• Thus they are difficult to find
• Sometimes found through testing
• Sometimes found by users
Chapter 2: Program Correctness and Efficiency 11
Avoiding Logic Errors
• Work from a precise specification
• Strive for clarity and simplicity
• Consider “corner” / extreme cases
• Have reviews / walk-throughs: other eyes
• Use library/published algorithms where possible
• Think through pre/post conditions, invariants
• Be organized and careful in general
Chapter 2: Program Correctness and Efficiency 12
The Exception Class Hierarchy
• When an exception occurs, the first thing that
happens is a new of a Java exception object
• Different exception classes have different rules
• Throwable is the root superclass of the exception
class hierarchy
• Error is a subclass of Throwable
• Exception is a subclass of Throwable
•RuntimeException is a subclass of Exception
Chapter 2: Program Correctness and Efficiency 13
The Class Throwable
• Throwable is the superclass of all exceptions
• All exception classes inherit its methods
Chapter 2: Program Correctness and Efficiency 14
The Class Throwable (continued)
Chapter 2: Program Correctness and Efficiency 15
The Exception Class Hierarchy (2)
Throwable is the superclass of all exception classes
• Error is for things a program should not catch
• Example: OutOfMemoryError
• Exception is for things a program might catch
• RuntimeException is for things the VM might throw
• It can happen anywhere: e.g., any object access can
throw NullPointerException
• So not required to catch it
• All others must be either:
• Explicitly caught or
• Explicitly mentioned as thrown by the method
Chapter 2: Program Correctness and Efficiency 16
Exception Hierarchy Summary
• Error: don’t catch, unchecked
• Exception:
• RuntimeException:
• (Usually) don’t catch, unchecked
• All others: checked, so must
• Catch, or
• Mention they may be thrown
Chapter 2: Program Correctness and Efficiency 17
Checked and Unchecked Exceptions
• Checked exceptions
• Normally not due to programmer error
• Generally beyond the control of the programmer
• Examples: IOException, FileNotFoundException
• Unchecked exception may result from
• Programmer error
• Serious external condition that is unrecoverable
Chapter 2: Program Correctness and Efficiency 18
Checked and Unchecked Exceptions (2)
unchecked
unchecked
Chapter 2: Program Correctness and Efficiency 19
Some Common Unchecked Exceptions
• ArithmeticException
• Division by zero, etc.
• ArrayIndexOutOfBoundsException
• NumberFormatException
• Converting a “bad” string to a number
• NullPointerException
• NoSuchElementException
• No more tokens available
Chapter 2: Program Correctness and Efficiency 20
Catching and Handling Exceptions
• When an exception is thrown, the normal
sequence of execution is interrupted
• Default behavior,i.e., no handler
• Program stops
• JVM displays an error message
• The programmer may provide a handler
• Enclose statements in a try block
• Process the exception in a catch block
Chapter 2: Program Correctness and Efficiency 21
Example Handler
InputStream in = null;
try {
in = new FileInputStream(args[0]);
...
} catch (FileNotFoundException e) {
System.out.printf(
“File not found: %s%n”, name);
} catch (Throwable e) {
System.err.println("Exception!");
e.printStackTrace(System.err);
} finally {
if (in != null) in.close();
}
Chapter 2: Program Correctness and Efficiency 22
Uncaught Exceptions
• Uncaught exception exits VM with a stack trace
• The stack trace shows
• The sequence of method calls
• Starts with throwing method
• Ends at main
Chapter 2: Program Correctness and Efficiency 23
The try-catch Sequence
• Avoiding uncaught exceptions
• Write a try-catch to handle the exception
• Point: prevent ugly program termination!
• Unpleasant for user
• Worse, may leave things messed up / “broken”
• catch block is skipped if no exception thrown
within the try block
Chapter 2: Program Correctness and Efficiency 24
Handling Exceptions to Recover from Errors
• Exceptions provide the opportunity to
• Report errors
• Recover from errors
• User errors common, and should be recoverable
• Most closely enclosing handler that matches is
the one that executes
• A handler matches if its class includes what’s thrown
• Compiler displays an error message if it
encounters an unreachable catch clause
Chapter 2: Program Correctness and Efficiency 25
The finally block
• On exception, a try is abandoned
• Sometimes more actions must be taken
• Example: Close an output file
• Code in a finally block is always executed
• After the try finishes normally, or
• After a catch clause completes
• finally is optional
Chapter 2: Program Correctness and Efficiency 26
Example of finally block
try {
InputStream ins = ...;
... ins.read(); ...
} catch (EOFException e) {
System.err.println(“Unexpected EOF”);
e.printStackTrace();
System.exit(17);
} finally {
if (ins != null) ins.close();
}
Chapter 2: Program Correctness and Efficiency 27
Throwing Exceptions
• Lower-level method can pass exception through
• Can be caught and handled by a higher-level method
• Mark lower-level method
• Say it may throw a checked exception
• Mark by throws clause in the header
• May throw the exception in the lower-level method
• Use a throw statement
• Particularly useful if calling module already has
a handler for this exception type
Chapter 2: Program Correctness and Efficiency 28
Throwing Exceptions (2)
• Use a throw statement when you detect an error
• Further execution stops immediately:
• Goes to closest suitable handler
• May be a number of level of calls earlier
• Does execute any finally blocks in the middle
Chapter 2: Program Correctness and Efficiency 29
Example of Throwing an Exception
/** adds a new entry or changes an old one
* @param name the name to create/update
* @param number the (new) number
* @return the previous number, a String
* @throws IllegalArgumentException if the number
* is not in phone number format
*/
public String addOrChangeEntry(
String name, String number) {
if (!isPhoneNumberFormat(number)) {
throw new IllegalArgumentException(
“Invalid phone number: “ + number);
}
...
}
Chapter 2: Program Correctness and Efficiency 30
Another Example of Throwing an Exception
public void accessLocalFile (String askingUser)
throws CertificateException {
...
if (user’s secure socket certificate bad) {
throw new CertificateException(reason);
}
...
}
Chapter 2: Program Correctness and Efficiency 31
Programming Style
• You can always avoid handling exceptions:
• Declare that they are thrown, or
• Throw them and let them be handled farther back
• But: usually best to handle instead of passing
• Guidelines:
1. If recoverable here, handle here
2. If checked exception likely to be caught higher up
Declare that it can occur using a throws clause
3. Don’t use throws with unchecked exceptions
Use an @throws javadoc comment when helpful
Chapter 2: Program Correctness and Efficiency 32
Programming Style (2)
Don’t do this!
try {...} catch (Throwable e) { }
• Omits arbitrary patches of code
Can leave things in “broken” state
• No warning to user
• Leads to hidden, difficult to detect, defects
Chapter 2: Program Correctness and Efficiency 33
Handling Exceptions in Phone Dir Example
In loadData:
FileNotFoundException from FileReader
constructor
IOException from readLine
In PDConsoleUI:
InputMismatchException from nextInt
In addOrChangeEntry:
IllegalArgumentException for empty String
Chapter 2: Program Correctness and Efficiency 34
Testing Programs
• A program with
• No syntax/semantic errors, and
• No run-time errors,
• May still contain logic errors
• “Best” case is logic error that always executes
• Otherwise, hard to find!
• Worst case is logic error in code rarely run
Goal of testing: Test every part of the code, on
“good” and “bad”/”hard” cases
Chapter 2: Program Correctness and Efficiency 35
Structured Walkthroughs
• Most logic errors:
• Come from the design phase
• Result from an incorrect algorithm
• Logic errors sometimes come from typos that do
not cause syntax, semantic, or run-time errors
• Famous FORTRAN: DO 10 I = 1.100
• Common C: if (i = 3) ...
• One way to test: hand-trace algorithm
before implementing!
• Thus: Structured Walkthroughs
Chapter 2: Program Correctness and Efficiency 36
Structured Walkthroughs (2)
The Designer:
• Explains the algorithm to other team members
• Simulate its execution with them looking on
The Team:
• Verifies that it works
• Verifies that it handles all cases
Walkthroughs are helpful, but do not replace testing!
Chapter 2: Program Correctness and Efficiency 37
Testing Defined
• Testing:
• Exercising a program under controlled conditions
• Verifying the results
• Purpose: detect program defects after
• All syntax/semantic errors removed
• Program compiles
• No amount of testing can guarantee the absence of
defects in sufficiently complex programs
Chapter 2: Program Correctness and Efficiency 38
Levels of Testing
• Unit testing: checking the smallest testable piece
• A method or class
• Integration testing:
• The interactions among units
• System testing: testing the program in context
• Acceptance testing: system testing intended to
show that the program meets its functional
requirements
Chapter 2: Program Correctness and Efficiency 39
Some Types of Testing
• Black-box testing:
• Tests item based only on its interfaces and
functional requirements
• Assumes no knowledge of internals
• White-box testing:
• Tests with knowledge of internal structure
Chapter 2: Program Correctness and Efficiency 40
Preparing to Test
• Develop test plan early, in the design phase
• How to test the software
• When to do the tests
• Who will do the testing
• What test data to use
• Early test plan allows testing during design & coding
• Good programmer practices defensive programming
• Includes code to detect unexpected or invalid data
Chapter 2: Program Correctness and Efficiency 41
Testing Tips for Program Systems
• Program systems contain collections of classes,
each with several methods
• A method specification should document
• Input parameters
• Expected results
• Carefully document (with javadoc, etc.):
• Each method parameter
• Each class attribute (instance and static variable)
• As you write the code!
Chapter 2: Program Correctness and Efficiency 42
Testing Tips for Program Systems (2)
Trace execution by displaying method name as
you enter a method:
public static final boolean TRACING = true;
...
public int computeWeight (...) {
if (TRACING) {
trace.printf(“Entering computeWeight“);
}
...
}
Chapter 2: Program Correctness and Efficiency 43
Testing Tips for Program Systems (3)
Display values of all input parameters on entry:
public int computeWeight (float volume,
float density) {
if (TRACING) {
trace.printf(“Entering computeWeight“);
trace.printf(“volume = %f, “, volume);
trace.printf(“density = %f%n“, density);
}
...
}
Chapter 2: Program Correctness and Efficiency 44
Testing Tips for Program Systems (4)
• Display values of any class attributes (instance
and static variables) accessed by the method
• Display values of all method outputs at point of
return from a method
• Plan for testing as you write each module,
• Not after the fact!
Chapter 2: Program Correctness and Efficiency 45
Developing Test Data
• Specify test data during analysis and design
• For each level of testing: unit, integration, and system
• Black-box testing: unit inputs  outputs
• Check all expected inputs
• Check unanticipated data
• White-box testing: exercise all code paths
• Different tests to make each if test (etc.) true and false
• Called coverage
Chapter 2: Program Correctness and Efficiency 46
Developing Test Data (2)
• Helpful to do both black- and white-box testing
• Black-box tests can be developed early since
they have to do with the unit specification
• White-box tests are developed with detailed
design or implementation: need code structure
Chapter 2: Program Correctness and Efficiency 47
Testing Boundary Conditions
• Exercise all paths for
• Hand-tracing in a structured walkthrough
• Performing white-box testing
• Must check special cases:
boundary conditions
• Examples:
• Loop executes 0 times, 1 time, all the way to the end
• Item not found
Chapter 2: Program Correctness and Efficiency 48
Who does the testing?
• Normally testing is done by
• The programmer
• Team members who did not code the module
• Final users of the product
• Programmers often blind to their own oversights
• Companies may have quality assurance groups
• Extreme programming: programmers paired
• One writes the code
• The other writes the tests
Chapter 2: Program Correctness and Efficiency 49
Stubs for Testing
• Hard to test a method or class that interacts with
other methods or classes
• A stub stands in for a method not yet available
• The stub:
• Has the same header as the method it replaces
• Body only displays a message that it was called
• Sometimes you need to synthesize a reasonable
facsimile of a result, for the caller to continue
Chapter 2: Program Correctness and Efficiency 50
Drivers
A driver program:
• Declares necessary instances and variables
• Provides values for method inputs
• Calls the method
• Displays values of method outputs
• A main method in a class can serve as a driver
to test the class’s methods
Chapter 2: Program Correctness and Efficiency 51
Regression Testing
• Once code has passed all initial tests, it is
important to continue to test regularly
• Environment and other changes  “software rot”
• A regression test is designed to:
• Catch any “regression” or decay in the software
• Insure old functionality works in face of enhancement
• Alert earlier to any issues arising from other changes
• Regression testing eased by a testing framework
Chapter 2: Program Correctness and Efficiency 52
Using a Testing Framework
Testing framework: software that facilitates:
• Writing test cases
• Organizing the test cases into test suites
• Running the test suites
• Reporting the results
Chapter 2: Program Correctness and Efficiency 53
JUnit
• A Java testing framework
• Open-source product
• Can be used stand-alone or with an IDE
• Available from junit.org
Chapter 2: Program Correctness and Efficiency 54
JUnit Example
import junit.framework.*;
public class TestDirectoryEntry
extends TestCase {
private DirectoryEntry tom;
private DirectoryEntry dick;
private DirectoryEntry tom2;
public void setUp () {
tom = new DirectoryEntry(“Tom” , “...”);
dick = new DirectoryEntry(“Dick”, “...”);
tom2 = new DirectoryEntry(“Tom” , “...”);
}
Chapter 2: Program Correctness and Efficiency 55
JUnit Example (2)
public void testTomCreate () {
assertEquals(tom.getName() , “Tom”);
assertEquals(tom.getNumber(), “...”);
}
public void testTomEqualsDick () {
assertFalse(tom.equals(dick));
assertFalse(dick.equals(tom));
}
Chapter 2: Program Correctness and Efficiency 56
JUnit Example (3)
public void testTomEqualsTom () {
assertTrue(tom.equals(tom));
assertTrue(tom.equals(tom2));
assertTrue(tom2.equals(tom));
}
public void testSetNumber () {
dick.setNumber(tom.getNumber());
assertEquals(tom.getNumber(),dick.getNumber());
}
Chapter 2: Program Correctness and Efficiency 57
Integration Testing
• Larger components: collection of classes
• Done with smaller collection, then larger ones
• Drive with use cases: scenarios with
• Sample user inputs
• Expected outputs
• Can be challenging to automate
Chapter 2: Program Correctness and Efficiency 58
Debugging a Program
Debugging: the major activity during the testing phase
• Testing determines that there is an error
• Debugging determines the cause
• Debugging is like detective work: logical deduction
• Inspect all program output carefully
• Insert additional output statements to find out more
• Use breakpoints to examine world ...
at carefully selected points
Chapter 2: Program Correctness and Efficiency 59
Using a Debugger
• Debuggers often are included with IDEs
• Debugger supports incremental program execution
• Single-step execution provides increments as small as
one program statement (or even one instruction)
• Breakpoints traverse larger portions of code at once
• Details depend on the specfic IDE
Key to debugging: Think first! Think a lot!
• Also: try to split possible error sources in half with
each investigation
Chapter 2: Program Correctness and Efficiency 60
Reasoning about Programs:
Assertions and Loop Invariants
• Assertions:
• Logical statements about program state
• Claimed to be true
• At a particular point in the program
• Written as a comment, OR use assert statement
• Preconditions and postconditions are assertions
• Loop invariants are also assertions
Chapter 2: Program Correctness and Efficiency 61
Reasoning about Programs:
Loop Invariants
A loop invariant:
• Helps prove that a loop meets it specification
• Is true before loop begins
• Is true at the beginning of each iteration
• Is true just after loop exit
Example: Sorting an array of n elements
Sorted(i): Array elements j, for 0 ≤ j < i, are sorted
Beginning: Sorted(0) is (trivially) true
Middle: We insure initial portion sorted as we increase i
End: Sorted(n): All elements 0 ≤ j < n are sorted
Chapter 2: Program Correctness and Efficiency 62
Efficiency of Algorithms
Question: How can we characterize the
performance of an algorithm ...
• Without regard to a specific computer?
• Without regard to a specific language?
• Over a wide range of inputs?
Desire: Function that describes execution time in
terms of input size
• Other measures might be memory needed, etc.
Chapter 2: Program Correctness and Efficiency 63
The “Order” of Performance: (Big) O
• Basic idea:
1. Ignore constant factor: computer and language
implementation details affect that: go for
fundamental rate of increase with problem size.
2. Consider fastest growing term: Eventually, for large
problems, it will dominate.
• Value: Compares fundamental performance
difference of algorithms
• Caveat: For smaller problems, big-O worse
performer may actually do better
Chapter 2: Program Correctness and Efficiency 64
T(n) = O(f(n))
• T(n) = time for algorithm on input size n
• f(n) = a simpler function that grows at about the
same rate
• Example: T(n) = 3n2+5n-17 = O(n2)
• f(n) has faster growing term
• no extra leading constant in f(n)
Chapter 2: Program Correctness and Efficiency 65
T(n) = O(f(n)) Defined
1. n0 and
2. c such that
If n > n0 then c·f(n) ≥ T(n)
Example: T(n) = 3n2+5n-17
Pick c = 4, say; need 4n0
2 > 3n0
2+5n0-17
n0
2 > 5n0-17, for which n0 = 5 will do.
Chapter 2: Program Correctness and Efficiency 66
Efficiency of Algorithms (continued)
Chapter 2: Program Correctness and Efficiency 67
Efficiency of Algorithms (continued)
Chapter 2: Program Correctness and Efficiency 68
Efficiency of Algorithms (continued)
Chapter 2: Program Correctness and Efficiency 69
Efficiency of Algorithms (continued)
Chapter 2: Program Correctness and Efficiency 70
Efficiency of Algorithms (continued)
Chapter 2: Program Correctness and Efficiency 71
Efficiency Examples
public static int find (int[]x, int val) {
for (int i = 0; i < x.length; i++) {
if (x[i] == val)
return i;
}
return -1; // not found
}
Letting n be x.length:
Average iterations if found = (1+...+n)/n = (n+1)/2 = O(n)
Iterations if not found = n = O(n)
Hence this is called linear search.
Chapter 2: Program Correctness and Efficiency 72
Efficiency Examples (2)
public static boolean allDifferent (
int[] x, int[] y) {
for (int i = 0; i < x.length; i++) {
if (find(y, x[i]) != -1)
return false;
}
return true; // no x element found in y
}
Letting m be x.length and n be y.length:
Time if all different = O(m·n) = m · cost of search(n)
Chapter 2: Program Correctness and Efficiency 73
Efficiency Examples (3)
public static boolean unique (int[] x) {
for (int i = 0; i < x.length; i++) {
for (int j = 0; j < x.length; j++ {
if (i != j && x[i] == x[j])
return false;
}
}
return true; // no duplicates in x
}
Letting n be x.length:
Time if unique = n2 iterations = O(n2)
Chapter 2: Program Correctness and Efficiency 74
Efficiency Examples (4)
public static boolean unique (int[] x) {
for (int i = 0; i < x.length; i++) {
for (int j = i+1; j < x.length; j++ {
if (i != j && x[i] == x[j])
return false;
}
}
return true; // no duplicates in x
}
Letting n be x.length:
Time if unique = (n-1)+(n-2)+...+2+1 iterations =
n(n-1)/2 iterations = O(n2) still ... only factor of 2 better
Chapter 2: Program Correctness and Efficiency 75
Efficiency Examples (5)
for (int i = 1; i < n; i *= 2) {
do something with x[i]
}
Sequence is 1, 2, 4, 8, ..., ~n.
Number of iterations = log2n = log n.
Computer scientists generally use base 2 for log, since
that matches with number of bits, etc.
Also O(logbn) = O(log2n) since chane of base just
multiples by a constant: log2n = logbn/logb2
Chapter 2: Program Correctness and Efficiency 76
Chessboard Puzzle
Payment scheme #1: $1 on first square, $2 on second,
$3 on third, ..., $64 on 64th.
Payment scheme #2: 1¢ on first square, 2¢ on second,
4¢ on third, 8¢ on fourth, etc.
Which is best?
Chapter 2: Program Correctness and Efficiency 77
Chessboard Puzzle Analyzed
Payment scheme #1: Total = $1+$2+$3+...+$64 =
$6465/2 = $1755
Payment scheme #2: 1¢+2¢+4¢+...+263¢ = 264-1¢ =
$184.467440737 trillion
Many cryptographic schemes require O(2n) work to
break a key of length n bits. A key of length n=40 is
perhaps breakable, but one with n=100 is not.

More Related Content

Similar to lecture-c-corr-effkkkkkkkkkkkkkp (1).ppt

Exception handling
Exception handlingException handling
Exception handlingpooja kumari
 
Lecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptxLecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptxVishuSaini22
 
Lecture 3.1.1 Try Throw Catch.pptx
Lecture 3.1.1 Try Throw Catch.pptxLecture 3.1.1 Try Throw Catch.pptx
Lecture 3.1.1 Try Throw Catch.pptxsunilsoni446112
 
Chap2 exception handling
Chap2 exception handlingChap2 exception handling
Chap2 exception handlingraksharao
 
Java-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handlingJava-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handlingraksharao
 
9781285852744 ppt ch14
9781285852744 ppt ch149781285852744 ppt ch14
9781285852744 ppt ch14Terry Yoast
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptxRDeepa9
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptxRDeepa9
 
A36519192_21789_4_2018_Exception Handling.ppt
A36519192_21789_4_2018_Exception Handling.pptA36519192_21789_4_2018_Exception Handling.ppt
A36519192_21789_4_2018_Exception Handling.pptpromila09
 
Exception Handling in UiPath.pptx
Exception Handling in UiPath.pptxException Handling in UiPath.pptx
Exception Handling in UiPath.pptxApurbaSamanta9
 
Exception handling in java-PPT.pptx
Exception handling in java-PPT.pptxException handling in java-PPT.pptx
Exception handling in java-PPT.pptxsonalipatil225940
 
Exception handling in c programming
Exception handling in c programmingException handling in c programming
Exception handling in c programmingRaza Najam
 
Exception handling in JAVA
Exception handling in JAVAException handling in JAVA
Exception handling in JAVAKunal Singh
 

Similar to lecture-c-corr-effkkkkkkkkkkkkkp (1).ppt (20)

Exception handling
Exception handlingException handling
Exception handling
 
Lecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptxLecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptx
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling in ASP .NET
Exception handling in ASP .NETException handling in ASP .NET
Exception handling in ASP .NET
 
Lecture 3.1.1 Try Throw Catch.pptx
Lecture 3.1.1 Try Throw Catch.pptxLecture 3.1.1 Try Throw Catch.pptx
Lecture 3.1.1 Try Throw Catch.pptx
 
Chap2 exception handling
Chap2 exception handlingChap2 exception handling
Chap2 exception handling
 
Java-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handlingJava-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handling
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
9781285852744 ppt ch14
9781285852744 ppt ch149781285852744 ppt ch14
9781285852744 ppt ch14
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptx
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptx
 
Handling
HandlingHandling
Handling
 
A36519192_21789_4_2018_Exception Handling.ppt
A36519192_21789_4_2018_Exception Handling.pptA36519192_21789_4_2018_Exception Handling.ppt
A36519192_21789_4_2018_Exception Handling.ppt
 
Exception_Handling.pptx
Exception_Handling.pptxException_Handling.pptx
Exception_Handling.pptx
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception Handling in UiPath.pptx
Exception Handling in UiPath.pptxException Handling in UiPath.pptx
Exception Handling in UiPath.pptx
 
Exception
ExceptionException
Exception
 
Exception handling in java-PPT.pptx
Exception handling in java-PPT.pptxException handling in java-PPT.pptx
Exception handling in java-PPT.pptx
 
Exception handling in c programming
Exception handling in c programmingException handling in c programming
Exception handling in c programming
 
Exception handling in JAVA
Exception handling in JAVAException handling in JAVA
Exception handling in JAVA
 

Recently uploaded

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 

Recently uploaded (20)

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 

lecture-c-corr-effkkkkkkkkkkkkkp (1).ppt

  • 1. Program Correctness and Efficiency Following Koffmann and Wolfgang Chapter 2
  • 2. Chapter 2: Program Correctness and Efficiency 2 Outline • Categories of program errors • Why you should catch exceptions • The Exception hierarchy • Checked and unchecked exceptions • The try-catch-finally sequence • Throwing an exception: • What it means • How to do it
  • 3. Chapter 2: Program Correctness and Efficiency 3 Outline (continued) • A variety of testing strategies • How to write testing methods • Debugging techniques and debugger programs • Program verification: assertions and loop invariants • Big-O notation • What it is • How to use it to analyze an algorithm’s efficiency
  • 4. Chapter 2: Program Correctness and Efficiency 4 Program Defects and “Bugs” • An efficient program is worthless if it breaks or produces a wrong answer • Defects often appear in software after it is delivered • Testing cannot prove the absence of defects • It can be difficult to test a software product completely in the environment in which it is used • Debugging: removing defects
  • 5. Chapter 2: Program Correctness and Efficiency 5 Major Categories of Defects • Syntax and other in-advance errors • Run-time errors and exceptions • Logic Errors
  • 6. Chapter 2: Program Correctness and Efficiency 6 Syntax Errors • Syntax errors: grammatical mistakes in a program • The compiler detects syntax errors • You must correct them to compile successfully • Some common syntax errors include: • Omitting or misplacing braces, parentheses, etc. • Misplaced end-of-comment • Typographical errors (in names, etc.) • Misplaced keywords
  • 7. Chapter 2: Program Correctness and Efficiency 7 Semantic Errors • Semantic errors: may obey grammar, but violate other rules of the language • The compiler detects semantic errors • You must correct them to compile successfully • Some common semantic errors include: • Performing an incorrect operation on a primitive type value • Invoking an instance method not defined • Not declaring a variable before using it • Providing multiple declarations of a variable • Failure to provide an exception handler • Failure to import a library routine
  • 8. Chapter 2: Program Correctness and Efficiency 8 Run-time Errors or Exceptions • Run-time errors • Occur during program execution (run-time!) • Occur when the JVM detects an operation that it knows to be incorrect • Cause the JVM to throw an exception • Examples of run-time errors include • Division by zero • Array index out of bounds • Number format error • Null pointer exceptions
  • 9. Chapter 2: Program Correctness and Efficiency 9 Run-time Errors or Exceptions (continued)
  • 10. Chapter 2: Program Correctness and Efficiency 10 Logic Errors • A logic error is programmer mistake in • the design of a class or method, or • the implementation of an algorithm • Most logic errors • Are not syntax or semantic errors: get by the compiler • Do not cause run-time errors • Thus they are difficult to find • Sometimes found through testing • Sometimes found by users
  • 11. Chapter 2: Program Correctness and Efficiency 11 Avoiding Logic Errors • Work from a precise specification • Strive for clarity and simplicity • Consider “corner” / extreme cases • Have reviews / walk-throughs: other eyes • Use library/published algorithms where possible • Think through pre/post conditions, invariants • Be organized and careful in general
  • 12. Chapter 2: Program Correctness and Efficiency 12 The Exception Class Hierarchy • When an exception occurs, the first thing that happens is a new of a Java exception object • Different exception classes have different rules • Throwable is the root superclass of the exception class hierarchy • Error is a subclass of Throwable • Exception is a subclass of Throwable •RuntimeException is a subclass of Exception
  • 13. Chapter 2: Program Correctness and Efficiency 13 The Class Throwable • Throwable is the superclass of all exceptions • All exception classes inherit its methods
  • 14. Chapter 2: Program Correctness and Efficiency 14 The Class Throwable (continued)
  • 15. Chapter 2: Program Correctness and Efficiency 15 The Exception Class Hierarchy (2) Throwable is the superclass of all exception classes • Error is for things a program should not catch • Example: OutOfMemoryError • Exception is for things a program might catch • RuntimeException is for things the VM might throw • It can happen anywhere: e.g., any object access can throw NullPointerException • So not required to catch it • All others must be either: • Explicitly caught or • Explicitly mentioned as thrown by the method
  • 16. Chapter 2: Program Correctness and Efficiency 16 Exception Hierarchy Summary • Error: don’t catch, unchecked • Exception: • RuntimeException: • (Usually) don’t catch, unchecked • All others: checked, so must • Catch, or • Mention they may be thrown
  • 17. Chapter 2: Program Correctness and Efficiency 17 Checked and Unchecked Exceptions • Checked exceptions • Normally not due to programmer error • Generally beyond the control of the programmer • Examples: IOException, FileNotFoundException • Unchecked exception may result from • Programmer error • Serious external condition that is unrecoverable
  • 18. Chapter 2: Program Correctness and Efficiency 18 Checked and Unchecked Exceptions (2) unchecked unchecked
  • 19. Chapter 2: Program Correctness and Efficiency 19 Some Common Unchecked Exceptions • ArithmeticException • Division by zero, etc. • ArrayIndexOutOfBoundsException • NumberFormatException • Converting a “bad” string to a number • NullPointerException • NoSuchElementException • No more tokens available
  • 20. Chapter 2: Program Correctness and Efficiency 20 Catching and Handling Exceptions • When an exception is thrown, the normal sequence of execution is interrupted • Default behavior,i.e., no handler • Program stops • JVM displays an error message • The programmer may provide a handler • Enclose statements in a try block • Process the exception in a catch block
  • 21. Chapter 2: Program Correctness and Efficiency 21 Example Handler InputStream in = null; try { in = new FileInputStream(args[0]); ... } catch (FileNotFoundException e) { System.out.printf( “File not found: %s%n”, name); } catch (Throwable e) { System.err.println("Exception!"); e.printStackTrace(System.err); } finally { if (in != null) in.close(); }
  • 22. Chapter 2: Program Correctness and Efficiency 22 Uncaught Exceptions • Uncaught exception exits VM with a stack trace • The stack trace shows • The sequence of method calls • Starts with throwing method • Ends at main
  • 23. Chapter 2: Program Correctness and Efficiency 23 The try-catch Sequence • Avoiding uncaught exceptions • Write a try-catch to handle the exception • Point: prevent ugly program termination! • Unpleasant for user • Worse, may leave things messed up / “broken” • catch block is skipped if no exception thrown within the try block
  • 24. Chapter 2: Program Correctness and Efficiency 24 Handling Exceptions to Recover from Errors • Exceptions provide the opportunity to • Report errors • Recover from errors • User errors common, and should be recoverable • Most closely enclosing handler that matches is the one that executes • A handler matches if its class includes what’s thrown • Compiler displays an error message if it encounters an unreachable catch clause
  • 25. Chapter 2: Program Correctness and Efficiency 25 The finally block • On exception, a try is abandoned • Sometimes more actions must be taken • Example: Close an output file • Code in a finally block is always executed • After the try finishes normally, or • After a catch clause completes • finally is optional
  • 26. Chapter 2: Program Correctness and Efficiency 26 Example of finally block try { InputStream ins = ...; ... ins.read(); ... } catch (EOFException e) { System.err.println(“Unexpected EOF”); e.printStackTrace(); System.exit(17); } finally { if (ins != null) ins.close(); }
  • 27. Chapter 2: Program Correctness and Efficiency 27 Throwing Exceptions • Lower-level method can pass exception through • Can be caught and handled by a higher-level method • Mark lower-level method • Say it may throw a checked exception • Mark by throws clause in the header • May throw the exception in the lower-level method • Use a throw statement • Particularly useful if calling module already has a handler for this exception type
  • 28. Chapter 2: Program Correctness and Efficiency 28 Throwing Exceptions (2) • Use a throw statement when you detect an error • Further execution stops immediately: • Goes to closest suitable handler • May be a number of level of calls earlier • Does execute any finally blocks in the middle
  • 29. Chapter 2: Program Correctness and Efficiency 29 Example of Throwing an Exception /** adds a new entry or changes an old one * @param name the name to create/update * @param number the (new) number * @return the previous number, a String * @throws IllegalArgumentException if the number * is not in phone number format */ public String addOrChangeEntry( String name, String number) { if (!isPhoneNumberFormat(number)) { throw new IllegalArgumentException( “Invalid phone number: “ + number); } ... }
  • 30. Chapter 2: Program Correctness and Efficiency 30 Another Example of Throwing an Exception public void accessLocalFile (String askingUser) throws CertificateException { ... if (user’s secure socket certificate bad) { throw new CertificateException(reason); } ... }
  • 31. Chapter 2: Program Correctness and Efficiency 31 Programming Style • You can always avoid handling exceptions: • Declare that they are thrown, or • Throw them and let them be handled farther back • But: usually best to handle instead of passing • Guidelines: 1. If recoverable here, handle here 2. If checked exception likely to be caught higher up Declare that it can occur using a throws clause 3. Don’t use throws with unchecked exceptions Use an @throws javadoc comment when helpful
  • 32. Chapter 2: Program Correctness and Efficiency 32 Programming Style (2) Don’t do this! try {...} catch (Throwable e) { } • Omits arbitrary patches of code Can leave things in “broken” state • No warning to user • Leads to hidden, difficult to detect, defects
  • 33. Chapter 2: Program Correctness and Efficiency 33 Handling Exceptions in Phone Dir Example In loadData: FileNotFoundException from FileReader constructor IOException from readLine In PDConsoleUI: InputMismatchException from nextInt In addOrChangeEntry: IllegalArgumentException for empty String
  • 34. Chapter 2: Program Correctness and Efficiency 34 Testing Programs • A program with • No syntax/semantic errors, and • No run-time errors, • May still contain logic errors • “Best” case is logic error that always executes • Otherwise, hard to find! • Worst case is logic error in code rarely run Goal of testing: Test every part of the code, on “good” and “bad”/”hard” cases
  • 35. Chapter 2: Program Correctness and Efficiency 35 Structured Walkthroughs • Most logic errors: • Come from the design phase • Result from an incorrect algorithm • Logic errors sometimes come from typos that do not cause syntax, semantic, or run-time errors • Famous FORTRAN: DO 10 I = 1.100 • Common C: if (i = 3) ... • One way to test: hand-trace algorithm before implementing! • Thus: Structured Walkthroughs
  • 36. Chapter 2: Program Correctness and Efficiency 36 Structured Walkthroughs (2) The Designer: • Explains the algorithm to other team members • Simulate its execution with them looking on The Team: • Verifies that it works • Verifies that it handles all cases Walkthroughs are helpful, but do not replace testing!
  • 37. Chapter 2: Program Correctness and Efficiency 37 Testing Defined • Testing: • Exercising a program under controlled conditions • Verifying the results • Purpose: detect program defects after • All syntax/semantic errors removed • Program compiles • No amount of testing can guarantee the absence of defects in sufficiently complex programs
  • 38. Chapter 2: Program Correctness and Efficiency 38 Levels of Testing • Unit testing: checking the smallest testable piece • A method or class • Integration testing: • The interactions among units • System testing: testing the program in context • Acceptance testing: system testing intended to show that the program meets its functional requirements
  • 39. Chapter 2: Program Correctness and Efficiency 39 Some Types of Testing • Black-box testing: • Tests item based only on its interfaces and functional requirements • Assumes no knowledge of internals • White-box testing: • Tests with knowledge of internal structure
  • 40. Chapter 2: Program Correctness and Efficiency 40 Preparing to Test • Develop test plan early, in the design phase • How to test the software • When to do the tests • Who will do the testing • What test data to use • Early test plan allows testing during design & coding • Good programmer practices defensive programming • Includes code to detect unexpected or invalid data
  • 41. Chapter 2: Program Correctness and Efficiency 41 Testing Tips for Program Systems • Program systems contain collections of classes, each with several methods • A method specification should document • Input parameters • Expected results • Carefully document (with javadoc, etc.): • Each method parameter • Each class attribute (instance and static variable) • As you write the code!
  • 42. Chapter 2: Program Correctness and Efficiency 42 Testing Tips for Program Systems (2) Trace execution by displaying method name as you enter a method: public static final boolean TRACING = true; ... public int computeWeight (...) { if (TRACING) { trace.printf(“Entering computeWeight“); } ... }
  • 43. Chapter 2: Program Correctness and Efficiency 43 Testing Tips for Program Systems (3) Display values of all input parameters on entry: public int computeWeight (float volume, float density) { if (TRACING) { trace.printf(“Entering computeWeight“); trace.printf(“volume = %f, “, volume); trace.printf(“density = %f%n“, density); } ... }
  • 44. Chapter 2: Program Correctness and Efficiency 44 Testing Tips for Program Systems (4) • Display values of any class attributes (instance and static variables) accessed by the method • Display values of all method outputs at point of return from a method • Plan for testing as you write each module, • Not after the fact!
  • 45. Chapter 2: Program Correctness and Efficiency 45 Developing Test Data • Specify test data during analysis and design • For each level of testing: unit, integration, and system • Black-box testing: unit inputs  outputs • Check all expected inputs • Check unanticipated data • White-box testing: exercise all code paths • Different tests to make each if test (etc.) true and false • Called coverage
  • 46. Chapter 2: Program Correctness and Efficiency 46 Developing Test Data (2) • Helpful to do both black- and white-box testing • Black-box tests can be developed early since they have to do with the unit specification • White-box tests are developed with detailed design or implementation: need code structure
  • 47. Chapter 2: Program Correctness and Efficiency 47 Testing Boundary Conditions • Exercise all paths for • Hand-tracing in a structured walkthrough • Performing white-box testing • Must check special cases: boundary conditions • Examples: • Loop executes 0 times, 1 time, all the way to the end • Item not found
  • 48. Chapter 2: Program Correctness and Efficiency 48 Who does the testing? • Normally testing is done by • The programmer • Team members who did not code the module • Final users of the product • Programmers often blind to their own oversights • Companies may have quality assurance groups • Extreme programming: programmers paired • One writes the code • The other writes the tests
  • 49. Chapter 2: Program Correctness and Efficiency 49 Stubs for Testing • Hard to test a method or class that interacts with other methods or classes • A stub stands in for a method not yet available • The stub: • Has the same header as the method it replaces • Body only displays a message that it was called • Sometimes you need to synthesize a reasonable facsimile of a result, for the caller to continue
  • 50. Chapter 2: Program Correctness and Efficiency 50 Drivers A driver program: • Declares necessary instances and variables • Provides values for method inputs • Calls the method • Displays values of method outputs • A main method in a class can serve as a driver to test the class’s methods
  • 51. Chapter 2: Program Correctness and Efficiency 51 Regression Testing • Once code has passed all initial tests, it is important to continue to test regularly • Environment and other changes  “software rot” • A regression test is designed to: • Catch any “regression” or decay in the software • Insure old functionality works in face of enhancement • Alert earlier to any issues arising from other changes • Regression testing eased by a testing framework
  • 52. Chapter 2: Program Correctness and Efficiency 52 Using a Testing Framework Testing framework: software that facilitates: • Writing test cases • Organizing the test cases into test suites • Running the test suites • Reporting the results
  • 53. Chapter 2: Program Correctness and Efficiency 53 JUnit • A Java testing framework • Open-source product • Can be used stand-alone or with an IDE • Available from junit.org
  • 54. Chapter 2: Program Correctness and Efficiency 54 JUnit Example import junit.framework.*; public class TestDirectoryEntry extends TestCase { private DirectoryEntry tom; private DirectoryEntry dick; private DirectoryEntry tom2; public void setUp () { tom = new DirectoryEntry(“Tom” , “...”); dick = new DirectoryEntry(“Dick”, “...”); tom2 = new DirectoryEntry(“Tom” , “...”); }
  • 55. Chapter 2: Program Correctness and Efficiency 55 JUnit Example (2) public void testTomCreate () { assertEquals(tom.getName() , “Tom”); assertEquals(tom.getNumber(), “...”); } public void testTomEqualsDick () { assertFalse(tom.equals(dick)); assertFalse(dick.equals(tom)); }
  • 56. Chapter 2: Program Correctness and Efficiency 56 JUnit Example (3) public void testTomEqualsTom () { assertTrue(tom.equals(tom)); assertTrue(tom.equals(tom2)); assertTrue(tom2.equals(tom)); } public void testSetNumber () { dick.setNumber(tom.getNumber()); assertEquals(tom.getNumber(),dick.getNumber()); }
  • 57. Chapter 2: Program Correctness and Efficiency 57 Integration Testing • Larger components: collection of classes • Done with smaller collection, then larger ones • Drive with use cases: scenarios with • Sample user inputs • Expected outputs • Can be challenging to automate
  • 58. Chapter 2: Program Correctness and Efficiency 58 Debugging a Program Debugging: the major activity during the testing phase • Testing determines that there is an error • Debugging determines the cause • Debugging is like detective work: logical deduction • Inspect all program output carefully • Insert additional output statements to find out more • Use breakpoints to examine world ... at carefully selected points
  • 59. Chapter 2: Program Correctness and Efficiency 59 Using a Debugger • Debuggers often are included with IDEs • Debugger supports incremental program execution • Single-step execution provides increments as small as one program statement (or even one instruction) • Breakpoints traverse larger portions of code at once • Details depend on the specfic IDE Key to debugging: Think first! Think a lot! • Also: try to split possible error sources in half with each investigation
  • 60. Chapter 2: Program Correctness and Efficiency 60 Reasoning about Programs: Assertions and Loop Invariants • Assertions: • Logical statements about program state • Claimed to be true • At a particular point in the program • Written as a comment, OR use assert statement • Preconditions and postconditions are assertions • Loop invariants are also assertions
  • 61. Chapter 2: Program Correctness and Efficiency 61 Reasoning about Programs: Loop Invariants A loop invariant: • Helps prove that a loop meets it specification • Is true before loop begins • Is true at the beginning of each iteration • Is true just after loop exit Example: Sorting an array of n elements Sorted(i): Array elements j, for 0 ≤ j < i, are sorted Beginning: Sorted(0) is (trivially) true Middle: We insure initial portion sorted as we increase i End: Sorted(n): All elements 0 ≤ j < n are sorted
  • 62. Chapter 2: Program Correctness and Efficiency 62 Efficiency of Algorithms Question: How can we characterize the performance of an algorithm ... • Without regard to a specific computer? • Without regard to a specific language? • Over a wide range of inputs? Desire: Function that describes execution time in terms of input size • Other measures might be memory needed, etc.
  • 63. Chapter 2: Program Correctness and Efficiency 63 The “Order” of Performance: (Big) O • Basic idea: 1. Ignore constant factor: computer and language implementation details affect that: go for fundamental rate of increase with problem size. 2. Consider fastest growing term: Eventually, for large problems, it will dominate. • Value: Compares fundamental performance difference of algorithms • Caveat: For smaller problems, big-O worse performer may actually do better
  • 64. Chapter 2: Program Correctness and Efficiency 64 T(n) = O(f(n)) • T(n) = time for algorithm on input size n • f(n) = a simpler function that grows at about the same rate • Example: T(n) = 3n2+5n-17 = O(n2) • f(n) has faster growing term • no extra leading constant in f(n)
  • 65. Chapter 2: Program Correctness and Efficiency 65 T(n) = O(f(n)) Defined 1. n0 and 2. c such that If n > n0 then c·f(n) ≥ T(n) Example: T(n) = 3n2+5n-17 Pick c = 4, say; need 4n0 2 > 3n0 2+5n0-17 n0 2 > 5n0-17, for which n0 = 5 will do.
  • 66. Chapter 2: Program Correctness and Efficiency 66 Efficiency of Algorithms (continued)
  • 67. Chapter 2: Program Correctness and Efficiency 67 Efficiency of Algorithms (continued)
  • 68. Chapter 2: Program Correctness and Efficiency 68 Efficiency of Algorithms (continued)
  • 69. Chapter 2: Program Correctness and Efficiency 69 Efficiency of Algorithms (continued)
  • 70. Chapter 2: Program Correctness and Efficiency 70 Efficiency of Algorithms (continued)
  • 71. Chapter 2: Program Correctness and Efficiency 71 Efficiency Examples public static int find (int[]x, int val) { for (int i = 0; i < x.length; i++) { if (x[i] == val) return i; } return -1; // not found } Letting n be x.length: Average iterations if found = (1+...+n)/n = (n+1)/2 = O(n) Iterations if not found = n = O(n) Hence this is called linear search.
  • 72. Chapter 2: Program Correctness and Efficiency 72 Efficiency Examples (2) public static boolean allDifferent ( int[] x, int[] y) { for (int i = 0; i < x.length; i++) { if (find(y, x[i]) != -1) return false; } return true; // no x element found in y } Letting m be x.length and n be y.length: Time if all different = O(m·n) = m · cost of search(n)
  • 73. Chapter 2: Program Correctness and Efficiency 73 Efficiency Examples (3) public static boolean unique (int[] x) { for (int i = 0; i < x.length; i++) { for (int j = 0; j < x.length; j++ { if (i != j && x[i] == x[j]) return false; } } return true; // no duplicates in x } Letting n be x.length: Time if unique = n2 iterations = O(n2)
  • 74. Chapter 2: Program Correctness and Efficiency 74 Efficiency Examples (4) public static boolean unique (int[] x) { for (int i = 0; i < x.length; i++) { for (int j = i+1; j < x.length; j++ { if (i != j && x[i] == x[j]) return false; } } return true; // no duplicates in x } Letting n be x.length: Time if unique = (n-1)+(n-2)+...+2+1 iterations = n(n-1)/2 iterations = O(n2) still ... only factor of 2 better
  • 75. Chapter 2: Program Correctness and Efficiency 75 Efficiency Examples (5) for (int i = 1; i < n; i *= 2) { do something with x[i] } Sequence is 1, 2, 4, 8, ..., ~n. Number of iterations = log2n = log n. Computer scientists generally use base 2 for log, since that matches with number of bits, etc. Also O(logbn) = O(log2n) since chane of base just multiples by a constant: log2n = logbn/logb2
  • 76. Chapter 2: Program Correctness and Efficiency 76 Chessboard Puzzle Payment scheme #1: $1 on first square, $2 on second, $3 on third, ..., $64 on 64th. Payment scheme #2: 1¢ on first square, 2¢ on second, 4¢ on third, 8¢ on fourth, etc. Which is best?
  • 77. Chapter 2: Program Correctness and Efficiency 77 Chessboard Puzzle Analyzed Payment scheme #1: Total = $1+$2+$3+...+$64 = $6465/2 = $1755 Payment scheme #2: 1¢+2¢+4¢+...+263¢ = 264-1¢ = $184.467440737 trillion Many cryptographic schemes require O(2n) work to break a key of length n bits. A key of length n=40 is perhaps breakable, but one with n=100 is not.