Presentation is loading. Please wait.

Presentation is loading. Please wait.

Specifications What? Not how!.

Similar presentations


Presentation on theme: "Specifications What? Not how!."— Presentation transcript:

1 Specifications What? Not how!

2 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.

3 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

4 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:

5 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

6 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

7 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

8 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

9 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 :

10 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:

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

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

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

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


Download ppt "Specifications What? Not how!."

Similar presentations


Ads by Google