This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
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

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Programming by contract" Watch "Programming by contract" New topic

Programming by contract

luk Hann
Ranch Hand

Joined: Jan 29, 2001
Posts: 48
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!
Cindy Glass
"The Hood"

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
Thanks much for your answer to my two questions. I highly appreciate!
[This message has been edited by luk Hann (edited February 02, 2001).]
I agree. Here's the link:
subject: Programming by contract