Print Page Print Page

CH5 – Logical Operators

  1. Open DrJava or clear the Interactions Pane by right-clicking and selecting Reset Interactions.
  2. Declare and assign values for the variables A and B.
> boolean A = true;
> boolean B = true;
  1. Test De Morgan’s laws by typing in the following.
> !(A && B)
false
> !A || !B
false
> !(A && B) == !A || !B
true
> 
  1. Now type the following.
> !(A || B) == !A && !B
false
> !(A || B)
false
> !A && !B
false
> !(A || B) == !A && !B
false
  1. Note that the final statement return false, which seems to be in error.
  2. Modify the final statement to appear as follows, note that although incomplete, the result is now true.
> !(A || B) == !A
true
  1. Modify the statement to appear as follows, the result is true, as we forced the correct order of operation.
> !(A || B) == (!A && !B)
true
  1. Reset the Interactions pane and declare X and Y as integers, assign six to x and 3 to y.
> int x = 6;
> int y =3;
  1. Type the following statements and note the results.
> !(x < 5 && y == 3) true > x >= 5 || y != 3
true
> !(x >=1 || y != 7)
false
> x < 1 && y == 7
false