140 likes | 283 Vues
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.
E N D
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: boolIsEven (int x); is supposed to determine if x is an even integer Spec: boolIsEven (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; inti, j); is supposed to swap the values of a[i] and a[j] Spec: void Swap (ItemsType a; inti, 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: intExp(int x; int n); is supposed to return x to power of n Spec: intExp(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: intHmmm (int x; int y) { if (x – y <= 0) return x ; else return y; } Spec: intHmmm (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 ( ); //Requires: //Ensures: //Checks:
Transformer Methods //transformers void AddStudentToRoster (name, SSN); //Requires: SSN is not in roster and roster is not full //Ensures: //Checks: void DropStudentFromRoster (SSN); //Requires: //Ensures: //Checks:
Selector Methods //selectors (observers or accessors) Student GetNthStudentOnRoster (n); //Requires: //Ensures: //Checks: intGetNumberofStudentsOnRoster ( ); //Requires: //Ensures: //Checks:
Predicates (return bool) //predicates boolIsStudentOnRoster (name); //Requires: //Ensures: returns true iff name is on roster //Checks: boolIsRosterFull ( ); //Requires: //Ensures: //Checks: