teaching machines

CS 1: Lecture 14 – Logical Operators

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:

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.

  • In range?
    Is a number in a given range?
  • Is gray?
    Is a String gray in American/British English?
  • Is vowel?
    Is a letter a vowel?
  • Parsnip blisters?
    Will a person get blisters from wild parsnip?
  • Is vowel?
    Is a letter a vowel?
  • Is Food Servable?
    Can you bring this dish to a potluck where you will be feeding strangers with unknown allergies?
  • Sleep in?
    Can one sleep in today?
  • Has digit?
    Does a password 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 or may not be able to make it, which makes planning difficult but not impossible. Can you divide the slices of pizza evenly amongst all guests, so as to arouse no one’s jealousy?
  • Mathch?
    You turn over three playing cards. Do two of them add up to the third?
  • Answer?
    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?

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

  • The first midterm is Monday. You may bring a single sheet (front and back) of 8.5 by 11″ paper with notes on it. Possible exam topics include data types, variables, operators, methods, String, Random, Scanner, Color, and Math. The problems on the exam will be similar to ones you’ve seen in class and on homework.
  • CS 145, we will not have lab next Tuesday. CS 148, we will not have lab next Thursday. I will be grading.
  • A reminder that will save you considerable headache: commit and push after every coding session. That way you save yourself from broken computers, forgotten deadlines, and Bitbucket configuration issues. I don’t know how to convince you to do this. Trouble is a key ingredient to learning, but you must leave yourself enough time to recover.

See you next class!

Sincerely,

P.S. It’s time for a haiku!

It’s opposite day
But don’t tell anybody
Then it wouldn’t be

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

Relational.java

package lecture1006;

import java.util.Scanner;

public class Relational {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter your stuff: ");
    String line = in.nextLine();
    System.out.println(isLogout(line));
  }

  public static boolean isEmpty(String s) {
    return 0 == s.length();
  }

  public static boolean isBlank(String s) {
    return s.trim().length() == 0;
  }
  
  public static boolean isLogout(String command) {
//    return command == "logout";
    return command.equals("logout");
  }
}

Logical.java

package lecture1006;

public class Logical {
  public static void main(String[] args) {
//    System.out.println(isInRange(1, 20, 11));
//    System.out.println(isGray("gray"));
//    System.out.println(isGray("grey"));
//    System.out.println(isGray("yellow"));
//    System.out.println(isGray("cyan"));
//    System.out.println(isBlistery(false, true));
    System.out.println(isMathch(5, 8, 3));
  }

  public static boolean isMathch(int a, int b, int c) {
    return a + b == c || b + c == a || a + c == b;
  }

  public static boolean isBlistery(boolean isNight, boolean isTouched) {
    return isTouched && !isNight;
  }

  public static boolean isInRange(int lo, int hi, int n) {
    return n >= lo && n <= hi;
  }

  public static boolean isGray(String color) {
    return color.equals("gray") || color.equals("grey");
  }
}

Relational.java

package lecture1006;

import java.util.Scanner;

public class Relational {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter stuff: ");
    String line = in.nextLine();
    System.out.println(isGray(line));
  }

  public static boolean isBlank(String s) {
//    return s.trim().length() == 0;
    return isEmpty(s.trim());
  }

  public static boolean isEmpty(String s) {
    return s.length() == 0;
  }

  public static boolean isLogout(String s) {
//    return s == "logout";
    return s.equals("logout");
  }

  public static boolean isGray(String s) {
    return s.equals("gray") || s.equals("grey");
  }

  public static boolean isVowel(char c) {
//    return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
    String vowels = "aeiou";
    return vowels.contains(c + "");
  }

  public static boolean isMathch(int a, int b, int c) {
    return a + b == c || a + c == b || b + c == a;
  }

  public static boolean isSplittable(int nFriends, int nSlices) {
    return nSlices % nFriends == 0 && nSlices % (nFriends - 1) == 0;
  }

  public static boolean isBlistery(boolean isNight, boolean isExposed) {
    return !isNight && isExposed;
  }

  public static boolean isInRange(int lo, int hi, int n) {
    return lo <= n && n <= hi;
  }
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *