De Morgan's Rules

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:

if ( car.price <= 8000 )
  System.out.println("Affordable");
else
  System.out.println("Too Expensive!");

De Morgan's Rules

Augustus De Morgan was a nineteenth century British mathematician who showed the importance of several rules of logic. Two of these, De Morgan's Rules, show how the NOT operator can be moved to the inside of an expression. (Although these rules are named after De Morgan, they were, in fact, known to Aristotle.)

!(A && B) is equivalent to !A || !B

!(A || B) is equivalent to !A && !B

These rules are very useful, and worth your time to memorize. This truth table shows why the first rule is true.

A B (A && B) !(A && B) !A !B !A || !B
F F F T T T T
F T F T T F T
T F F T F T T
T T T F F F F

The fourth column and the last column have the same truth values. This shows the the expressions at the head of those columns are equivalent.

QUESTION 13:

Rewrite the following fragment (from a previous example):

boolean reject =  !(speed > 2000 && memory > 512)