Lesson 7 - Activity 2 - Can you use logical operators?

Time: 15 minutes

Purpose: This activity helps you get familiar with using logical operators

Introduction: Youโ€™re had experience using relational operators, next step is logical operators and what they output

Task: Declare bool1, bool2, bool3 as Booleans.
Set bool1 and bool2 to true, bool3 to false.

  1. Print the value of bool1 AND bool2
  2. Print the value of bool1 AND bool 3
  3. Print the value of bool1 OR bool 2.
  4. Print the value of bool1 OR bool 3.
  5. Print the value of NOT bool1.
  6. Print the value โ€ฆ generate an AND of bool1 and bool2. Process this result in an OR with bool3.
    boolean bool1, bool2, bool3;
    bool1 = bool2 = Boolean.TRUE;
    bool3 = Boolean.FALSE;

    System.out.println(newLine + "Given: " + newLine + "bool1 = " + bool1 + newLine +
            "bool2 = " + bool2  + newLine + "bool3 = " + bool3);

    booleanResult = (bool1 && bool2);
    System.out.println("Then bool1 && bool2 is: " + booleanResult);

    booleanResult = (bool1 && bool3);
    System.out.println("Then bool1 && bool3 is: " + booleanResult);

    booleanResult = (bool1 || bool2);
    System.out.println("Then bool1 || bool2 is: " + booleanResult);

    booleanResult = (bool1 || bool3);
    System.out.println("Then bool1 || bool3 is: " + booleanResult);

    booleanResult = (!bool1);
    System.out.println("Then !bool1 is: " + booleanResult);

    booleanResult = ((bool1 && bool2)||(bool3));
    System.out.println("(bool1 && bool2)||(bool3) is: " + booleanResult);

Good work, esp with combining multiple operations at the end.