![]() |
![]() |
|
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
(We'll divide the class into groups, w/ a pair of representatives at the board.)
Complete the following table,
for each of the following
code snippets.
|
|
||
|
|
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 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.
*/
/** Some automated test cases for functions in this file.
*/
void testLoops() {
testEqualInts( triangularNumber(4), 10 );
testEqualInts( triangularNumber(6), 10 );
testEqualInts( triangularNumber( ), );
testEqualInts( triangularNumber( ), );
}
|
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.
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.
What is the general pattern, for the number of oranges on the nth layer?
More generally, you might wonder:
“Why does Java provide a
If we finish these examples, we will talk about:
The syntax of a
How to keep track of where you are in the loop?
-- or, keep track of the particular item you're
considering as you go through the loop another time.
Each time through the loop, how will you update your index?
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.
Cool Computer Science idea:
The crystal structure of graphite consists of layers of a hexagonal mesh -- strong connection within
the a single layer, but the layers just kinda nestle on top of each other (similar to our oranges).
(This is why graphite writes: the layers are sheeting off of each other, laying down onto the paper.)
But when you stack the layers, there are actually two possibilities:
a layer can be directly above the layer two levels down (as pictured),
or it can be offset by half a hexagon.
[When adding a new layer, you have two ways to offset the new hexagons — offset the same
way as the preceding one, or differently.]
Thus, each layer can be viewed as a 0 (directly above the two-layer-below version),
or as a 1 (offset half a hexagon from two layers below).
A computer scientist wants to construct a chunk graphite layer-by-layer, so that
it has a message in it!
A spy can give a pencil to their contact,
and the message is not written down anywhere — it is already encoded in the pencil's graphite!
(Moreover, the message could be encrypted — so even if interecepted, and even if
the interceptor looked at the crystal's graphite structure, it would look random.
The art of hiding messages inside innocuous files/objects is called
“steganography”.)
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
| ©2012, Ian Barland, Radford University Last modified 2012.Nov.02 (Fri) |
Please mail any suggestions (incl. typos, broken links) to ibarland |
![]() |