Slide 1Preventing Invalid BMIHow Should We Change the Class?Checking PreconditionsJava Assertions/Pre(Post)ConditionsAssertionsCompile time vs. runtime propertiesApplication-independent vs. dependentWhy Language SupportError vs. ExceptionAssertion UsesPreconditions ConventionsObjectEditor Uses PreconditionsObjectEditor Uses PreconditionObjectEditor Uses PreconditionNew ClassPreconditions of Other MethodsPreconditions of Other MethodsEquivalent ClassPrecondition Style RuleThe 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 EarnestExpressing 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 Dewan2PREVENTING INVALID BMI3HOW 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); }}4CHECKING 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);}}5JAVA 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/postcondition6ASSERTIONSDeclare some property of the program Before getBMI() is called, height and weight should be greater than 07COMPILE TIME VS. RUNTIME PROPERTIESSome assertions are language-supportedCompile timeString s = nextElement()Runtime((String) nextElement())We will consider runtime properties.Casting is application-independent.8APPLICATION-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 added9WHY LANGUAGE SUPPORTCan always define a library with assert(<Boolean Expression>) method that throws a special exception denoting assertion error.Assertions can be dynamically turned on or off for package or Classjava –ea assignment11.MainClass –da bus.uigen… public void myAssert (boolean boolExp, String message) throws AssertionError { if (boolExp) throw new AssertionError (message); }10ERROR VS. EXCEPTIONJava assertion failure results in AssertionErrorSubclass of Error rather than RunTimeExceptionReasoning: Convention dictates that Exception should be caughtShould “discourage programmers from attempting to recover from assertion failures.”Might do custom reporting, mail error report etc.AssertionError is a subclass of Throwable and can indeed be caughtDecision was controversial11ASSERTION USESPotentially useful forspecificationtestingformal correctness documentationuser-interface automation12PRECONDITIONS CONVENTIONSpublic 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.13OBJECTEDITOR USES PRECONDITIONS14OBJECTEDITOR USES PRECONDITION15OBJECTEDITOR USES PRECONDITIONThe menu item for a method is disabled when its precondition not met16public class ABMISpreadsheet {double height, weight;double initialHeight, initialWeight;public ABMISpreadsheet(double theInitialHeight, double theInitialWeight) {setHeight ( theInitialHeight);setWeight(
View Full Document