• 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

please explain this Assertions problem

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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???
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Sonali Sehgal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit and Swati I have understood this question
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sonali Sehgal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.....
 
reply
    Bookmark Topic Watch Topic
  • New Topic