teaching machines

CS 145 Lecture 15 – Logic Cont’d

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

Dear students,

We’ve had an exam, which is mostly graded but not quite. I will hand them back in lab next Tuesday so that you have time to ask questions. Instead, we will carry on with our discussion of the logical operators. Let’s start with some Blackboxes:

Blackbox #1
Blackbox #2
Blackbox #3

Then, let’s think deeply about how to employ these operators to create some handy methods:

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?
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?
Picky?
You only drink caffeine-free or diet soda, but not caffeine-free diet soda. Will you drink a given soda?

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

See you next class!

Sincerely,

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

Exercises.java

package lecture1012;

import java.io.File;

public class Exercises {
  public static void main(String[] args) {
    System.out.println(isEvenPizza(24, 6));
  }

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

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

  public static boolean isDrinkable(boolean isCaffeinated, boolean isDiet) {
    // return isCaffeinated ^ isDiet;
    return isCaffeinated != isDiet;
  }
  
  public static boolean isPresident(boolean isRebel, int age, int nYearsResident, int nTerms, boolean isNBC) {
    return !isRebel &&
         age > 34 &&
         nYearsResident >= 14 &&
         nTerms < 2 &&
         isNBC;
  }
}