Unformatted text preview:

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)CONDITIONSassert <Boolean Expression>assert <Boolean Expression>: <Value> Statement can be inserted anywhere to state that some condition should be trueIf 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/postcondition6ASSERTIONSDeclare some property of the program Before getBMI() is called, height and weight should be greater than 07COMPILE TIME VS. RUNTIME PROPERTIESSome assertions are language-supportedCompile timeString s = nextElement()Runtime((String) nextElement())We will consider runtime properties.Casting is application-independent.8APPLICATION-INDEPENDENT VS. DEPENDENTLanguage can provide us with fixed number of application-independent assertions.Cannot handleFirst character of String is a letter.Letter concept not burnt into language.Class Character defines itInnumerable assertions about letters possibleSecond 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 SUPPORTCan 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 Classjava –ea assignment11.MainClass –da bus.uigen… public void myAssert (boolean boolExp, String message) throws AssertionError { if (boolExp) throw new AssertionError (message); }10ERROR VS. EXCEPTIONJava assertion failure results in AssertionErrorSubclass of Error rather than RunTimeExceptionReasoning: Convention dictates that Exception should be caughtShould “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 caughtDecision was controversial11ASSERTION USESPotentially useful forspecificationtestingformal correctness documentationuser-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

UNC-Chapel Hill COMP 114 - Assertions

Download Assertions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Assertions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Assertions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?