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 logout?
Is 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?
Mr. Right?
Sleep in?
Has digit?
No attachment?
Even pizza?
Mathch?
Answer?
Is presidential?

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!

Sincerely,

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…

StringBlank.java

package lecture1007;

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

Logout.java

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"));
    
    System.out.println(isAllCaps("FOO1"));

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

Junctions.java

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;
  }
}