home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs
lect11a
and yet more with `while`
When writing a while-loop, keep these things in mind:
-
Work through an example by hand.
What “so-far” information do you accumulate?
(Make a local-variable to store that info, which will change over time.)
-
What corresponds to the new-information-looking-at-right-now?
(Make a local-variable to store that info, which will change over time.)
-
For each of these two pieces of info,
how do they change from one iteration to the next?
That is,
how do you construct the new values out of the pieces available to you?
We'll apply these steps to the following problem(s).
Exercise
In bowling, pins are arranged in a triangle with four layers;
there are a total of 10 pins.
You might be inspired on the weekends to make up your own
bowling formats, lining up empty bottles of Grape Nehi,
and realizing that you aren't confined to only four rows;
you might have 5 rows (requiring 5 more empties, for a total of 15),
or 6 rows (requiring yet 6 more empties, for a total of 21).
Of course, such home-grown bowling is only interesting for so long,
and then you start wondering,
in general, how many pins are needed for bowling-variants with other
numbers of rows?
o ( 1 pin in a 1-high triangle)
o o ( 3 pins in a 2-high triangle)
o o o ( 6 pins in a 3-high triangle)
o o o o (10 pins in a 4-high triangle)
o o o o o etc.
o o o o o o
That is, for a triangle with n rows, how many pins are needed?
Write the method triangularNumber
(or “tN”), which
returns the nth triangular number
Before launching into the code, let's compute some examples/test-cases
by hand, paying attention to our own thought process;
that will give us a clue as to how to compute the general case.
/** Return the nth triangular number.
* @param n The number of rows in a triangle.
* @return The number of pins needed to set up a n rows of pins,
* each row containing one more pin than the previous.
* That is, return 1+2+3+...+(n-1)+n.
* Test cases:
* triangularNumber(4) = 10
* triangularNumber(6) = 21
* ...other tests?...
*/
|
When computing this myself, I find that I am keeping track
of two pieces of information:
the total number of pins needed so far,
and
which row-number I'm currently working on.
(There is also the parameter n, given to me.)
teaching note:
Be sure to run a couple of
live tests of this, to set up triangularPyramid,
which will call this method.
Solution;
Exercise
Consider stacking oranges.
This isn't two-dimensional like bowling pins sitting on the floor;
oranges get stacked in pyramids.
Every pyramid begins with a single orange at its peak,
but there are several ways to extend the pyramid.
-
You could have square pyramids (like the Egyptian and Mayan tombs),
one peak orange (layer 1) is supported by
four oranges (layer 2), which is supported by
nine oranges (layer 3), which is supported by
sixteen oranges (layer 4), etc..
The cross-section of the nth layer is an n×n square.
Write a program to compute the number of oranges in a square pyramid of height n.
-
If you want your stack to look tall, then you might instead use
triangular pyramids (tetrahedra):
The top layer has one orange,
the next has three oranges,
the next has a triangle of six oranges,
the next has a triangle of ten oranges,
etc..
What is the general pattern, for the number of oranges on the nth layer?
Hint: if the general case involves a triangularNumber,
then your code should call that previous function!
(Unless you are clever enough to find a
closed-form formula for tetrahedral numbers.)
Write a program to compute the number of oranges in a triangular pyramid of height n,
using a while-loop.
If we finish these examples,
we will talk about:
- Read numbers from keyboard (as long as hasNextInt()),
and compute the min and max.
(What do our test cases tell us,
about the fewest number of times our loop might happen?
One — because the max of zero items isn't defined!)
-
Walking through characters in a string — find
the largest character.
-
Loops which might stop “early”:
Does a string contain the letter 'X'?
Note that we still have an accumulator “so-far” variable
and an index variable — but we can get fancier in our condition
if we want.
Does the program run much quicker, when we are fancy and stop earlier?
(For what inputs is it a big win?)
home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs