teaching machines

CS1: Lecture 21 – If Ladders

October 21, 2019 by . Filed under cs1, fall 2019, lectures.

Dear students,

Our programs have completed optional work with the diversion pattern. Our programs have chosen between two actions with the bifurcation pattern. Today we look at the making n-way choices with the ladder pattern.

Ladders

Suppose you’ve got an app that measures the pH of something—the food your about to ingest, perhaps. You want to know whether its acidic, basic, or neutral. To code this up, we will create what I called an if ladder, which looks like a series of more than two choices but which is really just an extension of the two-choice if/else structure.

It is legal to write conditionals without curly braces. Like this:

if (i < 0)
  System.err.println("That's not enough.");

But this is only allowed when the block is a single statement. What happens if we have multiple statements without braces?

if (6 == 7)
  System.out.println("a");
  System.out.println("b");

The second println is not actually part of the conditional. It just looks like it because of our indentation. Accordingly, many corporate style guides mandate braces all the time, even when the block consists of only one statement. Google does.

However, in the case of an if ladder, which nests if statements inside the else blocks, we generally prefer eliminating the braces. The structure that emerges looks much more like an n-way choice.

As another example, let’s write an application that we all need from time a time: a restaurant chooser. We think we like choice, but when it comes time to make a decision, most of us would like to not have that responsibility. So, we will give that responsibility over to our app. However, we will exert some influence. We’ll decide how likely it is a certain restaurant will be picked.

Word of Caution

We live in a world in which decisions are based on boolean criteria. To be eligible for a home loan, you have to pass a series of checks established by your bank. To get a visa to stay in another country, you have to be in good physical health, have a certain kind of job, and be insured. In the name of making decision-making objective and efficient, we cast these questions into software. Our systems drop people in buckets. For this to work, we reduce humans to a credit score, an income, an age, and a buying history. Our software doesn’t see full human beings, full of complexity and nuance.

Anything that makes decisions becomes an authority, a judge. When we make software a judge, we fool ourselves into thinking that the decisions it makes will be fair. This is not so. The criteria we code up comes from broken social systems and expediency. If we don’t get the criteria right, our software will reinforce the prejudice and privilege that we confuse for failure and success. Please make sure your criteria doesn’t deepen the divide between skin colors, genders, and languages. Humans should be evaluated by our software as individuals rather than members of a group.

TODO

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

See you next class!

Sincerely,

P.S. It’s time for a haiku!

This version costs less
Goldilocks OR the Three Bears
They cut out the bears

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

Birthday.java

package lecture1021.cs145;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class Birthday {
  public static void main(String[] args) throws FileNotFoundException {
    File file = new File("/Users/johnch/Desktop/birthday.txt");
    Scanner in = new Scanner(file);
    int month = in.nextInt();
    int day = in.nextInt();

    GregorianCalendar today = new GregorianCalendar();

    System.out.printf("%d %d%n", month, day);
    System.out.printf("%d %d%n", today.get(Calendar.MONTH), today.get(Calendar.DAY_OF_MONTH));

    if (today.get(Calendar.MONTH) + 1 == month &&
        today.get(Calendar.DAY_OF_MONTH) == day) {
      System.out.println("🎂");
    } else {
      System.out.println("\uD83D\uDE4D");
    }
  }
}

Litmus.java

package lecture1021.cs145;

public class Litmus {
  public static void main(String[] args) {
//    if (66 == 67)
//      System.out.println("samesies");
//    System.out.println("still samesies");
    System.out.println(litmus(1));
    System.out.println(litmus(7));
    System.out.println(litmus(7.500000000001));
  }

  public static String litmus(double pH) {
    if (pH < 6.5) {
      return "acid";
    } else if (pH <= 7.5) {
      return "neutral";
    } else {
      return "base";
    }
  }
}

Randaurant.java

package lecture1021.cs145;

import java.util.Random;

public class Randaurant {
  public static void main(String[] args) {
    System.out.println(getRandomRestaurant());
  }

  public static String getRandomRestaurant() {
    Random generator = new Random();
    int i = generator.nextInt(100000) + 1;
    if (i <= 25) {
      return "Taco Bell";
    } else if (i <= 33) {
      return "Milwaukee Burger Company";
    } else if (i <= 43) {
      return "Culver's";
    } else if (i <= 52) {
      return "Acoustic Cafe";
    } else if (i <= 82) {
      return "Five Guys";
    } else {
      return "420 Internet Cafe";
    }
  }
}

Randaurant.java

package lecture1021.cs148;

import java.util.Random;

public class Randaurant {
  public static void main(String[] args) {
    System.out.println(getRandomRestaurant());
  }

  public static String getRandomRestaurant() {
    Random generator = new Random();
    int i = generator.nextInt(100);
    if (i < 30) {
      return "Ban Thai";
    } else if (i < 40) {
      return "Olive Garden";
    } else if (i < 70) {
      return "Azul Tequila";
    } else if (i < 73) {
      return "Taco John's";
    } else {
      return "Five Guys";
    }
  }
}