ITEC120-ibarland (incl. office hrs)—info—lectures—labs—hws
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 { final String sourcePage = // "http://www.radford.edu" "http://www.radford.edu/~itec120/2006fall-ibarland/" // "file:///Users/ian/120/index.html" ; // 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 lineCount = 0; // Keep track of how many lines we've seen. String pageSoFar = ""; // All the lines of text seen so far. while ( src.hasNext() ) { pageSoFar = pageSoFar + src.nextLine(); lineCount = lineCount + 1; } System.out.println( "We read:\n" + pageSoFar ); return lineCount; } |
What does the following method return?
/** What does this method do? * @return A countdown string. */ public String countdown () { String chant = ""; /* A local variable, to accumulate the answer. */ int t = 10; /* The next number to count down. */ while (t >= 0) { chant = chant + (t + "..."); t = t-1; } return (chant + "Liftoff!"); } |
Oct.17 (Tue):
Challenge: 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?
To think about:
When first working through this by hand,
what pieces of information do you use?
How do they change, from one iteration to the next?
How do you get from one step to the next---exactly how do
you construct the new values out of the pieces available to you?
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 emptyies, 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 so far) 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?... */ |
Optional Exercise— covered over a couple days of lecture:
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.
Digression: Can we make a hexagonal pyramid? Hmm, a layer with seven oranges (one in the middle plus six around the edges) would actually be the third layer of the pyramid; the second layer would have three oranges, just like the triangular case. (And in fact, that hexagon is part of the triangular pyramid's fourth layer.)
Thinking about it more closely, There is a substantial difference between the square pyramids vs. the triangular and some sort of hexagonal pyramid: In a triangular pyramid, each layer is packed in tight. But in the square pyramid, each later in the grid is, in a way, loose:
o o o o o o o o o o o o o o o o o othe square packing on the left-hand-side would be more stable (and smaller) if you jiggled it so that they oranges of a single layer interleaved better. The version on the right is a “dense plane”, and is what our triangular pyramid was built out of.
So both the triangular and a hexagonal pyramid would be built out of dense layers, but even so they are different from each other. It turns out, if you have one dense layer covering the floor, and want to put another dense layer on top of it, there are actually two different ways to do so:
Note that each new layer added has two possible choices. Thus, each layer encodes one bit of information; you can use this to encode messages! One can imagine a James Bond scenario, where examining a diamond earring under a microscope and looking at the series of bits could reveal a message. (Moreover, that message could be well encrypted — if you didn't have the decryption key, you couldn't sure whether the layering represented a secret message, or just noise!)
But perhaps we are starting to digress from the topic of while-loops…
Challenge: figure out what a hexagonal pyramid might be like. How many oranges on the nth level? Write a function to compute the number of oranges needed for a hexagonal pyramid of height n.
ITEC120-ibarland (incl. office hrs)—info—lectures—labs—hws
©2006, Ian Barland, Radford University Last modified 2006.Oct.30 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |