CS 145 Lecture 16 – Diversions
Agenda
- what ?s
- boolean bingo
- what does this do?
- computer as pilot
TODO
- 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.
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:
- Create a 3×3 board of boolean expressions:
- Each expression must reference booleans
a
,b
, andc
exactly once. Use any operator that works with booleans. For example, one might writea && b && !c
. - 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.
- Each expression must reference booleans
- I will randomly generate values for
a
,b
, andc
. Cross off any expressions that are true for a given set of values. - 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:
- 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 toSystem.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
“I do. If he does.”
The priest wouldn’t accept that
We’re back to dating