Check out Manning's Countdown to 2014. Use discount code crdotd14 all month for 50% off every deal.
Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# Classes and Constraints Big doubt

Jorge Bendahan
Ranch Hand

Joined: Apr 11, 2008
Posts: 82
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.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
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 )

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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?

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jorge Bendahan
Ranch Hand

Joined: Apr 11, 2008
Posts: 82
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

Joined: Sep 17, 2006
Posts: 1282

......
Jorge Bendahan
Ranch Hand

Joined: Apr 11, 2008
Posts: 82
those are goood examples of preconditions.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
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
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Apr 11, 2008
Posts: 82
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

Joined: Apr 11, 2008
Posts: 82
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

Joined: Apr 11, 2008
Posts: 82
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 ]

Consider Paul's rocket mass heater.

subject: Classes and Constraints Big doubt