P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department.

Slides:



Advertisements
Similar presentations
Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Advertisements

CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach to the.
Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.
Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
L l One of the tree applications in Chapter 9 is binary search trees. l l In Chapter 9, binary search trees are used to implement bags and sets. l l This.
 One of the tree applications in Chapter 10 is binary search trees.  In Chapter 10, binary search trees are used to implement bags and sets.  This presentation.
CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.
 Chapter 2 introduces Object Oriented Programming.  OOP is a relatively new approach to programming which supports the creation of new data types and.
 Chapter 11 has several programming projects, including a project that uses heaps.  This presentation shows you what a heap is, and demonstrates two.
CSC212 Data Structure - Section AB Lecture 20 Hashing Instructor: Edgardo Molina Department of Computer Science City College of New York.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
P Chapter 6 introduces the stack data type. p Several example applications of stacks are given in that chapter. p This presentation shows another use called.
  Chapter 12 presents several common algorithms for sorting an array of integers.   Two slow but simple algorithms are Selectionsort and Insertionsort.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
Zhigang Zhu, CSC212 Data Structures - Section FG Lecture 1: Introduction Instructor: Zhigang Zhu Department of Computer Science City College.
CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York.
 Chapter 10 introduces trees.  This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
CSC212 Data Structure - Section FG Lecture 9 Linked Lists Instructor: Zhigang Zhu Department of Computer Science City College of New York.
Copyright © 1999, Carnegie Mellon. All Rights Reserved. Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple.
L l Chapter 11 discusses several ways of storing information in an array, and later searching for the information. l l Hash tables are a common approach.
 An important topic: preconditions and postconditions.  They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked.
 Chapter 9 introduces the technique of recursive programming.  As you have seen, recursive programming involves spotting smaller occurrences of a problem.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
CSC212 Data Structure - Section FG Lecture 12 Stacks and Queues Instructor: Zhigang Zhu Department of Computer Science City College of New York.
 Chapter 8 introduces the queue data type.  Several example applications of queues are given in that chapter.  This presentation describes the queue.
Edgardo Molina, CSC212 Data Structures - Section AB Lecture 1: Introduction Instructor: Edgardo Molina Department of Computer Science City College.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Xiaoyan Li, CSC211 Data Structures Lecture 1: Introduction Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
CS Fall 2007 Dr. Barbara Boucher Owens. CS 2 Text –Main, Michael. Data Structures & Other Objects in Java Third Edition Objectives –Master building.
Data Structures Chapter 1- Introduction Mohamed Mustaq.A.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
CSC211 Data Structures Lecture 20 Hashing Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
 An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions.
Software Development p Data structure p A collection of data organized so that the data can be accessed using a set of specific techniques p Object-oriented.
CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
P p Chapter 10 introduces trees. p p This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
Container Classes  A container class is a data type that is capable of holding a collection of items.  In C++, container classes can be implemented as.
CSC212 Data Structure - Section AB Lecture 13 Recursive Thinking Instructor: Edgardo Molina Department of Computer Science City College of New York.
CSC212 Data Structure Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
Xiaoyan Li CSC211 Data Structures Lecture 2 ADT and C++ Classes (I) Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 2 ADT and C++ Classes (I) Instructor: Zhigang Zhu Department of Computer Science City.
1 Introduction 1. Why Data Structures? 2. What AreData Structure? 3. Phases of Software Development 4. Precondition and Postcondition 5. Examples.
George Wolberg, CSC212 Data Structures Lecture 1: Introduction Instructor: George Wolberg Department of Computer Science City College of New York.
 Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes.  This presentation shows how.
Chapter 1 The Phases of Software Development. Software Development Phases ● Specification of the task ● Design of a solution ● Implementation of solution.
Chapter 1 The Phases of Software Development
Welcome to CSIS 10B Overview of Course Software Design (from Nyhoff)
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Chapter 1 The Phases of Software Development
Collection Classes A Collection class is a data type that is capable of holding a group of items. In Java, Collection classes can be implemented as a class,
Using a Stack Chapter 6 introduces the stack data type.
Preconditions, Postconditions & Assertions
Quadratic Sorting Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Preconditions and Postconditions
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Preconditions and Postconditions
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Presentation transcript:

p An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department of Applied Computer Science ACS 169 Data Structures and Other Objects Using C++

Preconditions and Postconditions Frequently a programmer must communicate precisely what a function accomplishes, without any indication of how the function does its work. Can you think of a situation where this would occur ?

Example p You are the head of a programming team and you want one of your programmers to write a function for part of a project. HERE ARE THE REQUIREMENTS FOR A FUNCTION THAT I WANT YOU TO WRITE. I DON'T CARE WHAT METHOD THE FUNCTION USES, AS LONG AS THESE REQUIREMENTS ARE MET.

What are Preconditions and Postconditions? p One way to specify such requirements is with a pair of statements about the function. p The precondition statement indicates what must be true before the function is called. p The postcondition statement indicates what will be true when the function finishes its work.

Example void write_sqrt( double x) // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output....

Example void write_sqrt( double x) // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output.... } p The precondition and postcondition appear as comments in your program. p They are usually placed after the function’s parameter list.

Example void write_sqrt( double x) // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output.... } p In this example, the precondition requires that x >= 0 x >= 0 be true whenever the function is called. be true whenever the function is called.

Example write_sqrt( -10 ); write_sqrt( 0 ); write_sqrt( 5.6 ); Which of these function calls meet the precondition ?

Example Which of these function calls meet the precondition ? The second and third calls are fine, since the argument is greater than or equal to zero. write_sqrt( -10 ); write_sqrt( 0 ); write_sqrt( 5.6 );

Example Which of these function calls meet the precondition ? But the first call violates the precondition, since the argument is less than zero. write_sqrt( -10 ); write_sqrt( 0 ); write_sqrt( 5.6 );

Example void write_sqrt( double x) // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output.... }  The postcondition always indicates what work the function has accomplished. In this case, when the function returns the square root of x has been written.

Another Example bool is_vowel( char letter ) // Precondition: letter is an uppercase or // lowercase letter (in the range 'A'... 'Z' or 'a'... 'z'). // Postcondition: The value returned by the // function is true if Letter is a vowel; // otherwise the value returned by the function is // false....

Another Example is_vowel( 'A' ); is_vowel(' Z' ); is_vowel( '?' ); What values will be returned by these function calls ?

Another Example is_vowel( 'A' ); is_vowel(' Z' ); is_vowel( '?' ); What values will be returned by these function calls ? true false Nobody knows, because the precondition has been violated.

Another Example is_vowel( '?' ); What values will be returned by these function calls ? Violating the precondition might even crash the computer.

Always make sure the precondition is valid... p The programmer who calls the function is responsible for ensuring that the precondition is valid when the function is called. AT THIS POINT, MY PROGRAM CALLS YOUR FUNCTION, AND I MAKE SURE THAT THE PRECONDITION IS VALID.

... so the postcondition becomes true at the function’s end. p The programmer who writes the function counts on the precondition being valid, and ensures that the postcondition becomes true at the function’s end. THEN MY FUNCTION WILL EXECUTE, AND WHEN IT IS DONE, THE POSTCONDITION WILL BE TRUE. I GUARANTEE IT.

On the other hand, careful programmers also follow these rules: p When you write a function, you should make every effort to detect when a precondition has been violated. p If you detect that a precondition has been violated, then print an error message and halt the program.

On the other hand, careful programmers also follow these rules: p When you write a function, you should make every effort to detect when a precondition has been violated. p If you detect that a precondition has been violated, then print an error message and halt the program... p...rather than causing a disaster. a disaster.

Example void write_sqrt( double x) // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output. { assert(x >= 0);... p The assert function (described in Section 1.1) is useful for detecting violations of a precondition.

Advantages of Using Preconditions and Postconditions p Succinctly describes the behavior of a function... p... without cluttering up your thinking with details of how the function works. p At a later point, you may reimplement the function in a new way... p... but programs (which only depend on the precondition/postcondition) will still work with no changes.

Precondition p The programmer who calls a function ensures that the precondition is valid. p The programmer who writes a function can bank on the precondition being true when the function begins execution. Postcondition p The programmer who writes a function ensures that the postcondition is true when the function finishes executing. Summary

T HE E ND Presentation copyright 1997, Addison Wesley Longman For use with Data Structures and Other Objects Using C++ by Michael Main and Walter Savitch. Some artwork in the presentation is used with permission from Presentation Task Force (copyright New Vision Technologies Inc.) and Corel Gallery Clipart Catalog (copyright Corel Corporation, 3G Graphics Inc., Archive Arts, Cartesia Software, Image Club Graphics Inc., One Mile Up Inc., TechPool Studios, Totem Graphics Inc.). Students and instructors who use Data Structures and Other Objects Using C++ are welcome to use this presentation however they see fit, so long as this copyright notice remains intact.