Sunday, November 16, 2008

Singing about Beer

Rejoice, for today is an auspicious day. Today, you will be learning about Java while singing about beer.

Let's start with a speed challenge. What happens when you run the following code? Does it compile? Does it run? Is there a small bug somewhere? If so, how quickly can you spot it? How quickly can you fix it?

------------------- BEER 1 -------------------

public class BottlesOfBeer {

int numberBottles;

BottlesOfBeer(int num){
numberBottles = num;
}

public void SingBeerSong() {

String bottle = "bottles";

while (numberBottles > 0) {

if (numberBottles ==1){ bottle = "bottle"; }
System.out.println (numberBottles + " " + bottle + " of beer on the wall...");
System.out.println (numberBottles + " " + bottle + " of beer! ");
System.out.println ("Take one down and pass it around");
numberBottles--;

System.out.println (numberBottles + " " + bottle + " of beer on the wall!\n");

}
} // end of SingBeerSong

public static void main(String[] args){

BottlesOfBeer b = new BottlesOfBeer(99);
b.SingBeerSong();

}
} // End of BottlesOfBeer Class

------------------- BEER 2 -------------------

Here's a less concise version:

27 public class NinetyNineBottles {
28
29 private static int NUMBER_BOTTLES_OF_BEER_ON_THE_WALL = 99;
30
31
32 /**
33 * Print the 99 bottles of beer song to the console.
34 */
35 public void printSong()
36 {
37 int nBottles = NUMBER_BOTTLES_OF_BEER_ON_THE_WALL;
38 while (nBottles > 0) {
39 String verse = "";
40 if (nBottles > 1) verse += verseForNBottles(nBottles);
41 else verse += finalVerse();
42
43 System.out.println(verse);
44
45 nBottles--;
46 }
47
48 }
49
50
51 public static void main(String[] args) {
52 NinetyNineBottles nnb = new NinetyNineBottles();
53 nnb.printSong();
54 }
55
56 //private methods
57
58 /**
59 * Return verse for N bottles of beer (where N is > 1)
60 * @param nBottles the current number of bottles of beer on the wall.
61 * @return verse appropriate for the number of bottles of beer on the wall.
62 */
63 private String verseForNBottles(int nBottles)
64 {
65 if (nBottles <= 0) return "";
66
67 String verse = nBottles + " bottles of beer on the wall,\n" +
68 nBottles + " bottles of beer!\n" +
69 " Take one down,\n" +
70 " Pass it around,\n";
71 int bottlesLeft = nBottles - 1;
72 if (bottlesLeft > 1)
73 verse += bottlesLeft + " bottles of beer on the wall!\n\n";
74 else
75 verse += "1 bottle of beer on the wall!\n\n";
76
77 return verse;
78 }
79
80
81 /**
82 * Print final verse for one bottle of beer.
83 * @return the final verse (one bottle of beer on the wall).
84 */
85 private String finalVerse()
86 {
87 return "1 bottle of beer on the wall,\n" +
88 "1 bottle of beer!\n" +
89 " Take one down,\n" +
90 " Pass it around,\n" +
91 "No more bottles of beer on the wall!\n\n";
92 }
93
94 }
95

------------------- BEER 3 -------------------

Here's a more concise version:

class bottles
{
public static void main(String args[])
{
String s = "s";
for (int beers=99; beers>-1;)
{
System.out.print(beers + " bottle" + s + " of beer on the wall, ");
System.out.println(beers + " bottle" + s + " of beer, ");
if (beers==0)
{
System.out.print("Go to the store, buy some more, ");
System.out.println("99 bottles of beer on the wall.\n");
System.exit(0);
}
else
System.out.print("Take one down, pass it around, ");
s = (--beers == 1)?"":"s";
System.out.println(beers + " bottle" + s + " of beer on the wall.\n");
}
}
}

------------------- BEER 4 -------------------

Here's a relatively clean, minimalist version (my favorite!)

class B {
public static void main(String[]a) {
int n=99;
String o=" bottle";
String e=" of beer";
String w=" on the wall\n";
String f1= "No more bottles of beer on the wall, no more bottles of beer\n";
String f2= "Go to the store and buy some more, 99 bottles of beer on the wall\n";
String b,v;

while(n>0){
b=n+o+(n==1?"":'s')+e;
v = b+w+b+"\nTake one down, and pass it around\n";
System.out.println(v);
if (--n==0)System.out.println(f1+f2);
}
}
}

--------------------------------------

If you're still thirsty for more, I've just discovered a
website with literally thousands of implementations.

Check it out: http://www.99-bottles-of-beer.net/

No comments: