This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes please explain this Assertions problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "please explain this Assertions problem" Watch "please explain this Assertions problem" New topic
Author

please explain this Assertions problem

Sonali Sehgal
Ranch Hand

Joined: Jul 09, 2009
Posts: 75



In the above code what does assert statement do ....???
In order to see how does assert statment exhibit its functianality is something that i dont understand???
Swati Pawar
Greenhorn

Joined: Jul 06, 2009
Posts: 2
private double calcSpeed(double distance, double time) {
assert distance>=0.0; // Check if distance is grater than or equal to 0.0,
// then continue else throw assertionException
assert time >0.0 : "Time is not a positive value:" +time;
// If time > 0.0 then continue to the next line else throw
// assertionException. It will print "Time is not a positive value:" +
// time with the exception
double speed =distance/time;
assert speed >=0.0; // If speed is greater than or equal to 0.0 then return
// speed else will throw AssertionException
return speed;
}

Assertions are used for debugging the program. Its like if condition. If condition is true it will continue to execute code else will throw Exception.

Whatever I know about assertions, I am telling you from that. Please correct me if I am wrong.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Remember, you have to enable assertion at runtime using -enableassertions, or -ea, switch. If you don't enable assertions at runtime, then assert statements will be ignored by the JVM. For SCJP, you must know what is the correct use of assertions. Like assertion can be used for validating parameters to private methods but should not be used for validating parameters of public methods. A full list of correct use of assertion is given in K&B book. You can also look at this article to get some idea about this...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sonali Sehgal
Ranch Hand

Joined: Jul 09, 2009
Posts: 75
Thanks Ankit and Swati I have understood this question
akaash singh
Ranch Hand

Joined: Jul 04, 2009
Posts: 45
Hi everyone

I am preparing for scjp too. So for learing purpose i tried out sonali's cod .I gave the arguments 12.0 & 0 to the calcSpeed method and ran it without enabling the assertions . I was expecting an exception but the output has me doubting whatever progress i have made in java so far .

Here's what i got :

C:\java>java -ea Speed
Exception in thread "main" java.lang.AssertionError: Time is not a positive valu
e:0.0
at Speed.calcSpeed(Speed.java:12)
at Speed.main(Speed.java:5)

This is ok , because of the assertions . but have a look at the next one

C:\java>java Speed
Speed (km/h):Infinity


someone please explain , what point am i missing . Cause i am panicking now . Shudnt it give an Arithmetic Exception . or is there something wrong with my compiler/jvm.
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
I guess you mean line 15 where the argument "12.0" and "0.0" would be effectively dividing by zero.
Yes, at first glance I'd expect to get some ArithemticException too. After all, that's what my calculator does.
I'm studying for the SCJP too, so we can panic together.

Right then, no "Divide by 0!" squeal from the JVM. Why?
Well, if you changed line 15 to use integers you do get an ArithmeticException. Phew! Must be something special about doubles then or more correctly, floating point arithmetic versus integer arithmetic.
Integers cannot store/represent infinity. floating points can represent positive and negative infinity.
So if you do divide by zero with integers, there's no way to return the answer; exception!
But with floating points infinity can be returned. That behaviour would appear to be the IEEE standard for floating point divide by zero and that's what Java uses.
There are probably deep mathematical reasons for treating integers and float points in such a different way.

I had to Google most of that, and I pray to the Java gods that that kind of horrible nastiness is not on the exam!


SCJP6
Sonali Sehgal
Ranch Hand

Joined: Jul 09, 2009
Posts: 75
I am also preparing for SCJP...If this question comes in exam ..............
Really ...dont know what to say as the right option in doubles..............kind of afraid
akaash singh
Ranch Hand

Joined: Jul 04, 2009
Posts: 45
hey jason
in retrospect of the a++ and b++ trouble and now this, i think had better stop tinkering with code now , lest it frightens us off into skipping the test altogether .. dont know wat to say ..

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Well this is a basic thing and you must know about such things for the exam. Java follows IEEE floating point standards and that defines that a positive number divided by zero is positive infinity. You must have some basic knowledge about NaN and Infinity values for the exam. BTW, that's why everyone says that you should code a lot, you find strange things while you practice ...
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
Hi Ankit,

I went back to Sierra&Bates and looked for this "topic". Other than mentioning the "strictfp" keyword I could not find any information on the particulars of floating point versus integer arithmetic. Maybe I've not have enough coffee this morning....

If this information is required for the exam could you possibly give provide a link or a reference to where it is in Sierra&Bates so I can read about it? Thanks.
Sonali Sehgal
Ranch Hand

Joined: Jul 09, 2009
Posts: 75
Hi Jason,

See this link and you will get the answer for fJava follows IEEE floating point standards and Infinty as the correct output.....

http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.4

This is for Java Language specification...and it has complete details...for the topic you are looking for.....
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: please explain this Assertions problem