DOC PREVIEW
UNC-Chapel Hill COMP 401 - COMP 401 ASSERTIONS

This preview shows page 1-2-3-4-5-6-39-40-41-42-43-79-80-81-82-83-84 out of 84 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 84 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 84 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 84 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 84 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 84 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 84 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 84 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 84 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 84 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 84 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 84 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 84 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 84 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 84 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 84 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 84 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 84 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 84 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 Dewan2ASSERTIONSDeclare some property of the program Potentially useful forspecificationtestingformal correctness documentationuser-interface automation3COMPILE 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.4APPLICATION-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 added5JAVA 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/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 RULEIf 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

UNC-Chapel Hill COMP 401 - COMP 401 ASSERTIONS

Documents in this Course
Objects

Objects

36 pages

Recursion

Recursion

45 pages

Load more
Download COMP 401 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 COMP 401 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 COMP 401 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?