RU beehive logo ITEC dept promo banner
ITEC 120
2008fall
aaray,
ejderrick,
ibarland,
jmdymacek

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive

lect09a
More with `while`

What does the following method return?

/** 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.



Another example of a while loop:
We saw earler, that if we have a source of characters (like the keyboard), we can make a java.util.Scanner who can take those individual characters and turn them into numbers (ints or doubles) or strings for us, with the method nextInt(), nextDouble(), next() (which returns the next word, as a string). There are other useful Scanner methods: nextLine() which returns the entire next line (a String), as well as a boolean method hasNext(), which determines whether the source of characters has run dry.

  /** Open up a URL, and read the input.
   * As a side-effect (for demonstration only), print each line to the console.
   * @return The number of lines in a given URL.
   */
  public static int countLines() throws java.net.MalformedURLException, java.io.IOException {
    String sourcePage = "http://www.radford.edu/itec120/2008spring/";

    // Open sourcePage for input: (Create a URL object based on sourcePage, open that URL as a stream-of-characters,
    //                             and create a Scanner who will group that stream's characters into Strings etc.)
    java.util.Scanner src = new java.util.Scanner( new java.net.URL( sourcePage ).openStream() );

    int linesSoFar = 0;      // Keep track of how many lines we've seen.

    while ( src.hasNext() ) {
      src.nextLine();   // Have the scanner consume the next line.  We ignore its return value.
      linesSoFar = linesSoFar + 1;
      }

    return linesSoFar;
    }
Task: modify this method so that it returns a single String containing the entire contents of the file.
Task

When writing a while-loop, keep these things in mind:

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.

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive


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