Title: More Boolean Concepts
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

More Boolean Concepts

1. For the String class, the == operator doesn't quite work as expected. We need the equals(String other) method
2. The String class also has other methods:
equalsIgnoreCase(String other)
compareTo(String other)


import java.util.Scanner;


public class BooleanLogic {

    public static void main(String[] args) {
        String s1="batman";

        System.out.println("Enter a word to compare with: "+ s1);
        // Try batman, batmanager, batwoman, BATMAN


        Scanner keyboard = new Scanner(System.in);
        String response = keyboard.next();

        // Normally never do the following
        if (response == s1)
            System.out.println("== : Your response matched "+s1);

        if (response.equals(s1))
            System.out.println("equals : Your response matched "+s1);

        if (response.equalsIgnoreCase(s1))
            System.out.println("equalsIgnoreCase :  Your response matched "+s1);

        int value = response.compareTo(s1);
        System.out.println("compareTo value : " + value);
        if (value < 0)
            System.out.println(response +" precedes " + s1);
        else if (value > 0)
            System.out.println(response +" follows " + s1);
        else // must be a zero
            System.out.println(response +" must = "+ s1);


        System.out.println("Done");

    }

}



3. Suppose you want to ask if "x" is in the following range:
5 < x <= 20
4. && the AND operator between 2 booleans - True if both booleans are true, otherwise false
5. || the OR operator between 2 booleans - False if both booleans are false, otherwise true
6. ! operator - Reverses the logic

7.
import java.util.Scanner;


public class BooleanLogic {

    public static void main(String[] args) {

        System.out.println("Enter an integer x and check for 5 < x <= 20");

        Scanner keyboard = new Scanner(System.in);
        int x = keyboard.nextInt();

        if ( (5 < x)   &&  (x <=  20) )
            System.out.println("x is in the range: test 1");
        else
            System.out.println("x is NOT in the range: test 1");

        if ( (5 >= x)   ||  (x >  20) )
            System.out.println("x is NOT in the range: test 2");
        else
            System.out.println("x is in the range: test 2");

        // Remove parenthesis

        if ( 5 < x   &&  x <=  20 )
            System.out.println("x is in the range: test 3");
        else
            System.out.println("x is NOT in the range: test 3");

        if ( 5 >= x   ||  x >  20 )
            System.out.println("x is NOT in the range: test 4");
        else
            System.out.println("x is in the range: test 4");


        if (!( 5 >= x   ||  x >  20) )
            System.out.println("x is in the range: test 5");
        else
            System.out.println("x is NOT in the range: test 5");


        System.out.println("Done");

    }

}



8. Note that in examples 3, 4, and 5, we removed parenthesis and got the same result. So how do I determine when parenthesis are necessary. You might decide to keep the parenthesis for readability.
9. The book provides the following Precedence table from high priority --> Low priority
  1. ()[]
  2. ++ --
  3. casts
  4. * / %
  5. + -
  6. < > <= >=
  7. == !=
  8. &
  9. |
  10. &&
  11. ||
  12. = *= += etc.
10. Short circuit logic with && and ||
11. Java also has the operators & and |. These operators do not have short circuit logic. In the above example this is not a big thing (perhaps a little less efficient). In the above example, replace all of the && with &, and the || with |. You should get the same result. These operators can also be used for integer masking operations:


int x = 21;             // 10101
x = x | 5;              // 10101  |  00101 = 10101
System.out.println("x=" + x);
x = x & 33;             // 010101  &  100001 = 000001
System.out.println("x="+x);


12. && Short circuit logic thinking advantages good and bad


import java.util.Scanner;


public class BooleanLogic {

    public static void main(String[] args) {

        // Enter in the values 1, 100, and 0 to see what you get

        System.out.println("Enter an integer");
        Scanner keyboard = new Scanner(System.in);
        int x = keyboard.nextInt();

        // Good choice
        if (x != 0  && 100/x > 10)
            System.out.println("Expression is True");
        else
            System.out.println("Expression is False");


        // Not a good choice
        if (x != 0  & 100/x > 10)
            System.out.println("Expression is True");
        else
            System.out.println("Expression is False");

        // Not a good choice
        if ( 100/x > 10 && x != 0)
            System.out.println("Expression is True");
        else
            System.out.println("Expression is False");

        System.out.println("Done");

    }



}


13. || Short circuit logic thinking advantages good and bad


import java.util.Scanner;


public class BooleanLogic {

    public static void main(String[] args) {

        // Enter in the values 1, 100, and 0 to see what you get

        System.out.println("Enter an integer");
        Scanner keyboard = new Scanner(System.in);
        int x = keyboard.nextInt();

        // Good choice
        if (x == 0  || 100/x > 10)
            System.out.println("Expression is True");
        else
            System.out.println("Expression is False");

        // Not a good choice
        if (x == 0  | 100/x > 10)
            System.out.println("Expression is True");
        else
            System.out.println("Expression is False");


        // Not a good choice
        if ( 100/x > 10 || x == 0)
            System.out.println("Expression is True");
        else
            System.out.println("Expression is False");

        System.out.println("Done");

    }



}



14. Java has the reserved words true and false.
boolean b = true;
while (true) { .....
15. Enter in the values 2, 22, 23, 19. Analyze how the CPU evaluates these expressions based on the Precedence table.
Put in extra parenthesis where you think it would help the readability.


import java.util.Scanner;

public class BooleanLogic {

    public static void main(String[] args) {

        // Enter in the values 2, 22, 23, 19
        // Analyze how the CPU evaluates these expressions based on the
        // Precedence table.

        System.out.println("Enter an integer");
        Scanner keyboard = new Scanner(System.in);
        int x = keyboard.nextInt();

        //even numbers in the regions x < 5  or x>=20
        if (x % 2 == 0  &&  (x < 5 || x >= 20) )
            System.out.println("x is in set 1");

        //even numbers in the region x < 5  or  any number x >=20
        if (x % 2 == 0  &&  x < 5 || x >= 20 )
            System.out.println("x is in set 2");

        //even numbers in the region x < 5  or  an odd number  x >=20
        if (x % 2 == 0  &&  x < 5 || x % 2 == 1 && x >= 20 )
            System.out.println("x is in set 3");

        System.out.println("Done");

    }

}



Java Practice(JPHI):

(10 points) You are to complete the following program by entering in logic where you see:
***** Put your code here *******


import java.util.Scanner;

public class AnalyzeLetters {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("enter some text");
        String input = keyboard.nextLine();

// Note you can reference the character values 'A', 'Z', '0', '9'
// Also note that in the ASCII character tables, that 'A', 'B', ... 'Z'
//       show up in this order.
// Likewise, for 'a', 'b', ... 'z' and
// '0', '1', ... '9'

        for (int i=0; i < input.length(); i++)
        {
            char c = input.charAt(i);
            if (c >= 'a' && c <= 'z')
                System.out.println(c + " is a lower case letter");
            if (***** Put your code here *******)
                System.out.println(c + " is NOT a lower case letter");
            if (***** Put your code here *******)
                System.out.println(c + " is an upper case letter");
            if (***** Put your code here *******)
                System.out.println(c + " is a digit");
            if (***** Put your code here *******)
                System.out.println(c + " is a digit or a lower case letter");
        }
    }

}


Last Modified: December 6, 2014 2:21 PM