Truth Tables and De Morgan's Rules
18. More De Morgan
Answer:
Using the De Morgan Rule
!(A && B)
is equivalent to!A || !B
The expression
boolean discount = !( (days < 14) && onSeason );
is equivalent to
boolean discount = !(days < 14) || !onSeason ;
which can be further transformed to
boolean discount = days >= 14 || !onSeason ;
More De Morgan
De Morgan's rules can be extended to three operands:
!(A && B && C)
is equivalent to!A || !B || !C
!(A || B || C)
is equivalent to!A && !B && !C
Expressions with more than three operands behave similarly.
Question 18:
A driver holding a learner's permit may drive a car only during daylight hours and
only if accompanied by a licensed driver of age 21 or over.
boolean drivingOK = daylight && passenger.age >= 21 && passenger.licensed;
Assume that
passenger
refers to an object with an integer memberage
and a boolean memberlicensed
.Here is an expression that says when driving is not permitted:
boolean noDriving = !(daylight && passenger.age >= 21 && passenger.licensed );
Transform this into a simpler expression.