Specifications What? Not how!.

Slides:



Advertisements
Similar presentations
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Advertisements

Code Documentation. Important!  It’s for you  It’s for others.
Chapter 1. The Phases of Software Development. Data Structure 2 Chapter outline  Objectives  Use Javadoc to write a method’s complete specification.
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
1 Heaps & Priority Queues (Walls & Mirrors - Remainder of Chapter 11)
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
 An important topic: preconditions and postconditions.  They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
Object Specification Moving towards a more precise specification of software.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
Data Structures Chapter 2 Stacks Andreas Savva. 2 Stacks A stack is a data structure in which all insertions and deletions of entries are made at one.
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.
Object Oriented Data Structures
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
CS Fall 2007 Dr. Barbara Boucher Owens. CS 2 Text –Main, Michael. Data Structures & Other Objects in Java Third Edition Objectives –Master building.
TECH Computer Science Data Abstraction and Basic Data Structures Improving efficiency by building better  Data Structure Object IN  Abstract Data Type.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
111 Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract,
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
CS 261 – Data Structures Preconditions, Postconditions & Assert.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department.
Chapter 5: Sequences, Mathematical Induction, and Recursion 5.5 Application: Correctness of Algorithms 1 [P]rogramming reliability – must be an activity.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
Protocols Software Engineering II Wirfs Brock et al, Designing Object-Oriented Software, Prentice Hall, Mitchell, R., and McKim, Design by Contract,
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
1 Data Structures and Algorithms Stacks and Queues.
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
21. THE STANDARD LIBRARY. General Rules The C standard library is divided into 15 parts (24 in C99), with each part described by a header. The names of.
1 Introduction 1. Why Data Structures? 2. What AreData Structure? 3. Phases of Software Development 4. Precondition and Postcondition 5. Examples.
CSI 312 Dr. Yousef Qawqzeh Heaps and Priority Queue.
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
2.4 Exceptions n Detects try { //code that may raise an exception and/or set some condition if (condition) throw exceptionName; //Freq. A string } n Handles.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
Function Parameters and Overloading Version 1.0. Topics Call-by-value Call-by-reference Call-by-address Constant parameters Function overloading Default.
ALGORITHMS PROVING ALGORITHMS (PROGRAMS) CORRECT WITH AND WITHOUT INDUCTION.
C++ Lesson 1.
Basic concepts of C++ Presented by Prof. Satyajit De
CC 215 Data Structures Queue ADT
Bill Tucker Austin Community College COSC 1315
CC 215 Data Structures Stack ADT
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Preconditions, Postconditions & Assertions
Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract, Computer,
Pointers & Dynamic Data Structures
Jordi Cortadella and Jordi Petit Department of Computer Science
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Stack.
CS150 Introduction to Computer Science 1
Functions and Recursion
Priority Queue and Heap
Templates CMSC 202, Version 4/02.
Presentation transcript:

Specifications What? Not how!

Specifications About What, NOT How. Specify functions, classes and methods. Class specification - describe the common qualities that objects of a particular class have. Example: STACK – a stack is a linear sequence of items of the same type; items can only be added to the sequence by inserting them at the top and any item removed from the sequence is always the top item. An operation is associated with two conditions: a precondition and a postcondition.

Preconditions – specify what must be true prior to calling a function or methods. It is a statement of expected values for input parameters and the state of an object. Postconditions – specify what happens or what will be true after the called function or method is executed IF THE PRECONDITIONS ARE MET. A state of output values relative to expected input values. Checks – specify those preconditions that will be checked and what will be done if a precondition is not met

When declaring a function or a method, specify it by using comments: Put the preconditions in a comment //Requires: Put the post conditions in a comment //Ensures: Put the conditions checked in a comment //Checks:  

Examples Q: bool IsEven (int x); is supposed to determine if x is an even integer Spec: bool IsEven (int x); //Requires: none //Ensures: returns true iff x = 2n, where n is an // integer (otherwise returns false) //Checks: none

Q: Assume following type typedef char ItemsType [25]; void Swap (ItemsType a; int i, j); is supposed to swap the values of a[i] and a[j] Spec: void Swap (ItemsType a; int i, j); //Requires: //Ensures: // //Checks: //Requires: 0 <= i, j <= 24 //Ensures: a[i] = #a[j] and a[j] = #a[i], otherwise // a is unchanged //Checks: 0 <= i, j <= 24, write error message

Q: Assume above definition for ItemsType char Max (ItemsType a); is supposed to return the largest value in a   Spec: char Max (ItemsType a); //Requires: none //Ensures: returns a[i] where 0 <= I <= 24 and // a[i] >= a[0], a[1], … a[24] //Checks: none char Max (ItemsType a); //Requires: none //Ensures: returns a[i] where 0 <= I <= 24 and // a[i] >= a[0], a[1], … a[24] //Checks: none

Q: int Exp(int x; int n); is supposed to return x to power of n   Spec: int Exp(int x; int n); //Requires: x <= nth root of MAXINT //Ensures: returns x to the n //Checks: x <= nth root of MAXINT, writes error // message

FIGURE OUT YOURSELF!! Q: int Hmmm (int x; int y) { if (x – y <= 0) return x ; else return y; } Spec: int Hmmm (int x; int y) //Requires : //Returns : //Checks :

Specifying Methods and knowing Method Categories object: Roster – a list of student name and SSN pairs   methods: //constructors – default and copy constructor (parameterized is not useful here) Roster ( ); //Requires: //Ensures: //Checks:

Roster (Roster other); //Requires: //Ensures: //Checks: //destructor ~Roster ( );

Transformer Methods //transformers void AddStudentToRoster (name, SSN); //Requires: SSN is not in roster and roster is not full //Ensures: //Checks: void DropStudentFromRoster (SSN); //Requires:

Selector Methods //selectors (observers or accessors) Student GetNthStudentOnRoster (n); //Requires: //Ensures: //Checks:   int GetNumberofStudentsOnRoster ( );

Predicates (return bool) bool IsStudentOnRoster (name); //Requires: //Ensures: returns true iff name is on roster //Checks:   bool IsRosterFull ( ); //Ensures: