Equivalent Boolean Expressions

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Answer:

Yes. The two fragments are equivalent. It may have taken you a while to figure this out, and you may not be completely convinced.

Equivalent Boolean Expressions

To decide if the two fragments are equivalent, pick some example values for speed and memory and see if the expressions evaluate the to the same boolean values. A systematic way of doing this is with a truth table. To start, look at the "speed" question:

speed > 2000 speed <= 2000
F T
T F

If the speed is greater than 2000, then it is not less than or equal to 2000. And the speed is less than or equal to 2000, then it is not greater than 2000.

Only one of X>Y and X<=Y can be true for particular values of X and Y.

Another way to say this is !(X>Y) and X<=Y are equivalent. The last two columns of the following table show equivalent expressions.

speed > 2000 !(speed > 2000)speed <= 2000
F T T
T F F

It is also true that X>Y and !(X<=Y) are equivalent. This is demonstrated in the following question.

QUESTION 8:

Fill in the following table. Fill the cells of a row so they are consistent with the first cell of the row.

speed > 2000 (speed <= 2000)!(speed <= 2000)
F
T

The first column and the last column have the same T/F values, which shows that speed > 2000 is equivalent to !(speed <= 2000).