Radford University ITEC

ITEC120-ibarland (incl. office hrs)infolectureslabshws

practicing loops:

bottles of beverages

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.

  1. 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”.

    You may use a different drink than beer, if you like (as long as you're willing to drink 99 bottles of it :-).

    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 .

  2. Write a function which takes two numbers, start and stop (with startstop), and returns the verses from start bottles down to but not including stop bottles.
    Write this using a for loop. (If you like, you can first write a while-loop version and get that working, before writing a for-loop version.)
  3. 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.

  4. Write a function which takes no inputs, and returns the entire lyrics to the song. (Hint: use existing functions, to help this task!)
    When you are being checked off, change your named-constant from 99 to 4. (This is also a good debugging tip.)
  5. 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.)

  6. 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.
    "
    While still keeping around your one-argument version which uses beer (or whatever default you choose), write another version which takes in the name of a drink.

    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

Extra-credit is individual work only. You can turn this in (or get it checked off) up through Nov.16 (Fri).

  1. 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).

  2. Your program currently prints half of the lyrics of a song which goes on forever: “…go to the store, buy one more, 1 bottle of beer on the wall” (etc.)!
    Write a program which returns a given number of down-and-up “mega-verses” of this longer song. (That is, one mega-verse is nearly 1000 lines of text.) You still do not want any repeated code, so you'll want to factor out the common part of the verse structure into its own function, and pass in the varying part as an argument.

Oct.26 (Fri)

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)infolectureslabshws


©2006, Ian Barland, Radford University
Last modified 2006.Nov.13 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme