teaching machines

CS 145 Lecture 16 – Diversions


  • what ?s
  • boolean bingo
  • what does this do?
  • computer as pilot


  • Read chapter 4 through section 4.1 in your book. On a 1/4 sheet, write down 2-3 questions, observations, or examples of conditional sequences you see in your life.
  • For extra credit participation: Read about the bitwise operators AND (&), OR (|), XOR (^), and NOT (~) at Wikipedia or elsewhere. These operators are similar to the boolean operators, except they apply to integers and yield an integer. They work by applying standard boolean logic separately to each bit of the operands.

    The numbers 0 through 15 can be generated by applying the bitwise operators to a set of given numbers. Suppose we are given the set of numbers 1 and 2. 1 is 1 in binary, and 2 is 10. Let’s pad these numbers with leading zeroes to make thinking about bitwise operations easier:

    • 1 = 0001
    • 2 = 0010

    What other numbers in [0, 15], then, can we generate using these numbers and the given operators? Here are a few:

    • ~1 = 1110 = 14
    • ~2 = 1101 = 13
    • 1 | 2 = 0011 = 3
    • 1 & 2 = 0000 = 0
    • 1 & ~2 = 0001 = 1 (not helpful, we already had 1)
    • ~1 & 2 = 0010 = 2 (not helpful, we already had 2)
    • 1 ^ 2 = 0000 = 0 (not helpful, we already had 0)
    • and so on…

    Choose a set of given numbers and combine them using the four operators to produce all numbers in [0, 15]. Share your numbers and expressions on a separate 1/4 sheet turned in next lecture.

    For an extra challenge, find the smallest set of starting numbers you can.


Today we round out our discussion of the Computer as a Philosopher with two short activities: boolean bingo and an “operating table.” First, we’ll follow these steps for boolean bingo:

  1. Create a 3×3 board of boolean expressions:
    1. Each expression must reference booleans a, b, and c exactly once. Use any operator that works with booleans. For example, one might write a && b && !c.
    2. The operation with the lowest precedence must be &&. For example, a || b || c is not legal, but (a || b) && c is. Without this requirement, the game is too easily won.
  2. I will randomly generate values for a, b, and c. Cross off any expressions that are true for a given set of values.
  3. First folks with a completed board win. Of course, the real winners are the folks who strengthen their ability to evaluate boolean expressions.

Second, we’ll complete an operating table. I’ll share a class with getTrue and getFalse methods that have a side effect of printing t or f, and we’ll call them from main. Your task is to complete the tables below for the different permutations of calls combined by the two binary logical operators:

 && getTrue()  && getFalse()
 || getTrue()  || getFalse()

We next segue into treating the Computer as a Pilot, flying through our code, steering clear of obstacles and performing loop de loops. Just like a pilot, our software often has to make decisions. If the user speaks Japanese, buttons must use Japanese characters. But if the user speaks German, it must show German. Or Spanish. Or English. It must choose the translation to use based on user’s preferences.

Additionally, our software tends to do the same thing over and over again: a stoplight goes on forever through its red-green-yellow dance, an MP3 player cycles through our playlists, and a brute force password cracker generates password after password until it gains access. How can we write software that navigates these worlds full of decisions and repetition? Using conditional statements and loops, we can make software that is sensitive to the world’s complexities.

We start by looking at the very simplest decision structure available to us in Java: an if statement. Using a plain old if, we can introduce a diversion in our code, some extra little computation that needs to be done only when certain criteria is met. The diversion pattern looks like this in Java:

// statements before the diversion
if (booleanExpression) {
  // statements executed only when criteria is met
// statements after the diversion

We’ll implement the following methods using a diversion pattern:

  • absolute value, a la the Math class
  • sanitize a URL by prepending http:// if necessary
  • pluralize a message if n refers to a plurality

Here’s an exercise for you:

Write a method split that accepts parameters for a number of items and a number of groups. Return how many items are in each complete group. But if there are leftovers, print a warning message to System.err.

Next week we’ll expand the patterns to bifurcations and n-way choices.



package lecture1009;

import java.util.Random;

public class RandomABC {
  public static void main(String[] args) {
    Random g = new Random();
    System.out.println("a = " + g.nextBoolean());
    System.out.println("b = " + g.nextBoolean());
    System.out.println("c = " + g.nextBoolean());


package lecture1009;

public class OperatingTable {
  public static boolean getTrue() {
    return true;
  public static boolean getFalse() {
    return false;
  public static void main(String[] args) {
    boolean b = getFalse() || getFalse();


package lecture1009;

public class Abs {
  public static void main(String[] args) {
    System.out.println(abs(-5 * Math.PI));

  public static double abs(double x) {
    if (x < 0) {
      x = -x;
    return x;


package lecture1009;

public class Sanitizer {
  public static void main(String[] args) {
  public static String sanitizeURL(String url) {
    if (!url.startsWith("http://")) {
      url = "http://" + url;
    return url;


package lecture1009;

public class Pluralizer {
  public static void main(String[] args) {

  // You have n messages 0, 2, 3, 4, 5
  // You have 1 message 1
  public static String getMessage(int n) {
    String announcement = "You have " + n + " message";
    if (n != 1) {
      announcement += "s";
    return announcement;


on conditional love:
“I do. If he does.”
The priest wouldn’t accept that
We’re back to dating


Leave a Reply

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