1 / 21

ITEC200 Week02

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

Download Presentation

ITEC200 Week02

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ITEC200 Week02 Program Correctness and Efficiency www.ics.mq.edu.au/ppdp

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

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

  4. The Exception class hierarchy www.ics.mq.edu.au/ppdp

  5. Methods inherited from Throwable www.ics.mq.edu.au/ppdp

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

  7. Checked and Unchecked Exceptions www.ics.mq.edu.au/ppdp

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

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

  10. Catching Exceptions Example www.ics.mq.edu.au/ppdp

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

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

  13. Debugging www.ics.mq.edu.au/ppdp

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

  15. Assertions and Loop Invariants Example www.ics.mq.edu.au/ppdp

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

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

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

  19. Efficiency of Algorithms (continued) www.ics.mq.edu.au/ppdp

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

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

More Related