1 / 14

Specifications

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.

edan
Télécharger la présentation

Specifications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

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

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

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

  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 ( ); //Requires: //Ensures: //Checks:

  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: //Ensures: //Checks:

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

  14. Predicates (return bool) //predicates boolIsStudentOnRoster (name); //Requires: //Ensures: returns true iff name is on roster //Checks: boolIsRosterFull ( ); //Requires: //Ensures: //Checks:

More Related