teaching machines

CS 145 Lecture 14 – Logical Operators

October 7, 2016 by . Filed under cs145, fall 2016, lectures.

Dear students,

Our computer can now ponder our data. It can examine order and equality, two operations at the root of all decision making. Before we sign up for something, we ask ourselves if the benefit exceeds the cost. We compare two brands of pasta on price and weight. We scan the details of our roommate’s face to see if it matches the blurry one we saw on a police bulletin.

These operators are pretty straightforward, so we won’t dwell on them. But let’s visit a few more quick problems before we jump to our next set of operators:

Is blank?
Is the given string empty?
Is logout?
Is a given String "logout"?
Is all-caps?
Is a given String ALL CAPs?

Now we introduce the logical operators: &&, ||, and !. You should be able to draw upon a deep store of experience as a rational being as we discuss these, but the level of formality in programming makes them just a wee bit stranger.

Parsnip blisters?
Will a person get blisters from wild parsnip?
Mr. Right?
Is a person Mr. Right?
Sleep in?
Can one sleep in today?
Has digit?
Does a given String contain at least one digit?
No attachment?
Is the word “attachment” absent from the body of an email?
Even pizza?
You are having a party for some friends who struggle with jealousy. One friend may not be able to make it. Can you divide the slices of pizza evenly amongst all guests?
You turn over three playing cards. Do two of them add up to the third?
You normally don’t answer your phone in the mornings—unless it’s a parent calling you. But you never answer if you’re asleep. The phone is ringing now. Do you answer?
Is presidential?
Are you eligible to be president of the United States?

We will examine these operators through the lens of a truth table. Truth tables enumerate all possible combinations of the inputs and show the resulting outputs. For instance, here is AND’s truth table:

a b a && b
0 0 0
0 1 0
1 0 0
1 1 1

And OR’s:

a b a || b
0 0 0
0 1 1
1 0 1
1 1 1

And NOT’s:

a !a
0 1
1 0

What are the truth tables for these expressions?

Here’s your TODO list to complete before we meet again:

See you next class!


P.S. It’s Haiku Friday!

I nailed question three
“Is it A, B, C, or D?”
It certainly is

P.P.S. Here’s the code we wrote together in class…


package lecture1007;

public class StringBlank {
  public static void main(String[] args) {
    String s = "asdf";
    System.out.println(s.length() == 0);


package lecture1007;

import java.util.Scanner;

public class Logout {
  public static void main(String[] args) {
//    Scanner in = new Scanner(System.in);
//    System.out.print("> ");
//    String line = in.nextLine();
//    System.out.println(line == "logout");
//    System.out.println(line.equals("logout"));

  public static boolean isAllCaps(String contender) {
    return contender.toUpperCase().equals(contender);


package lecture1007;

public class Junctions {
  public static void main(String[] args) {
    // System.out.println(getsBlisters(true, true));
    System.out.println(isMrRight(true, false, false, false));


  public static boolean getsBlisters(boolean playedInParsnip, boolean playedInLight) {
    return playedInParsnip && playedInLight;

  public static boolean isMrRight(boolean isTall, boolean isDark, boolean isHandsome, boolean isRich) {
    return (isTall && isDark && isHandsome) || isRich;
  public static boolean canSleepIn(boolean isHoliday, boolean isWeekday) {
    return isHoliday || !isWeekday;