Itec200 week02
1 / 21

ITEC200 Week02 - PowerPoint PPT Presentation

  • Uploaded on
  • Presentation posted in: General

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

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

  • 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

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

The Exception class hierarchy

Methods inherited from Throwable

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

Checked and Unchecked Exceptions

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

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

Catching Exceptions Example

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

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


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

Assertions and Loop Invariants Example

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

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)

Efficiency of Algorithms (continued)

  • We can reduce the expression in parentheses to:

    n x (n – 1)


  • So, T(n) = 1.5n2 – 1.5n

  • We can therefore conclude that T(n) is O(n2)

Efficiency of Algorithms (continued)

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


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

  • Login