# 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:

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?
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:

• Read sections 5.3 through 5.4 in your book. On a quarter sheet, identify 3 compound truths of life using the logical operators &&, ||, and !. For example:
boolean isEdible = isCooked || !isMeat;
• Homework 3 is posted, which is due before October 26. Some fixes have been applied to the PDFs and SpecCheckers, so be sure to do a Team / Pull… from the twodee remote in Eclipse. The steps are described in Homework 0, Part 3.
• The next lab is posted. We will have a review session for homework 2, which you must attend to receive your Blugolds. Do not attend if you did not receive credit. If you resubmit during the amnesty period, you will have a chance at a second review.
• Code.org is a non-profit whose mission is to get kids learning about computer science at a young age. On Friday, October 14, they are holding a workshop at a local elementary school, and I will attend. So, no class Friday!

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