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

Ranch Hand
Posts: 1561
• Number of slices to send:
Optional 'thank-you' note:
Which of the following statements are true in terms of the java.lang.Math.abs method?
a. Four overloaded versions of abs exist.
b. An ArithmeticException is declared in the throws clause.
c. The type of the return value depends on the type of the argument.
d. The returned value is always of a floating-point primitive type.
e. If the argument is greater than or equal to zero then the returned value is equal to the argument.
f. If the argument, arg, is less than zero then the returned value is -arg.
g. None of the Above
Corrects : a, c, e,f
why option f? I'd say:
f. If the argument, arg, is less than zero then the returned value is arg.

mister krabs
Posts: 13974
• Number of slices to send:
Optional 'thank-you' note:
If the argument is less than zero then the returned value is -arg...
Let's say that the arg is -15 (a number less than zero). Then the returned value will be +15. But +15 is -(-15). If abs returned arg then it would return -15 which isn't what abs does. It only returns positive numbers.

Ranch Hand
Posts: 1392
• Number of slices to send:
Optional 'thank-you' note:
Math.abs(Integer.MIN_VALUE) == -Integer.MIN_VALUE
-Integer.MIN_VALUE == Integer.MIN_VALUE
Integer.MIN_VALUE < 0
[ June 13, 2003: Message edited by: Marlene Miller ]

Ranch Hand
Posts: 348
• Number of slices to send:
Optional 'thank-you' note:
In general, option (f) is true ... ,try the code

output :
Integer.MIN_VALUE = -2147483648
Math.abs(Integer.MIN_VALUE) = -2147483648
I believe Dan's mock exam has something similar.
[ June 13, 2003: Message edited by: chi Lin ]

Marlene Miller
Ranch Hand
Posts: 1392
• Number of slices to send:
Optional 'thank-you' note:
In math, the absolute value of a real number is always >= 0.
In Java, there is one int and one long whose absolute value < 0.
[ June 13, 2003: Message edited by: Marlene Miller ]

Ranch Hand
Posts: 76
• Number of slices to send:
Optional 'thank-you' note:

In math, the absolute value of a real number is always >= 0.
In Java, there is one int and one long whose absolute value < 0.

What could be the rational behind this just providing one int/long whose abs value is < 0...any thoughts??
TIA

Ranch Hand
Posts: 443
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Andres Gonzalez:
why option f? I'd say:
f. If the argument, arg, is less than zero then the returned value is arg.

I think the Math.abs is doing exactly of what is expected of it - with a few exception.
From Math.abs API doc:

If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.

I think the confussion stems from the rewording of the phrase "negation of the argument" to "-arg". (f) could be interpreted as abs returning a negative value if you pass it a negative value.

Marlene Miller
Ranch Hand
Posts: 1392
• Number of slices to send:
Optional 'thank-you' note:

What could be the rational behind this just providing one int/long whose abs value is < 0...any thoughts??

I don�t know. What else could Math.abs(Integer.MIN_VALUE) be? an ArithmeticException. Interger.MAX_VALUE + 1 < 0. The addition does not cause an exception. We live with it.
The Java Programming Language (where I get my insights into language design) says "the absolute value of the most negative value of an int or long its itself and therefore negative; that's how two's complement integers work.
Here is the source code.
public static int abs(int a) { return (a < 0) ? -a : a; }
[ June 14, 2003: Message edited by: Marlene Miller ]

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.