Full Price Hotel

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:

Using the De Morgan Rule

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

The expression

boolean shipping =  !(purchase >= 50 && !onSale ); 

is equivalent to

boolean shipping =  !(purchase >= 50) || !onSale ; 

which can be further transformed to

boolean shipping =  purchase < 50  || onSale ; 

Full Price Hotel

A hotel charges full price for its rooms during the "on season" when a reservation is made less than 14 days in advance. Otherwise, a reservation qualifies for a discount.

boolean fullFare = (days < 14) && onSeason;

Assume that onSeason is a boolean variable. Here is an expression that says when a discount rate may be available:

boolean discount = !( (days < 14) && onSeason );

QUESTION 17:

Rewrite this expression in two steps. In the first step, apply De Morgan's rule that !(A&&B) is equivalent to !(A)||!(B).

boolean discount =   

In the second step, rewrite the relational expression:

boolean discount =