The assert statements are being used to check a precondition--something that must be true when the method is invoked.---->true The assert statement is being used to check a postcondition--something that must be true when the method completes successfully.---->false
I don�t get this.Can anyone please explain the above one for me.
Assertions can be used to check postconditions as well as preconditions. The precondition is based on the state of the object before a state transition. The postcondition is based on the state of the object after a state transition.
What is the source of the above two statements? If a mock exam please quote the complete question.
Thanks for the reply Leandro and Barry.Here is the mock exam question
Which statements are true?
a. If assertions are not enabled at run time it prints an error message. b. If assertions are not enabled at run time it prints nothing. c. With assertions enabled it prints an error message. d. With assertions enabled it prints nothing. e. The assert statement is being used to check a postcondition--something that must be true when the method completes successfully. b,c,e are answers
posted 13 years ago
If you look at method m1(), you will see that the assert statement (assert c < 200) is done just before the return. The purpose of the statement is to check the postcondition (a condition that must be true after the computation) that the computed value of c is less than 200.
Please Barry help me again.Still I don�t get this part.i am only getting the answer b and c.In the assert statement 200<200 is false.Right?Then how come The assert statement is being used to check a postcondition--something that must be true when the method completes successfully.What they mean by postcodition?
posted 13 years ago
By calling the methods setA(), setB(), and m1() you are changing the internal state of the instance of class B. Methods setA() and setB() can set the two instance members a and b to any values, there are no constraints. But method m1 can only be passed a value i such that the computed value of member c is less than 200. If the new value of c is less than 200 then everything is correct. If the new value of c is greater or equal to 200 then we have a problem. We specify this constraint by saying "after the new value of c is computed, we must have c less than 200". This type of condition is known as a postcondition because it is evaluated after the computation is finished (post- means after, pre- means before).
So we are using the assertion statement to ensure that the postcondition c < 200 is true. If c >= 200 then we throw an AssertionError (if assertions are enabled).
Let's try an analogue - suppose that you are driving a car on a road where the speed limit is 100 km/hr. You can give gas by pressing the accelerator and change the gears. After you have changed the speed of the car you must still be driving at less or equal to 100 km/hr. That is a postcondition that you can test with an assertion, assert speed < 100 : "get fined";
But before you give gas there are other conditions to be checked. For example: Am I going slower than the limit? Is the road clear? Is there a bend coming? Do I have enough gas? Is the road dry? These are preconditions to be satisfied before you increase the speed of the car. You can also test these with an assertion: assert (speed < 95) && isClear() && !isBendy() && isDry() && !emptyGasTank() : "Do not try to go faster!";