RU beehive logo ITEC dept promo banner
ITEC 120
2009spring
ibarland
nokie
jmdymacek

homeinfolectslabsexamshws
textbooktutor/PIsjava.lang docsjava.util docs

lect07a
Boolean expressions

Review if (and, factoring code)

Consider a program for the following:

  /** Given how many slices of (say) mushroom pizza are
   * currently available, create a nice tempting message
   * to advertise (shout out, or post, or put on a 
   * LED sign...)
   *
   * @param topping The type of pizza (e.g. "pepperoni")
   * @param slicesReady The number of currently ready-to-serve
   *    slices with the given topping.
   * @return A complete sentence describing the inventory.
   * For example:
   * inventoryMessage( "mushroom", 3 ) 
   *   = "There are 3 piping hot slices of mushroom pizza, ready to eat!"
   * inventoryMessage( __________, __ )
   *   = ______________________________________________
   * inventoryMessage( __________, __ )
   *   = ______________________________________________
   */
By writing out some test cases, we realize that there is a need for an if. This then gives rise to our first version:
  String slicesReadyMsg( String topping, int slicesReady ) {

    if (slicesReady == 1) {
      return "There is "
           + slicesReady
           + " piping hot slice of "
           + topping
           + " pizza, ready to eat!";
      }
    else {   // slicesReady isn't equal to 1
      return "There are "
           + slicesReady
           + " piping hot slices of "
           + topping
           + " pizza, ready to eat!";
      }
    }
This function works, but ech, it's full of repeated code. In particular -- if you later realize that (say) you forgot a space somewhere in the message, you might fix the code in place but forget to update the other copy. Or, if your boss decides that the message should read something like “Only 5 pizzas of pepperoni left -- get 'em while you can!” then you again need to remember to update multiple places in the code.

We can do better: Where do the two solutions differ? In only two places: “is” vs. “are”, and the suffix “s” vs. no suffix at all. We can factor out the common code, and let our if statement focus on just those differences:

  /* (Same comments as before; omitted for lecture presentation)
   */
  String slicesReadyMsg( String topping, int slicesReady ) {
    String verbForm;   // The correct verb for our result.
    String plurality;  // The noun-suffix, correctly plural or singular.
    
    // We just declared two local variables; now initialize them:

    if (slicesReady == 1) {
      verbForm = "is";
      plurality = "";
      }
    else {
      verbForm = "are";
      plurality = "s";
      }

    return   "There " + verbForm + " "
           + slicesReady +
           + " piping hot " 
           + ("slice" + plurality) 
           + " of "
           + topping  
           + " pizza, ready to eat!";
    }
One note: in our return statement, we had to add a "There " before verbForm. Should we just have had verbForm include that "There ", assigning it to either "There is " and "There are "? What about the trailing space, after verbForm? Discuss.

Boolean expressions

The type boolean is much simpler than (say) int, because rather than 4+ billion ints, there are only two boolean values: true, and false. Note that these two names aren't variables; they're values, just like 17 and -34 are.

There are some functions in Java which take two numbers, and return a boolean. ==, >, >=, ==, !=, etc.. Type the following boolean expressions into BlueJ's Code Pad:

(2+2 == 4)
2+2 == 5
2+2 != 5
2+2 != 4  // "Is it true, that 2+2 is not four?"

5 > 4
5 > 99
5 > 5
5 >= 5

double x = 5;
double y = 43.2;
(x+y)/2 >= Math.sqrt(x*y) // is the arith.avg less than geom.avg., for the current value of x,y?

Booleans as inputs

/** Give an estimate of how far it will take to travel a given distance, on the highway.
 * @param miles How far to travel, in miles.
 * @param isRoadClear  Is the road in good condition (true), or is it icy/foggy/accidented (false)?
 * @return The estimated time of driving that far, in hours.
 * highwayDrivingTime(   0, true  ) = 0
 * highwayDrivingTime(   0, false ) = 0
 * highwayDrivingTime(  65, true  ) = 1.0
 * highwayDrivingTime(  65, false ) = 1.6
 * highwayDrivingTime( 130, true  ) = 2.0
 * highwayDrivingTime( 130, false ) = 3.2
 */
double highwayDrivingTime( double miles, boolean isRoadClear ) {
  final int SPEED_LIMIT = 65;  // posted limit, in MPH.  (We can declare and initialize, all on one line.)

  final double DELAY_FACTOR = 1.60;  
  // If road isn't clear, how much does that slow us down?  
  // 2.0 means twice as long, 1.0 is no slowdown (and less than 1.0 would be a speedup).


  if (isRoadClear) {
    return (miles / SPEED_LIMIT);
    }
  else {
    return (miles / SPEED_LIMIT) * DELAY_FACTOR;
    }
  }
Observe how, in if (isRoadClear) , that one variable is a true/false expression.

Booleans as outputs (predicates)

We can also return a true/false answer from a function:

/**
 * @param age The age (in years) of the person in question.  E.g., 18months is age 1.
 * @return whether or not the person must register with Selective Service.
 *   mustRegisterForDraft(22) == true
 *   mustRegisterForDraft(14) == false
 */
boolean mustRegisterForDraft( int age ) {
  return (age >= 18);
  }
Note that the first impulse is to write this function as
  if (age >= 18) {
    return true;
    }
  else {
    return false;
    }
This is certainly acceptable, and you might even find it clearer than return (age >= 18);. But you'll find that as you really internalize the idea of booleans as values, the short version really will feel more natural.

By the way, is this a silly function to write? Why, anybody who wants to call this function could more simply write the comparison against 18 directly. So what do we gain, by writing this as a separate function? We'll revisit this function soon.

A function which returns a boolean value is called a predicate function (or just, “predicate”). Often, predicates' names start with “is” or “has”: For example “isHappy”, “hasEaten”, etc..

boolean connectives: &&, ||, !

The boolean functions are && (“and”) and || (“or”). For instance,

((30 <= choice) && (choice < 33))
reads as “choice is at least thirty, and less than 33”. Similarly,
((newFace < 1) || (newFace > NUM_SIDES))
tests for newFace being less than 1, or bigger than NUM_SIDES.

In general, && looks at the boolean expressions on each side, and the entire && will evaluate to true if (and only if) the left side and the right side evaluate to true:

expr1 expr2 (expr1&&expr2)
false false false
false true false
true false false
true true true
(expr1 and expr2 can be any other boolean expression—including more-deeply-nested and/or expressions!)
Complementing &&, an entire || expression evaluates to true if (and only if) one side or the other evaluates to true:
expr1 expr2 (expr1||expr2)
false false false
false true true
true false true
true true true
Really, you can think of && as a function with the signature boolean &&( boolean b1, boolean b2 ) (although, like arithmetic1, we write them in infix notation, with the function-name coming inbetween its two inputs, rather than in-front-of-them-with-parentheses).

Exercise: What is the value of:

( ((3 <= 7) || (4 == (2+3))) && (Math.sqrt(4) > 0.0) )
This is just like solving an arithmetic problem, except that we are dealing with true and false instead of all the integers2.

It's worth a mention that in English, “or” is used in two different ways: The inclusive-or, which is what || means, means “one or the other or both”. For example, if the question on the tax form asks “are you over 18, or had income of more than $20,000”, and you meet both criteria, then the answer is true. (People might use “and/or” to emphasize they include the “and” part.) However, sometimes English uses “or” to mean one or the other but not both. For instance, “You can eat veal, or you can be morally responsible” is implying that one option precludes the other. (You may or may not agree with that presumption, of course.) Similarly, if you are told “Keep dating your ex, or date me!”, don't presume that the option of both is included in the “or”!

While we're on the topic, there is one last boolean operator, ! (“not”):

expr !expr
false true
true false
So (2+2 == 4) is true, but !(2+2 == 4) is false. This could also have been written directly using the not-equals operator: (2+2 != 4).

Exercise: What is

Let's revisit mustRegisterForDraft. The current laws are a bit different from what is shown: only men aged 18-25 must register. Update our function accordingly:

/**
 * @param age The age (in years) of the person in question.  E.g., 18months is age 1.
 * @param ___  _______________________________________________
 * @return whether or not the person must register with Selective Service.
 *   mustRegisterForDraft_v2( 0, ___) == false
 *   mustRegisterForDraft_v2( 0, ___) == false
 *   mustRegisterForDraft_v2(17, ___) == false
 *   mustRegisterForDraft_v2(18, ___) == true
 *   mustRegisterForDraft_v2(22, ___) == true
 *   mustRegisterForDraft_v2(22, ___) == false
 *   mustRegisterForDraft_v2(25, ___) == _______
 *   mustRegisterForDraft_v2(27, ___) == _______
 */
boolean mustRegisterForDraft_v2( int age, ____ ______ ) {
  return _ (age >= 18) __ ________ __ _______ _;
  }

solution

/**
 * @param age The age (in years) of the person in question.  E.g., 18months is age 1.
 * @param isFemale  Is the person in question female?
 * @return whether or not the person must register with Selective Service.
 *   mustRegisterForDraft_v2( 0, true ) == false
 *   mustRegisterForDraft_v2( 0, false) == false
 *   mustRegisterForDraft_v2(17, false) == false
 *   mustRegisterForDraft_v2(18, false) == true
 *   mustRegisterForDraft_v2(22, false) == true
 *   mustRegisterForDraft_v2(22, true ) == false
 *   mustRegisterForDraft_v2(25, false) == true
 *   mustRegisterForDraft_v2(27, false) == false
 */
boolean mustRegisterForDraft_v2( int age, boolean isFemale ) {
  return ( (age >= 18) && (age <= 25) && !isFemale );
  }
By the way, now we see more clearly, the advantage of making this its own function -- we don't have to update the rules in several places. In fact, we'll need to update our function further, to account for whether the person is a U.S. citizen, or an alien living in the U.S. In fact, ultimately your code should be a direct translation of the actual regulations, which can be fairly detailed.

homeinfolectslabsexamshws
textbooktutor/PIsjava.lang docsjava.util docs


Naming convention: start boolean variable names with a word to make it clear that the variable stores a true/false value: “isHappy”, “hasAllPrereqs”, etc.

Aside: Some languages — not Java — allow “?” to be part of a name; in those languages boolean variables are often named “happy?”, “hasAllPrereqs?”. Since functions which return a boolean value are sometimes called “predicates”, some people use the letter “p” to indicate a predicate function: “isPrimeP(int n)”, “flightIsBookedP( int flightNum )”.

Practice

Various javabat.com problems, such as:


1Just like we think of + and * as functions which take in two numbers, and return a number.      

2If you look at the tables and squint, thinking of false,true being like 0,1 and ||,&& being like +,* then these tables above really do look fairly similar to arithmetic.      

homeinfolectslabsexamshws
textbooktutor/PIsjava.lang docsjava.util docs


©2009, Ian Barland, Radford University
Last modified 2009.Mar.18 (Wed)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme