Re: Boolean
On 3/1/2013 9:42 AM, odie4penname@gmail.com wrote:
On Friday, March 1, 2013 12:18:15 AM UTC-8, JLP wrote:
Le 01/03/2013 08:22, odie4penname@gmail.com a ?crit :
Create a public boolean method named
MynumberisOdd [...]
^^^^^^^^^^^^^
public boolean isNumberOdd (int num)
^^^^^^^^^^^
Actually, the name you have chosen is better than the
one the problem statement demands. (See below.)
Nota : Class names must start with a capital letter => Example
A bit of context on this "must." The Java language does
not require CapitalizedClassNames, nor camelCaseMethodNames,
and a Java implementation will accept and execute code that
uses other kinds of names. However, there are widely-followed
conventions about how to write names for different kinds of
things: packages, classes, interfaces, methods, and so on.
Human readers are accustomed to these conventions and will
find code easier to read and understand if the conventions
are followed. Bear in mind that code in any programming
language is written for two audiences: Computers and people.
Of the two, people are the more important.
Is this correct? This is what I have now:
public class Example
{
public static void main(String [] args)
{
int x=1;
`x' is never used; why is it here?
System.out.println(new Example().isNumberOdd(1));
Did you mean `...isNumberOdd(x)', perhaps? Also, if the
idea is to demonstrate that the method works correctly, it
might be a good idea to try a few other argument values: a good
set of test cases would probably include both odd and even
numbers, negative and positive numbers, and the special "edge
cases" 0, Integer.MAX_VALUE, and Integer.MIN_VALUE.
}
public boolean isNumberOdd (int num)
{
return num%2 !=0;
}
}
Looks all right to me. Since the isNumberOdd() method
relies only on the value of its parameter and not upon any
"context" from an enclosing object, it might be slightly
better to write it as a `static' method and call it as such
(if you've learned about `static' yet).
--
Eric Sosman
esosman@comcast-dot-net.invalid