CS 145 Lecture 18 – Bifurcations and N-way Choices
Agenda
- what ?s
- more bifurcations
- n-way choices
- restaurand
TODO
- Complete some problems from the Logic-1 section of Coding Bat. Write down the names of the problems you solve (and any other interesting tidbits of your thought) on a 1/4 sheet for Friday.
Note
Last time we introduced the bifurcation pattern, where we choose between two mutually exclusive paths through our code. Let’s continue to examine it through a series of examples:
- a zigzag pattern in Madeup
- a sunshine pattern in Madeup
- a method that computes the minimum of two numbers
- a method that computes the overall price of an item, including tax if necessary
- a method that determines if a String’s last character is
!
- a method that given a month and day, tells what weekday that day is in the current year
We also extend this idea to making n-way choices. We’ll see that there’s nothing in Java’s if
beyond bifurcations, but we can nest them such that it appears as though we were choosing between n alternatives.
Code
Bifur.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 `'
Restaurand.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
No sense counting them
Just mod
If odd, he’s the one
Just mod
nPetals
by 2If odd, he’s the one