Even Function in Java
Java programs can calculate whether an integer type number is even or odd. This can be a useful task in certain applications, for example where a process is alternating. By carrying out an arithmetic operation on an integer, Java code can determine whether it is even. If it is not even, it must be odd. Using this process in conjunction with boolean variables, programs can implement control flow tailored to the number value.
-
Variables
-
If a program needs to determine whether or not a number variable is even, it can do so using variables. The following sample Java code instantiates two variables:
int number = 6;
boolean isEven=false;The number is modeled as a primitive type integer variable with an arbitrary initial value. The boolean variable can only have a value of either true or false. The boolean is initialized to false as its default value. The boolean variable name is a meaningful representation of its purpose and meaning. After the arithmetic process, the value will be true if the number is even, false if it is not.
Remainder
-
Java programs can use a range of arithmetic operators. The remainder operator returns a value representing the remainder after a division calculation completes. For example, the following calculation would result in a value of five:
15%10Once the first operand has been divided by the second, the remainder is five. A program can use the following code to establish the remainder of the number variable after dividing by two:
number%2The calculation divides by two because even numbers divide by two with no remainder, while odd numbers leave a remainder of one when divided by two.
-
Conditional
-
By incorporating the remainder calculation into a conditional statement, a Java program can update the value of the boolean variable. The following code demonstrates:
if((number%2)==0) isEven=true;This code carries out a test. If the remainder of the number, after dividing by two, is equal to zero, it must be even. If the test returns true, the boolean variable is therefore updated to reflect a true value. If the conditional test returns a false result, meaning the number is not even, it has to be odd. In this case the program does not need to do anything because the boolean variable still has its initial value of false. The program could test the function as follows:
System.out.println(number + " is even? - " + isEven);
Control
-
Once a Java program has a boolean variable with a value that represents whether or not the number is even, it can use that variable to dictate control flow. The following sample code demonstrates:
if(isEven) {
//processing for even numbers
}
else {
//processing for odd numbers
}Inside each of these blocks, the program can dictate processing for each case, the number being even or odd. This structure allows programmers to tailor what happens when a program executes to specific circumstances.
-
References
- Oracle: The Java Tutorials - Primitive Data Types
- Oracle: The Java Tutorials - Variables
- Oracle: The Java Tutorials - Assignment, Arithmetic, and Unary Operators
- Oracle: The Java Tutorials - The if-then and if-then-else Statements
- Oracle: The Java Tutorials - Equality, Relational, and Conditional Operators
Resources
- Photo Credit BananaStock/BananaStock/Getty Images