This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.

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 My answer: a,c,e 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.

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury

Think about it: 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.

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 ]

not so smart guy still curious to learn new stuff every now and then

Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391

posted

0

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 ]

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

Joined: Mar 05, 2003
Posts: 1391

posted

0

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 ]