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?
No attachment?
Even pizza?
Mathch?
Answer?
Is presidential?
Picky?

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