ITEC120-ibarland (incl. office hrs)—info—lectures—labs—hws
99 bottles of beer on the wall,
99 bottles of beer;
take one down,
pass it around,
98 bottles of beer on the wall!
98 bottles of beer on the wall,
98 bottles of beer;
take one down,
pass it around,
97 bottles of beer on the wall!
[etc., ad nauseum]
— anonymous1
Today's program will be checked off (either before you leave today, or at the start of next lab). You may work individually, or with one other person (but not somebody you've already done a 120 lab with in the last couple of weeks).
You may want to refer to lecture notes and code examples for while- and for- loops.
Write a function which takes in a number of bottles, and returns the one corresponding verse (5 lines) of that eternal classic, “99 bottles of beer on the wall”.
This method should not turn pixels of the console
window on/off, so
do not have your method call System.out.println;
your method simply returns a string.
This allows future code to call your function,
and then use the resulting string for whatever it wants
(sending it to a speech-synthesizer,
writing it to System.out,
writing it to a file or a web-page,
or even
email a single verse of the song to your parents each minute for 100 minutes).
If you want to see your result formatted in a screen-specific way
on the console,
you can go to BlueJ's code pad and call
System.out.println yourself,
passing it the result of calling your function.
(See from lecture:
calling static methods.)
Also, when BlueJ's terminal window has focus, a helpful menu selection is Options > Unlimited buffering as well as Options > Clear screen at method call .
Note: if BlueJ's progress-bar is churning away and you think your program is an infinite loop, you can right-click on the progress bar to stop it.
Don't repeat string literals unnecessarily. (That is, if we modify the song to use “mugs of hot tea”, you want to change only one line of your code.)
ITEC120 does not condone underage drinking. It would be nice to provide users with a version of our function which allows the caller to specify what sort of drink they'd like 99 bottles of. For example,
oneVerse( 17, "ginger ale" ) = "17 bottles of ginger ale on the wall, 17 bottles of ginger ale; take one down, pass it around, 16 bottles of ginger ale on the wall. " |
If you like, you can give this new version the same name as your old version, overloading the name. When java sees it being called with one argument, it calls your original version. If java sees somebody calling the method with two arguments, it calls this new version. (The one-argument version is convenient
You should still allow people to call your original (one-argument) versions, but of course you should not have repeated code: your original versions should be nothing but a call your two-argument versions. If you use the same (overloaded) name for the pair, then essentially you have provided a function with an optional argument (whose default value is "beer").In order to get full credit, you should use named constants for the default starting point and the default drink.
Extra-credit is individual work only. You can turn this in (or get it checked off) up through Nov.16 (Fri).
Make sure your output is always grammatical (no “1 bottles”),
and yet you don't repeat data.
(That is, if the song were modified to use carafes instead of bottles,
you should only have to change one word of your program.)
Hint: I have functions verseShortLine(int),
verseLongLine(int),
and
pluralSuffixFor(int).
At the start of class, we'll check off the above program.
Amnesty day: if you have labs that you never did (see WebCT gradebook), I'll check them off (with the 30% late penalty, even though it's long after the standard 3-day late period). A grade with a late penalty is still far better than having a 0 for a lab check-off.
If you have old assignments which you somehow didn't get checked off but were completed on time, and these programs are on your H: drive or memory stick then don't modify those files: Come by office hours (or the PI's office hours), and we'll use the file's timestamp to verify they were done on time, and check them off.
If you are up-to-date and checked off, you can either work on the extra-credit parts of this week's or last week's lab, or you can enjoy the crisp weather.
1Anonymous, and extremely annoying back
ITEC120-ibarland (incl. office hrs)—info—lectures—labs—hws
©2006, Ian Barland, Radford University Last modified 2006.Nov.13 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |