reset password
Author Message
ckolodin
Posts: 6
Posted 01:54 Apr 10, 2009 |

Last week's puzzle:

Assume you have a class with a method that checks if an integer is even or odd. The method takes an integer as a parameter and returns a boolean value. Below is the method:

public static boolean odd(int i){      return i % 2 == 1; }

What is wrong with the method above?

 

A: This method only works for positive i. If i is odd (and negative) i%2 is -1. To fix it just include the case where i%2 is -1 in the return statement.

public static boolean odd (int i) {return (i % 2 == 1 | i % 2 == -1); }

mhajianpour
Posts: 20
Posted 09:52 Apr 10, 2009 |

Good job. That's the problem with the method. Let me know if you want more. ;)

--
Mahan Hajianpour