Chapter 7 Programming by contract: preconditions and postconditions.

Slides:



Advertisements
Similar presentations
Chapter 5 Implementing a simple class. This chapter discusses n Implementing class definitions. n How to store data in an object and how to write method.
Advertisements

Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Chapter 13 Abstraction and inheritance. This chapter discusses n Implementing abstraction. u extension u inheritance n Polymorphism/dynamic binding. n.
Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.
Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Chapter 6 Conditions. This chapter discusses n Conditions and conditional statements. n Preconditions, postconditions, and class invariants. n Boolean.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
An Introduction to Programming and Object Oriented Design using Java 2 nd Edition. May 2004 Jaime Niño Frederick Hosch Chapter 5 : Programming By Contract.
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
1 Software Testing and Quality Assurance Lecture 21 – Class Testing Basics (Chapter 5, A Practical Guide to Testing Object- Oriented Software)
Road Map Introduction to object oriented programming. Classes
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
Software Testing and Quality Assurance
OOP #10: Correctness Fritz Henglein. Wrap-up: Types A type is a collection of objects with common behavior (operations and properties). (Abstract) types.
Information Hiding and Encapsulation
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 10 Classes Continued
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
An Introduction to Programming and Object Oriented Design using Java 2 nd Edition. May 2004 Jaime Niño Frederick Hosch Chapter 5 : Programming By Contract.
Computer Science 340 Software Design & Testing Design By Contract.
Procedure specifications CSE 331. Outline Satisfying a specification; substitutability Stronger and weaker specifications - Comparing by hand - Comparing.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
Contract based programming Using pre- and post-conditions, and object invariants Contract based programming1.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
CS 261 – Data Structures Preconditions, Postconditions & Assert.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 4 Specification of a simple class. This chapter discusses n How to write the specifications for a class. u The precise description of features.
Chapter 9 Putting together a complete system. This chapter discusses n Designing a complete system. n Overview of the design and implementation process.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
CSC 205 Java Programming II Defining & Implementing Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Data Structures Using C++ 2E
Object Oriented Programing (OOP)
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Chapter 8: Designing Classes Accessors, Mutators, and Immutable Classes(8.3) Side Effects(8.4) Parameter Passing (Advanced Topic 8.1) Preconditions and.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
Defensive Programming CNS 3370 Copyright 2003, Fresh Sources, Inc.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Classes - Intermediate
Data Structures Using Java1 Chapter 1 Software Engineering Principles and Java Classes.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
Interfaces CMSC 202. Public Interfaces Objects define their interaction with the outside world through the their public interface. A class' public interface.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Topics: jGRASP editor ideosyncrasies assert debugger.
Chapter 4: Writing Classes
Chapter 4 : Conditionals
PRACTICE OVERVIEW PL/SQL Part - 2.
Defining Your Own Classes
Chapter 6 Conditions.
User-Defined Classes and ADTs
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
Implementing a simple class
CSE 1020:Software Development
slides created by Ethan Apter and Marty Stepp
Chapter 5 Classes.
Presentation transcript:

Chapter 7 Programming by contract: preconditions and postconditions

This chapter discusses n An elaboration of precondition and postcondition. n Programming by contract. n Test plans.

Programming by contract n A programming style in which the invocation of a method is viewed as a contract between client and server, with each having explicitly stated responsibilities. n To imply a precondition, we will use the term ‘require:’. n To imply a postcondition, we will use the term ‘ensure:’.

The objective of programming by contract n Delineate, clearly and explicitly, responsibilities between client and server. n Delineate, clearly and explicitly, responsibilities between the user of a method and the implementor of the method. n Ensure that any possible run time error will be detected with minimal explicit error checking. n Unfortunately, Java requires all checking to be coded explicitly as part of the method implementation.

The objective of programming by contract (cont.) n Test for every possible error condition only once (for program efficiency). n Achieve a balance between program reliability and maintainability.

The contract n If the preconditions are satisfied, then the server guarantees that the postconditions will be satisfied when the method completes. n If the preconditions are not satisfied, that is, if the client does not meet his end of the contract, then the server promises nothing.

Explorer class n Constructor: u require: hitStrength >= 0 stamina >= 0 u ensure: this.name() == name this.location() == location this.strength() == strength this.stamina() == stamina

Explorer class (cont.) Method takeHit : u require: hitStrength >= 0 u ensure: this.stamina() <= old.stamina() n “old” refers to the value of the variable at the beginning of the method; it is not a Java construct.

What if preconditions are not met? n We could set the value to a default value if it doesn’t meet criteria. But this isn’t entirely satisfactory, since it treats an error condition as a normal, expected, occurrence. n Usually an error is returned if the preconditions are not met.  CSCI.utilities package contains functionality for doing this.

Require class Include the following statement immediately after the package statement. import CSCI.utilities.*; Now you can use the method condition in the Require class. static public void condition (boolean precondition)

Require in Explorer method public Explorer (String name, rooms.Room location, int hitStrength, int stamina) { Require.condition(hitStrength >= 0); Require.condition(stamina >= 0); playerName = name; room = location; strengthPoints = hitStrength; staminaPoints = stamina; }

More on preconditions: n Occasionally, preconditions constrain the order in which methods can be invoked or require that an object be in a certain state before a given method can be invoked. n Example: an automobile must be running before it can move.

More on postconditions: n Query postconditions: u Queries do not change objects’ states. u Query postconditions simply say something about the value returned. n Command postconditions: u Commands change states. u Command postconditions describe the new state after execution of the command. n Constructor postconditions: u describe the initial state of the object.

Part of the specification: n Since preconditions and postconditions are part of the specifications, they should not mention private implementation components. n The reset method. ensure: tally == 0 (This is not correct!)

Named constants n Use named constants rather than literals in preconditions and postconditions.  public int suit ( ) ensure: result == Card.CLUB || result == Card.DIAMOND || result == Card.HEART || result == Card.SPADE.

Creating a test plan n Test the implementation to insure that it conforms to the specifications. n Create a “test harness” to interact with the object. n The test system acts as the client of the object: u Invoke the object’s methods. u Examine the behavior of the object.

Testing stamina Provide an initial value for the object’s stamina. Invoke the takeHit method repeatedly. After each invocation, query the object with the method stamina and report the result.

Testing stamina (cont.) Sample test session: Initial stamina value: 100 Stamina is now 100. Strength of hit: 10 Stamina is now 90. Strength of hit: 20 Stamina is now 70. Stength of hit: 5 Stamina is now 65. …

Creating a test plan n Develop a test plan, giving values to be tested, the purpose of the test and the individual test cases, and the expected results. n Test the system thoroughly, but not inordinately. Explicitly include limiting values and “equivalence classes.” u If the system performs properly for one member of an “equivalence class,” it should perform properly for all members of the “equivalence class.”

Testing stamina Purpose: test the stamina property of a Explorer object, as modified by the method takeHit. n Preconditions:  Initial value of stamina >=0  hitStrength argument to takeHit >=0 n Postconditions:  stamina >=0

Testing stamina (cont.) Test run 1: Input Initial stamina value: 100 hitStrength : Expected Output stamina :

Testing stamina (cont.) Test run 2: Input Initial stamina value: 50 hitStrength : 100 Test run 3: Input Initial stamina value 0 hitStrength 10 Expected Output stamina : 50 0 Expected Output stamina : 0

We’ve covered n Programming by contract. u The client satisfying the precondition. u The server satisfying the postcondition. n Test plans.

Glossary