|
home—info—labs—hws—exams
textbook—java.lang docs—java.util docs—archive
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!"); } |
style tip: Don't just make the parameter named t and then assign to it (but mention that approach). Keeping the parameter untouched segues well into the next problem.
Exercise: Without changing the answer being computed, can you change how the answer is arrived at, by t counts from 1 up to (say) 10, and prepends that number to chant?
teaching comment: this was a useful exercise to do in class; or at least, it wasn't obvious to people!
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; } |
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 oThat 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?... */ |
teaching note: Be sure to run a couple of live tests of this, to set up triangularPyramid, which will call this method.
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.
home—info—labs—hws—exams
textbook—java.lang docs—java.util docs—archive
©2008, Ian Barland, Radford University Last modified 2008.Nov.10 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |