teaching machines

CS 1: Lecture 14 – Logical Operators

October 6, 2017 by . Filed under cs1, cs145, cs148, fall 2017, 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:

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.

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

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