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

Slides:



Advertisements
Similar presentations
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Advertisements

ITEC200 Week04 Lists and the Collection Interface.
Simulation and Example
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 8 Exceptions and Assertions.
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
© The McGraw-Hill Companies, 2006 Chapter 15. © The McGraw-Hill Companies, 2006 Exceptions an exception is an event that occurs during the life of a program.
 2005 Pearson Education, Inc. All rights reserved Exception Handling.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Chapter 1. The Phases of Software Development. Data Structure 2 Chapter outline  Objectives  Use Javadoc to write a method’s complete specification.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
ISBN Chapter 3 Describing Syntax and Semantics.
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
ITEC200 – Week03 Inheritance and Class Hierarchies.
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
ITEC200 Week01 Introduction to Software Design.
ITEC200 Week06 Queues. 2 Learning Objectives – Week06 Queues (Ch6) Students can Manage data using the queue Abstract Data Type.
ITEC200 Week05 Stacks. 2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface.
Program Correctness and Efficiency Chapter 2. Chapter 2: Program Correctness and Efficiency2 Chapter Objectives To understand the differences between.
Sets and Maps ITEC200 – Week Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Chapter 8 Exceptions. Topics Errors and Exceptions try-catch throwing Exceptions Exception propagation Assertions.
Chapter 2: Algorithm Discovery and Design
Program Correctness and Efficiency Chapter 2. Chapter Objectives  To understand the differences between the three categories of program errors  To understand.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
OOP #10: Correctness Fritz Henglein. Wrap-up: Types A type is a collection of objects with common behavior (operations and properties). (Abstract) types.
Introduction to Software Design Chapter 1. Chapter 1: Introduction to Software Design2 Chapter Objectives Intro - Software OOP Inheritance, interfaces,
Chapter 1 Principles of Programming and Software Engineering.
Describing Syntax and Semantics
Chapter 10 Classes Continued
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
Advanced Java Course Exception Handling. Throwables Class Throwable has two subclasses: –Error So bad that you never even think about trying to catch.
Program Correctness and Efficiency Chapter 2 Chapter 2: Program Correctness and Efficiency2 Chapter Objectives To understand the differences between.
Fall 2007CS 225 Program Correctness and Efficiency Chapter 2.
Java Software Solutions Foundations of Program Design Sixth Edition
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Object Oriented Programming
Chapter 12: Exception Handling
Slides Credit Umair Javed LUMS Web Application Development.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
Computer Concepts 2014 Chapter 12 Computer Programming.
Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --
Introduction to Exception Handling and Defensive Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
Exceptions and Assertions Chapter 15 – CSCI 1302.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
CIS 068 Welcome to CIS 068 ! Lesson 3: Algorithm Correctness And Efficiency.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
Principles of Programming & Software Engineering
Principles of Programming and Software Engineering
Computer Programming.
Efficiency (Chapter 2).
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Exception Handling Chapter 9 Edited by JJ.
Algorithm Correctness
Program Correctness and Efficiency
Exception Handling.
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 product that facilitates writing test cases, organizing the test cases into test suites, running the test suites, and reporting the results

Debugging

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) 2 So, T(n) = 1.5n 2 – 1.5n We can therefore conclude that T(n) is O(n 2 )

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

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