A software specification indicates the task (or some aspect of the task) that is supposed to be performed when software executes. Types of Specifications Class Diagrams Object Diagrams Activity Diagrams (control flow diagrams) Assertions ( ) Others An assertion is a statement of fact that is presumed true relative to a code location(s). Example /** assert: str is a String and str.length > 2 */ assert str instanceof String && str.length > 2;! char firstchar, secondchar, bigchar; firstchar = str.charat(0); secondchar = str.charat(1); if (firstchar > secondchar) { bigchar = firstchar; else { bigchar = secondchar; /** assert: * str.length > 2 * and (str.charat(0) str.charat(1) * implies bigchar == str.charat(0)) * and (str.charat(0) str.charat(1) * implies bigchar == str.charat(1)) */ 1
Assertion Notation Assertions are based on logic and certain program notations (i.e., variable references and possibly non-void method calls). Assertions should NOT contain action verbs. Logical Operators not SubAssertion1! SubAssertion1 and SubAssertion2! SubAssertion1 or SubAssertion2! SubAssertion1 implies SubAssertion2! The subassertion must be false. Both subassertions must be true. One or both subassertion is true. When the first subassertion is true, the second must also be true. Assertion Notation Another logical notation, known as quantification, permits expressing assertions! about data structures. Form (universal quantification)! forall(type var : boundarycondition SubAssertion ) Example forall(integer j : 0 j 3 arr[j] > 0 ) Form (existential quantification)! exists(type var : boundarycondition SubAssertion ) Example exists(integer j : 0 j 3 arr[j] == 7 ) 2
Quantification Examples Assume two arrays of double: arr1 and arr2 and arr1.length == arr2.length == 5! forall (Integer r : 0 r 3 arr1[r] < arr1[r+1] ) forall (w : 0 w 4 arr1[w] == arr2[w] ) exists (k : 0 k 4 arr1[k] == 33 and arr2[k] == 33 ) exists (k : 0 k 4 ( arr1[k] < 0 and forall (j : k < j 4 arr2[k] == arr1[j]) ) ) forall (j,k : 0 j,k 4 and j!= k arr1[j]!= arr2[k] ) Where to place Assertions? An assertion can be located anywhere within executable instructions.! However, some locations have been found most effective:! Class Invariant! Method Precondition! Method Postcondition! Loop Invariant! 3
Example /** class invariant * distanceinmiles > 0 and timeinseconds > 0 */ public class LapTime { private double distanceinmiles, timeinseconds; /** pre: d > 0 and t > 0 * post: distanceinmiles == d and timeinseconds == t */ public LapTime(double d, double t) { distanceinmiles = d; timeinseconds = t; When is each assertion presumed to be true?! /** post: distanceinmiles == 60 * and timeinseconds == 3600 */ public void setto60mph() { distanceinmiles = 60; timeinseconds = 3600; // more methods on later slides Special Postcondition Notations Return value (result)! // Within LapTime class /** post: result == distanceinmiles / (timeinseconds*3600) */ public double milesperhour() { double velocity; velocity = distanceinmiles/(timeinseconds*60*60); return velocity Previous value (@pre)! // Within LapTime class /** post: distanceinmiles == distanceinmiles@pre * 2 */ public void doublethemileage() { distanceinmiles = distanceinmiles * 2; 4
Method Contract Method caller guarantees... precondition & class invariant (at time of method call) Method is required to ensure... postcondition & class invariant (at time of method return) Addendum: A modifies clause can stipulate what alterations are permitted Example: the standard Color class.! java.awt.color - int redness - int blueness - int greenness - int opaqueness «alpha» «constructor» + Color(int r, int g, int b) + Color(float r, float g, float b, float a) «query» + int getred() + Color darker() + Color brighter()... What does this class diagram tell you?! What doesn t it tell you?! Using method contracts, fills in more design details.! 5
java.awt.color Class Specifications Invariant (for every Color object) 0 redness 255 and 0 greenness 255 and 0 blueness 255 and 0 opaqueness 255 Constructor Methods public Color(int r, int g, int b) pre: 0 r 255 and 0 g 255 and 0 b 255 (throws IllegalArgumentException) modifies: redness, greenness, blueness, opaqueness post: redness == r and greenness == g and blueness == b and opaqueness == 255 public Color(float r, float g, float b, float a) pre: 0.0 r 1.0 and 0.0 g 1.0 and 0.0 b 1. 0 and 0.0 a 1.0 (throws IllegalArgumentException) post: redness == r*255 and greenness == g*255 and blueness == b*255 and opaqueness == a*255 java.awt.color Class Specifications (continued) Query Methods public int getred() post: result == redness public Color darker() post: result.redness == redness * 0.7 and result. greenness == greenness * 0.7 and result. blueness == blueness * 0.7 and result. opaqueness == 255 public Color brighter() post: (redness / 0.7) > 255 implies result.redness == 255 and (redness / 0.7) 255 implies result.redness == redness / 0.7 and (greenness / 0.7) > 255 implies result. greenness == 255 and (greenness / 0.7) 255 implies result. greenness == greenness / 0.7 and (blueness / 0.7) > 255 implies result. blueness == 255 and (blueness / 0.7) 255 implies result. blueness == blueness / 0.7 and result. opaqueness == 255... 6
The Sun Documentation Color public Color(int r, int g, int b) Creates an opaque srgb color with the specified red, green, and blue values in the range (0-255). The actual color used in rendering depends on finding the best match given the color space available for a given output device. Alpha is defaulted to 255. Parameters: r - the red component g - the green component b - the blue component Throws: IllegalArgumentException - if r, g or b are outside of the range 0 to 255, inclusive See Also: getred(), getgreen(), getblue(), getrgb() brighter More Sun Documentation public Color brighter()int r, Creates a new Color that is a brighter version of this Color. This method applies an arbitrary scale factor to each of the three RGB components of this Color to create a brighter version of this Color. Although brighter and darker are inverse operations, the results of a series of invocations of these two methods might be inconsistent because of rounding errors. Returns: a new Color object that is a brighter version of this Color. Since: JDK1.0 See Also: darker() 7