creation: 07/07/06

Review of Truth Tables and De Morgan's Rules


1.       Complete the following truth table:

Operand Operand Expression
x <= y y > 100 x <= y && y > 100
F F
F T
T F
T T

Decide from the table what the value of the expression is is when x == 150 and y == 125.


2.       Complete the following truth table:

Operand Operand Expression
ch >= 'a' ch <= 'z' ch >= 'a' && ch <= 'z'
F F
F T
T F
T T

Decide from the table what the value of the expression is is when ch == 'A'.


3.       Complete the following truth table:

Operand Operand Operand Subexpression Expression
ch >= 'a' ch <= 'z' ch == '?'ch >= 'a' && ch <= 'z'(ch >= 'a' && ch <= 'z') || ch == '?'
F F F
F F T
F T F
F T T
T F F
T F T
T T F
T T T

Decide from the table what the value of the expression is is when ch == 'x'.


4.       Is !A && B equivalent to A || !B   ?   Complete the table to find out.

Operand Operand Subexpression Subexpression Expression Expression
A B !A !B !A && B A || !B
F F
F T
T F
T T

The two expressions are equivalent if the T/F values in the last two columns are the same.

Now use De Morgan's Rule:

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

to rewrite !A && B.


5.       You wish to purchase a certificate of deposit, but only if the interest rate is greater than three percent and the minimum deposit is less than 10000. Fill in the blanks.

if ( interest  3  minimum  10000)
  System.out.println("Buy this CD");
else
  System.out.println("Reject this CD");

Rewrite the above fragment so that it fits the following.

if ( interest  3  minimum  10000)
  System.out.println("Reject this CD");
else
  System.out.println("Buy this CD");

(The first program fragment is probably the best way to write this.)



6.       A program is searching for books with a call number that starts "QA" published between 1990 and 1995 (inclusive) with the word "web" in the title. Assume that the attributes of a book are accessed by book.getTitle(), book.getDate(), and book.getCall(). Recall that indexOf( String sub ) returns the integer index of the substring, or -1 if the substring is not found.

if ( book.getCall().indexOf("QA") 0  
     book.getDate().  1990  
     book.getDate().  1995  
     book.getTitle().indexOf("Web")  0)
     
  System.out.println( book.getCall(), book.getTitle() );

(The first program fragment is probably the best way to write this.)



End of the Exercise. If you want to do it again, click on "Refresh" in your browser window. Click here to go back to the main menu.