Slide 1AssertionsCompile time vs. runtime propertiesApplication-independent vs. dependentJava Assertions/Pre(Post)ConditionsPreventing Invalid BMIHow Should We Change the Class?Checking PreconditionsObjectEditor Uses PreconditionsObjectEditor Uses PreconditionObjectEditor Uses PreconditionNew ClassPreconditions of Other MethodsPreconditions of Other MethodsEquivalent ClassPrecondition Style RuleExpressing AssertionsPropositional CalculusPropositional AlgebraExample PropositionsQuantified PropositionsQuantified AssertionsExpressing Quantified AssertionsSeparate the Three ComponentsList Based DomainIterating the DomainAsserter ClassDescribing the Domain Using Method ParametersDescribing the Domain Using Method ParametersSubproposition Visitor ObjectsAccessing External VarsSubproposition accessing vars other than domain elementsActionObjectCommand ObjectVisitor ObjectExample of Visitor PatternVisitor PatternEveryday Visitor ObjectsVisitor PatternAsserting falseNested AssertionsActual LibraryJava AssertionsWhy Language SupportError vs. ExceptionThe Importance of Being EarnestThe Importance of Being EarnestThe Importance of Being EarnestThe Importance of Being EarnestThe Importance of Being EarnestThe Importance of Being EarnestThe Importance of Being EarnestThe Importance of Being EarnestThe Importance of Being EarnestExtra SlidesProblem with inaccessible variablesComplete AsserterAsserter ClassGoalGoalProblem with inaccessible variablesHow to describe domain?Describing the DomainDescribing the Domain Using Method ParametersDescribing the DomainDescribing the domainHow to describe SubpropositionSubproposition as a functionHow to describe SubpropositionDescribing the subpropositionGoalCalls vs. CallbacksSubproposition accessing vars other than domain elementsSubproposition accessing vars other than domain elementsVisitor PatternActionObjectCommand ObjectVisitor ObjectVisitor PatternExample of Visitor PatternNested AssertionsExample of Pattern in Everyday ApplicationsSubproposition accessing vars other than domain elementsCheck MethodCOMP 401ASSERTIONSInstructor: Prasun Dewan2ASSERTIONSDeclare some property of the program Potentially useful forspecificationtestingformal correctness documentationuser-interface automation3COMPILE TIME VS. RUNTIME PROPERTIESSome assertions are language-supportedCompile timeString s = nextElement()Runtime((String) nextElement())We will consider runtime properties.Casting is application-independent.4APPLICATION-INDEPENDENT VS. DEPENDENTLanguage can provide us with fixed number of application-independent assertions.Cannot handleFirst character of String is a letter.Letter concept not burnt into language.Class Character defines itInnumerable assertions about letters possibleSecond elements of string is letter.Third element of string is letter.Need mechanism to express arbitrary assertions.Originally Java had no assertions.In 1.4, assertions were added5JAVA ASSERTIONS/PRE(POST)CONDITIONSassert <Boolean Expression>assert <Boolean Expression>: <Value> Statement can be inserted anywhere to state that some condition should be trueIf condition is false, Java throws AssertionError, which may be caught by programmer code.If uncaught, depending on which assert used:generic message saying assertion failed printed<Value>.toString() printed An assertion made at the beginning/end of a statement block (method, loop, if ..) is called its precondition/postcondition6PREVENTING INVALID BMI7HOW SHOULD WE CHANGE THE CLASS?public class ABMISpreadsheet {double height, weight;public ABMISpreadsheet(double theInitialHeight, double theInitialWeight) {setHeight ( theInitialHeight);setWeight( theInitialWeight);}public double getHeight() { return height; }public void setHeight(double newHeight) { height = newHeight; }public double getWeight() { return weight; }public void setWeight(double newWeight) { weight = newWeight; }public double getBMI() { return weight/(height*height); }}8CHECKING PRECONDITIONSpublic class ABMISpreadsheet {double height, weight;public ABMISpreadsheet(double theInitialHeight, double theInitialWeight) {setHeight ( theInitialHeight);setWeight( theInitialWeight);}…public boolean preGetBMI() { return weight > 0 && height > 0;}public double getBMI() {assert (preGetBMI());return weight/(height*height);}}Precondition of method M() is preM()ObjectEditor does not call M() if preM() is false.9OBJECTEDITOR USES PRECONDITIONS10OBJECTEDITOR USES PRECONDITION11OBJECTEDITOR USES PRECONDITIONThe menu item for a method is disabled when its precondition not met12public class ABMISpreadsheet {double height, weight;double initialHeight, initialWeight;public ABMISpreadsheet(double theInitialHeight, double theInitialWeight) {setHeight ( theInitialHeight);setWeight( theInitialWeight);initialHeight = theInitialHeight;initialWeight = theInitialWeight;}…public boolean preGetBMI() { return weight > 0 && height > 0; }public double getBMI() {assert preGetBMI(); return weight/(height*height); }public boolean preRestoreHeightAndWeight() {return height != initialHeight || weight != initialWeight; }public void restoreHeightAndWeight() {assert preRestoreHeightAndWeight();height = initialHeight;weight = initialWeight;}}NEW CLASS13public class ABMISpreadsheet {…public double getWeight() {return weight;}public void setWeight(double newWeight) {weight = newWeight;}…}PRECONDITIONS OF OTHER METHODS14public class ABMISpreadsheet {…public double preGetWeight() {return weight > 0;}public double getWeight() {assert preGetWeight();return weight;}public boolean preSetWeight (double newWeight) {return newWeight > 0;}public void setWeight(double newWeight) {assert preSetWeight(newWeight);weight = newWeight;} …}PRECONDITIONS OF OTHER METHODSPrevention of getter not needed if setter and constructor prevent assignment of illegal values15EQUIVALENT CLASSpublic class ABMISpreadsheet {…public double getWeight() {return weight;}public boolean preSetWeight (double newWeight) {return newWeight > 0;}public void setWeight(double newWeight) {assert preSetWeight(newWeight);weight = newWeight;} …}Prevention of getter not needed if setter and constructor prevent assignment of illegal values16PRECONDITION STYLE RULEIf there are constraints on the input of a method M(…) that may not be met, write a precondition boolean method, preM(…) for it.Call the precondition method in an assert statement as the
View Full Document