210 likes | 314 Views
ITEC200 Week02. Program Correctness and Efficiency. Learning Objectives – Week 02 Program Correctness and Efficiency (Ch2). Students can: Explain the differences between syntax errors, run-time errors and logical errors
E N D
ITEC200 Week02 Program Correctness and Efficiency www.ics.mq.edu.au/ppdp
Learning Objectives – Week 02 Program Correctness and Efficiency (Ch2) Students can: • Explain the differences between syntax errors, run-time errors and logical errors • Explain and use the exception handling paradigm in Java (the Exception hierarchy, checked versus unchecked exceptions, try-catch-finally sequences, exception throwing) • Describe various testing strategies and implement them effectively • Use debugging tools and strategies effectively • Use assertions and loop invariants to verify program segments • Analyse algorithm efficiency using big-O notation www.ics.mq.edu.au/ppdp
Types of Errors • Syntax errors: mistakes in the grammar of a language • Run-time errors: errors picked up by the Java Virtual Machine during program execution (causes JVM to throw an Exception) • Logical errors: program works but doesn’t behave as intended www.ics.mq.edu.au/ppdp
The Exception class hierarchy www.ics.mq.edu.au/ppdp
Methods inherited from Throwable www.ics.mq.edu.au/ppdp
Checked and Unchecked Exceptions • Two categories of exceptions: checked and unchecked • Checked exception normally not due to programmer error and is beyond the control of the programmer • Unchecked exception may result from • Programmer error • Serious external conditions that are unrecoverable www.ics.mq.edu.au/ppdp
Checked and Unchecked Exceptions www.ics.mq.edu.au/ppdp
Catching and Handling Exceptions • When an exception is thrown, the normal sequence of execution is interrupted • Default behavior • Program stops • JVM displays an error message • The programmer may override the default behavior by • Enclosing statements in a ‘try’ block • Processing the exception in a ‘catch’ block • Specify any final instructions in a ‘finally’ block www.ics.mq.edu.au/ppdp
Throwing Exceptions • Instead of catching an exception in a lower-level method, it can be caught and handled by a higher-level method • Declare that the lower-level method may throw a checked exception by adding a throws clause to the method header • Can throw the exception in the lower-level method, using a throw statement • The throws clause is useful if a higher-level module already contains a catch clause for this exception type www.ics.mq.edu.au/ppdp
Catching Exceptions Example www.ics.mq.edu.au/ppdp
Levels and Types of Testing • Unit testing: checking the smallest testable piece of the software (a method or class) • Integration testing: testing the interactions among units • System testing: testing the program in context • Acceptance testing: system testing designed to show that the program meets its functional requirements • Black-box testing: tests the item based on its interfaces and functional requirements • White-box testing: tests the software with the knowledge of its internal structure www.ics.mq.edu.au/ppdp
Testing Tools • Stub: a substitute method that has the same header as the method it replaces, but its body only displays a message indicating that the stub was called • Driver program: specifies the testing routine to be executed • Test framework: a software productthat facilitates writing test cases, organizing the test cases into test suites, running the test suites, and reporting the results www.ics.mq.edu.au/ppdp
Debugging www.ics.mq.edu.au/ppdp
Reasoning about Programs: Assertions and Loop Invariants • Assertions: logical statements about a program that are claimed to be true; generally written as a comment • Preconditions and postconditions are assertions • A loop invariant is an assertion • Helps prove that a loop meets it specification • True before loop begins, at the beginning of each repetition of the loop body, and just after loop exit www.ics.mq.edu.au/ppdp
Assertions and Loop Invariants Example www.ics.mq.edu.au/ppdp
Efficiency of Algorithms • Difficult to get a precise measure of the performance of an algorithm or program • Can characterize a program by how the execution time or memory requirements increase as a function of increasing input size • Big-O notation • A simple way to determine the big-O of an algorithm or program is to look at the loops and to see whether the loops are nested www.ics.mq.edu.au/ppdp
Efficiency of Algorithms (continued) • Consider: • First time through outer loop, inner loop is executed n-1 times; next time n-2, and the last time once. • So we have • T(n) = 3(n – 1) + 3(n – 2) + … + 3 or • T(n) = 3(n – 1 + n – 2 + … + 1) www.ics.mq.edu.au/ppdp
Efficiency of Algorithms (continued) • We can reduce the expression in parentheses to: n x (n – 1) 2 • So, T(n) = 1.5n2 – 1.5n • We can therefore conclude that T(n) is O(n2) www.ics.mq.edu.au/ppdp
Efficiency of Algorithms (continued) www.ics.mq.edu.au/ppdp
Where to from here… • Work through Chapter 2 of the Koffman & Wolfgang Text • Conceptual Questions and Practical Exercises • Submit all preliminary work • Be prompt for your online class www.ics.mq.edu.au/ppdp
Acknowledgements These slides were based upon the Objects, Abstraction, Data Structures and Design using Java Version 5.0 Chapter 2 PowerPoint presentation by Elliot B. Koffman and Paul A. T. Wolfgang www.ics.mq.edu.au/ppdp