Section 6h

Boolean Methods


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Functional Abstraction - Continued

Handy Tools

Boolean methods can be powerful tools to help you make decisions but to keep the messy stuff out of the way of the problem-solving activities. All you need to do to develop and use Boolean methods is to remember that various methods can return various values and data types, so it stands to reason that Boolean methods can return Boolean values. Suppose you had a method that implemented the test for leap year. The following is an example of how this method might be implemented.

public static boolean isLeapYear( int year )
{
boolean fourthYear, notACentury, fourthCentury;

fourthYear = ( year % 4 == 0 );

notACentury = ( year % 100 != 0 );

fourthCentury = ( year % 400 == 0 );

return( fourthYear && notACentury || fourthCentury );
}

Recall from your previous work that you can return a value or a calculation back from a called method, so it should be logical that you can return a Boolean expression. The above is a good example of a Boolean method. Note that from the code in the previous topic, it could have been narrowed down to the simple Boolean expression [i.e., return ( year % 4 == 0 ) && ( year % 100 != 0 ) || ( year % 400 == 0 ); ], but that is not as readable or clear, and would be very difficult to debug if someone unfamiliar with the algorithm were assigned to work on this method.

Now, if you can return Boolean expressions in the form of methods, then it is logical that you can test them just as you would a Boolean expression . . . and you can. Consider the following.

if( isLeapYear( presentYear ) == true )
{
daysInFebruary = 29;
}

The above is a good example of how you can use abstraction to present a simple test. The messy part of all the leap year Boolean processing is hidden inside the method, but it is clear what you are testing in your code. The nice thing about this is that it can be made even easier.

However, remember from the previous topic that the computer only cares about the result of Boolean expressions. It makes its decisions from the true or false value that is found between the parentheses. Above, the if statement is looking to see if isLeapYear( presentYear ) is in fact equal to true. However, if the method can actually be equal to true, then by itself it can be tested for "truth". Consider the following.

if( isLeapYear( presentYear ) )
{
daysInMonth = 29;
}

Think about this carefully. What does the computer want to see between the parentheses? It wants to see something that evaluates to true or false. The isLeapYear method can do this. Therefore, it is redundant to test for its equality to true or false. While it is perfectly legal and appropriate code, it is not necessary. In most conditions where Boolean methods are used, they are used as you see in the example above [i.e., if( isLeapYear( presentYear ) ) ]. Plan on using this form of code as you learn more about using methods.

Consider these last two examples from a mouse in a maze program that tests for possible movement. To test to see what is ahead before moving, you might write one of the following if statements, using example Boolean methods as shown.

if( doesNotHaveWallToLeft() )

if( doesNotHaveWallAhead() )

These two methods are tested for "truth". They could have been presented in the form if( doesNotHaveWallAhead() == true ), but again, that would have been redundant. The form you see here is easier to read, and is actually more like the English question, "If the mouse does not have a wall ahead of him" . . . take the next action.

There is not much more to say about Boolean methods. The great value of them is that they abstract away the details of certain tests so that the programmer, and the person reading the code later on, can focus on the problem at hand rather than worrying about how a certain test might work. Using Boolean methods will make your design operations easier, and your resulting code cleaner and better.

Watch this video to see the isLeapYear method go together and get used.