teaching machines

CS 145 Lecture 16 – Diversions

October 9, 2015 by . Filed under cs145, fall 2015, lectures.

Agenda

TODO

Note

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()
 || 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:

/usr/lib/ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'coderay' (>= 0) among 56 total gem(s) (Gem::MissingSpecError)
Checked in 'GEM_PATH=/.gem/ruby/2.7.0:/var/lib/gems/2.7.0:/usr/lib/ruby/gems/2.7.0:/usr/share/rubygems-integration/2.7.0:/usr/share/rubygems-integration/all:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.7.0:/home/johnch/.gems', execute `gem env` for more information
	from /usr/lib/ruby/2.7.0/rubygems/dependency.rb:323:in `to_spec'
	from /usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_gem.rb:62:in `gem'
	from ./coderay:24:in `
'

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

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.

Code

RandomABC.java

/usr/lib/ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'coderay' (>= 0) among 56 total gem(s) (Gem::MissingSpecError)
Checked in 'GEM_PATH=/.gem/ruby/2.7.0:/var/lib/gems/2.7.0:/usr/lib/ruby/gems/2.7.0:/usr/share/rubygems-integration/2.7.0:/usr/share/rubygems-integration/all:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.7.0:/home/johnch/.gems', execute `gem env` for more information
	from /usr/lib/ruby/2.7.0/rubygems/dependency.rb:323:in `to_spec'
	from /usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_gem.rb:62:in `gem'
	from ./coderay:24:in `
'

OperatingTable.java

/usr/lib/ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'coderay' (>= 0) among 56 total gem(s) (Gem::MissingSpecError)
Checked in 'GEM_PATH=/.gem/ruby/2.7.0:/var/lib/gems/2.7.0:/usr/lib/ruby/gems/2.7.0:/usr/share/rubygems-integration/2.7.0:/usr/share/rubygems-integration/all:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.7.0:/home/johnch/.gems', execute `gem env` for more information
	from /usr/lib/ruby/2.7.0/rubygems/dependency.rb:323:in `to_spec'
	from /usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_gem.rb:62:in `gem'
	from ./coderay:24:in `
'

Abs.java

/usr/lib/ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'coderay' (>= 0) among 56 total gem(s) (Gem::MissingSpecError)
Checked in 'GEM_PATH=/.gem/ruby/2.7.0:/var/lib/gems/2.7.0:/usr/lib/ruby/gems/2.7.0:/usr/share/rubygems-integration/2.7.0:/usr/share/rubygems-integration/all:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.7.0:/home/johnch/.gems', execute `gem env` for more information
	from /usr/lib/ruby/2.7.0/rubygems/dependency.rb:323:in `to_spec'
	from /usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_gem.rb:62:in `gem'
	from ./coderay:24:in `
'

Sanitizer.java

/usr/lib/ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'coderay' (>= 0) among 56 total gem(s) (Gem::MissingSpecError)
Checked in 'GEM_PATH=/.gem/ruby/2.7.0:/var/lib/gems/2.7.0:/usr/lib/ruby/gems/2.7.0:/usr/share/rubygems-integration/2.7.0:/usr/share/rubygems-integration/all:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.7.0:/home/johnch/.gems', execute `gem env` for more information
	from /usr/lib/ruby/2.7.0/rubygems/dependency.rb:323:in `to_spec'
	from /usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_gem.rb:62:in `gem'
	from ./coderay:24:in `
'

Pluralizer.java

/usr/lib/ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'coderay' (>= 0) among 56 total gem(s) (Gem::MissingSpecError)
Checked in 'GEM_PATH=/.gem/ruby/2.7.0:/var/lib/gems/2.7.0:/usr/lib/ruby/gems/2.7.0:/usr/share/rubygems-integration/2.7.0:/usr/share/rubygems-integration/all:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.7.0:/home/johnch/.gems', execute `gem env` for more information
	from /usr/lib/ruby/2.7.0/rubygems/dependency.rb:323:in `to_spec'
	from /usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_gem.rb:62:in `gem'
	from ./coderay:24:in `
'

Haiku

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