RU beehive logo ITEC dept promo banner
ITEC 120
2008spring
ibarland,
jdymacek

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lect12b
while loops
continued

Review

When writing a while-loop, keep the same things in mind as when writing a for-each loop:

We finished last lecture having walked through

/** Announce a countdown, starting at 10.
 * @return A countdown starting at 10.  
 *   That is, "10...9...8... [etc] 2...1...Liftoff!"
 */
public String countdown() {
  String chantSoFar = "";  /* A local variable, to accumulate the answer. */
  int t = 10;         /* The next number to count down. */

  while (t >= 0) {
    chantSoFar = chantSoFar + (t + "...");
    t = t-1;
    }

  return (chantSoFar + "Liftoff!");
  }
In order to walk through this, we need to keep track of what chant and t are, in the environment.

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; .jar

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.

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2008, Ian Barland, Radford University
Last modified 2008.Jan.24 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme