• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Classes and Constraints Big doubt

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, i have a doubt regarding the use of constraints on classes, the type of constraints I am talking about are preconditions, invariants and postconditions, I know their definition but still can notice the difference in some cases, well suppose i have a class named Account, an it has two operations, one deposit(int: amount):void and the other withdraw(int: amount):void, i need to place some constraints on both amount parameters, in the deposit operation the amount parameter must be greater than zero(deposit : amount > 0 ) and in the withdraw operation the amount must be at least 10 and up to 1000( withdraw: amount >= 10 && amount <= 1000), so far I have looked at this constraints as preconditions, because they must be true before entering the operations, but now i have some concerns because, they have to be true at all time?. If someone have a better understanding of this topic please be so kind and give me ha hand.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is Problem Definition.

Constraints, Pre-Conditions and Post-Conditions ( as you ask the question here ) has to be based on some grasp of an actual problem.

Need, task, or other definable parameter has no Zero Base. In other words, Zero is never One like it was in an ultra-advanced book I was reading yesterday. Some one from the Bank with the authority of the Bank to make decisions on matters of Banking has to decide if an account can float. ( that's teminology used in banking )
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to look at this from the point of view of a potential client class. What the client needs to know is what he needs to make sure before calling a method, and what he can rely on what will be true after the method executed.

To call your methods, he needs to make sure that the amount passed in satisfies some conditions beforehand, so those are surely preconditions.

Does that help?
 
Jorge Bendahan
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so if we say that invariants are conditions that must be true at all time in the objects life, what are good examples of those constraints?.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

......
 
Jorge Bendahan
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
those are goood examples of preconditions.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I would have to read the book you are reading or look at the website you are looking at to be of further use without risking needless floundering.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Invariants are more for statements about the state of an object that always are true. Such as, the length of a String is always >=0.

Does that help?
 
Jorge Bendahan
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well i'm reading R. Binder Testing O.O. Systems among others,the thing is that i'm trying to make a simple model to demonstrate some stuff, let's say a piece of a banking application, just with a few classes(Account, Customer, Card, CreditCard, and so on..) and extract the constraints from these classes. I'll post some examples, and lets see what you think. I'm allways open for suggestions.
 
Jorge Bendahan
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class: Account
Attributes:
- balance : int
- openDate : Date
- state : char --->(O pen, F:Frozen, I:Inactive, C:Closed)
- owner : Person
Operations:
+deposit(amt : int) @Pre: amt > 0 && state=='O'
+withdraw(amt : int) @Pre: amt > 0 && amt<=balance && state=='O'<br /> +getBalance(): int<br /> +getState(): char <br /> +setState(char newState) @Pre: newState in ('O', 'F', 'I', 'C')<br /> <br /> Class: ATM<br /> Atts:<br /> - onLine: boolean<br /> - cashState : char --> ('O' K, 'L':Low, 'N':NoCash)
- cardIn : boolean
- pinAccepted : boolean
- availableCash : int
- MAX_CASH : int ---> (maximum ammount that can be loaded)

Ops:
+ countMoney():void @post : cashState=@someNewState && availableCash=@..
+ connectToNet():void @pre: cashState!='N' && availableCash > 0;
+ enterCard(card : Card) @pre: online == true
+ enterPin(short : pin) @pre : cardIn == true
+ getBalance(): int @pre : onLine ==true && cardIn == true && pinAccepted == true

so far looks good for me, but as you can see i cant find any invariants..
 
Jorge Bendahan
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More classes of the little banking example.
Class: DebitCard
Atts:
- account : Account
- totalCredit : int
- usedCredit : int
- overdrawnLimit : int
- pin : short
- state : char
- dayTx : short
- maxDayTx : short
- pinAttemps : short
- lastTxDate : Date
Ops:
+ setPin(short:newPin) @pre: state=='A' && newPin >99 && newPin<1000, @post: getPin==newPin <br /> + setTotalCredit(int:newLimit) @pre: state=='A'<br /> + getTotalCredit():int<br /> + setState(char:newState) @pre: newState in (A, O, C), @post: getState == newState<br /> + getState():char @post: state in (A, O, C)<br /> + withdraw(int : amt) @pre: amt>=10 && amt=<1000 && dayTx<3 && (amt <= totalCredit-usedCredit+overdrawnLimit)
+ makePayment(int : amt, short : pin, String : destination) @pre: amt>0 && amt <= (totalCredit - usedCredit + overdrawnLimit)
+ addAttempt() @post pinAttemps++

Off Topic: So far I don't like the fact that there are three withdraw operations for the three different classes, is the same method interface, what can i do to improve this.
[ June 06, 2008: Message edited by: Jorge Bend ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic