go to previous page   go to home page   go to next page hear noise highlighting

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 member age and a boolean member licensed.

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.


go to previous page   go to home page   go to next page