Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Programming by contract Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Programming by contract" Watch "Programming by contract" New topic
Author

Programming by contract

luk Hann
Ranch Hand

Joined: Jan 29, 2001
Posts: 48
Hi,
Does anybody feel familiar with the subject term? Relating it to an exception example: a square-root methd could require that it must be called only with a non-negtive argument. If called with a negative argument, the method would react by throwing an exception, since the contract between it and its caller has been broken. So how to express this contract in the code? What the code should be to demonstrate the above example? Thanks for any leads!
Luk
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Well, in java you CAN take the sqrt of a negative number, you just get back the value NaN (Not a Number). However if it DID throw an Exception you would code it like this.
Put it in a try catch block

So to give you a better example try inputting a string value, then revise the code to handle the problem.


"JavaRanch, where the deer and the Certified play" - David O'Meara
luk Hann
Ranch Hand

Joined: Jan 29, 2001
Posts: 48
Cindy,
Thanks much for your answer to my two questions. I highly appreciate!
luk
[This message has been edited by luk Hann (edited February 02, 2001).]
 
Don't get me started about those stupid light bulbs.
 
subject: Programming by contract
 
Similar Threads
declare runtime exceptions in javadoc?
Public method argument validation
Equals and HashCode
Interface Method Signatures
Assertion