• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Assertions

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Firstly, in the line 1, you are working at deploy time, and it�s correct, if you work with the idea of postcondition, we are saying that assert could modify something during runtime, and it�s wrong !

Could you understood ?
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Leandro.Still i don't get it.Could you please give me an example source code if possible.
 
Leandro Eschiavi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
CORRECT EXAMPLE

private void methodA(int num) {
assert (num >=0);
}

WRONG EXAMPLE

private void methodA(int num) {
assert (modifyThings());
}

public boolean modifyThings() {
y = x++;
retunr true;
}
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!";
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Barry,Thanks you for the excellent explanation. After I spent a long time, I got it.

private void m1 (int i) {
assert i < 10 : i; System.out.print(i);
}

In the above method, The assert statement is being used to check a precondition--something that must be true when the method completes successfully. Am I right?

This site and you guys are really wonderful in helping others. Thankyou very much.Keep it up.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Almost. Um...
Consider this class for example:


I have commented possible pre or post conditions in method debit.
[ September 28, 2006: Message edited by: Barry Gaunt ]
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic