Activity 2.4 - Understanding relational and logical operators

Topic

This activity looks at relational and logical operators.

Materials

In addition to the discussion of the solution in the Solution document, the project for this activity contains two classes:

Task

The method checkTruthValues defines some primitive data values and then evaluates and prints out six expressions. At present, every expression evaluates to the boolean value false. Your job is to alter one of the operators, but not the variables, in each of these expressions, so that the new versions all then evaluate to true.

To give an example of what we mean, take the statement

x = (A == B) || D;

If A were 10, B were 42 and D boolean false, the expression on the right-hand side would evaluate to false. One way to make it evaluate to true by changing one operator would be to change the statement to

x = (A != B) || D;

There are usually several ways of achieving a true expression by changing one operator. For example, we could also have said:

x = (A < B) || D;

Instructions

  1. Run the project and check that all the expressions evaluate to false to begin with.
  2. Open the class Ex2_4 and edit the method checkTruthValues.
  3. You can test your changes by re-running the project.
  4. Note that there may be other solutions than the ones we give.