JMU
Lab: Gaining Experience with Logical and Relational Operators


Instructions: Answer as many of the following questions as you can during the lab period. If you are unable to complete the assignment during the lab period it is strongly recommended that you complete it on your own.

1. One Set of Symbols: Given the following logical operators (with equal precedence):
AND Logical AND
OR Logical OR
XOR Logical Exclusive OR
NOT Logical Complement

evaluate each of the following expressions:

  1. true OR false


  2. true AND false


  3. true AND (false OR true)


  4. false AND (false OR true)


  5. NOT (false OR (true AND true))


  6. (true OR false) AND (true AND false)


  7. (true XOR false) XOR true


  8. (false XOR false) XOR false


2. A Second Set of Symbols: Given the following logical operators (with equal precedence):
&& Logical AND
|| Logical OR
! Logical Complement

evaluate each of the following expressions:

  1. true || true


  2. true && true


  3. (!true) && true


  4. (!(true || false)) && (true && false)


3. Relational Operators: Given the following relational operators (with equal precedence):
== Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
!=; Not equal to

and the following logical operators (with equal precedence but lower precedence than the relational operators):

&& Logical AND
|| Logical OR
! Logical Complement

evaluate each of the following expressions:

  1. (5 > 5)


  2. (5 >= 2) && (10 < 100)


  3. (4 == 2) && (10 >= 10)


  4. !(2 <= 10)


  5. (41 >= 40)


  6. (!(5 >= 2)) || ((10 < 100) && (8 != 8))


4. Using the Assignment Operator: Given the following logical operators (with equal precedence):
&& Logical AND
|| Logical OR
! Logical Complement

the following relational operators (with equal precedence):

== Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
!= Not equal to

the following arithmetic operators (in decreasing precedence):

* Multiplication
/ Integer Division
% Remainder
+ Addition
- Subtraction

and the following declarations and initializations:

final boolean X = true;
final double Y = 7.0;
final int Z = 7;

what type must be each of the following variables on the left side of the assignment operator?

  1. b = (5 > Z);


  2. i = (5 % 7);


  3. b = (X || false);


  4. b = (5 < 7) && (6 == 3);


  5. d = (5 / Y);


5. Combining Operators of Different Kinds: Given the following logical operators (with equal precedence):
&& Logical AND
|| Logical OR
! Logical Complement

the following relational operators (with equal precedence):

== Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
!= Not equal to

and the following arithmetic operators (in decreasing precedence):

* Multiplication
/ Integer Division
% Remainder
+ Addition
- Subtraction

evaluate the following expressions or indicate the errors they contain:

  1. (5 >= 5) && ((10 > 10) || (3 == 3))


  2. (5 % 3) + (8 / 16)


  3. 3 < 5 < 7


  4. 5 == ((5 / 2) * 2)


  5. 5 > 3 && 2


6. A Programming Exercise:
  1. Write a simple Java application that checks your answers to the previous section. (Hint: You can print a boolean variable b using the println() function as follows: System.out.println(b);.)


  2. Compile and execute it.
7. Programming Patterns: This part of the lab will give you some experience with programming patterns involving logical and relational operators (as well as arithmetic operators).
  1. Letting the days of the week be denoted by the integers 0 through 6 (starting with Sunday), the int variable today denote the integer corresponding to today and letting the int variable due denote the day of the week (during this week) that an assignment is due, write an expression that evaluates to true when the assignment is late. (Note: You should assume that we are only concerned with a single week.)


  2. Continuing with the previous example, let the boolean variable late denote whether the assignment is late or not. Write a statement that assigns the value true to late when the assignment is late.


  3. Letting the days of the week be denoted by the integers 0 through 6 (starting with Sunday), the int variable today denote the integer corresponding to today and letting the int variable days denote the number of days in the future we are interested in, and the int constant DAYS_PER_WEEK denote the numer of days in a week, write an expression that evaluates to true if the day in the future is the same day of the week as today.


  4. Write an expression that evaluates to true if the two right-most digits of the int named year are 13. (You may assume that year has four digits.)


  5. Write an expression that evaluates to true if the double named salary has exactly "seven figures" (i.e., 7 digits).


Copyright 2013