Language Java
(object-oriented version)
Date: | 04/20/05 |
Author: | Anonymous |
URL: | n/a |
Comments: | 33 |
Info: | n/a |
Score: | ![]() |
/* the main method instantiates drinkers and directs them to a newly-instantiated wall of beer. */ public class Sing99Bob { public static void main(String[] args) { ((NeedToFindBeer)(new Drinkers())).pointAtBeer(new WallOfBeer()); } } interface NeedToFindBeer { void pointAtBeer(WallOfBeer wob); } /* an instance of Drinkers, after being pointed at a wall containing beer, will periodically take one down. The singing is driven by the taking of the beer. */ class Drinkers extends Thread implements NeedToFindBeer { static final int drinkRate = 2; WallOfBeer ourBeer; public void run() { while (ourBeer.takeOne()>0) { try { Thread.sleep(drinkRate*1000); } catch (InterruptedException ignore) {} } } public void pointAtBeer(WallOfBeer wob) { ourBeer = wob; this.start(); } } interface Countable { int howMany(); } /* an instance of a WallOfBeer will maintain a count of the beer bottles. The wall has an associated Narrator who reports each time the number of bottles is changed. */ class WallOfBeer implements Countable { static final int full = 99; int count = 0; WallWatcher ww; { ww = new Narrator(); putSome(full); } void putSome(int some) { count += some; if (ww!=null) ww.wallEvent(this); } int takeOne() { count--; if (ww!=null) ww.wallEvent(this); return count; } public int howMany() { return count; } } interface WallWatcher { void wallEvent(Countable wob); } /* an instance of Narrator sings the verse each time it is notified that something has happened to the wall. This narrator attempts to be grammatical, and uses words for the bottle count (singers sing words not numerals). The singing is paced, not merely lump-dumped to sysout. */ class Narrator implements WallWatcher { static final int singspeed = 500; public void wallEvent(Countable wob) { int b = wob.howMany(); try { System.out.print(bob(b, true)+onwall+", "); Thread.sleep(singspeed); System.out.print(bob(b, false)+".\n"); Thread.sleep(singspeed); System.out.print((b>0)?takedown:gostore); Thread.sleep(singspeed); System.out.print((b>0)?bob(b-1, false):bob(WallOfBeer.full, false)); System.out.print(onwall+".\n"); } catch (InterruptedException ignore) {} } static final String bob(int i, boolean c) { String word = word(i); if (c) word = (word.substring(0, 1).toUpperCase())+word.substring(1); return word+" bottle"+((i==1)?"":"s")+" of beer"; } static final String word(int i) { if (i==0) return "no more"; if (i<20) return units[i]; return tens[i/10]+((units[i%10]=="")?"":"-"+units[i%10]); } static final String onwall = " on the wall", takedown = "Take one down and pass it around, ", gostore = "Go to the store, get some more, "; static final String[] tens = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}, units = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; }
Download Source | Write Comment
Alternative Versions
Version | Author | Date | Comments | Rate |
---|---|---|---|---|
standard version | Sean Russell | 04/20/05 | 12 | ![]() ![]() |
exception oriented | Jarek Ratajski | 09/08/05 | 5 | ![]() ![]() |
bytecode-version with loader | Tilo Dickopp | 05/23/06 | 10 | ![]() ![]() |
Java 5.0 object-oriented version | Kvols | 11/19/05 | 3 | ![]() ![]() |
Singing with Java Speech API | Kevin Seifert | 05/04/06 | 2 | ![]() ![]() |
Download Source | Write Comment
Add Comment
Please provide a value for the fields Name,
Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.
Please don't post large portions of code here! Use the form to submit new examples or updates instead!
Comments
Short and simple, true Java-way
That being said, yes, you can write for loops in Java as well. But this program shows off how easy it is to use threads and the power of programming to interface in Java. For example, want a different language? Or even a different song? Implement the WallWatcher interface for each version of the song you want, and this program can handle all of them, either singing separately, or even at the same time (if this were a bar with multiple singers in a video game or something).
Nice job, very well-designed and robust!
public class Bottles {
static final String p1 = "{0, choice, -1#99|0#no more|0<{0}} bottle{0, choice, 0#s|1#|1<s} of beer";
static final String p2 = " on the wall";
static final String p3 = "{0, choice, -1#Go to the store and buy some more|0#Take one down and pass it around}, ";
static final String patterns[] = { p1 + p2 + ", " + p1 + ".", p3 + p1 + p2 + "." } ;
public static void main(String[] args ) {
for (int i = 99; i >= 0; i-- ) {
System.out.println(java.text.MessageFormat.format(patterns[0], i ).replaceFirst("^n", "N" ) ) ;
System.out.println(java.text.MessageFormat.format(patterns[1], i - 1 ) );
}
}
}
If can be written in five lines.
It is interesting though how people can find the most complicated solutions to the simplest of problems.
Cheers
public static void main(String[] args) {
beer();
}
public static void beer() {
for (int i=99; i>=3; i--){
System.out.println(i+" bottles of beer on the wall, " +i+ " bottles of beer."
System.out.println("Take one down and pass it around, "+(i-1)+ " bottles of beer on the wall."
System.out.println();
}
System.out.println("2 bottles of beer on the wall, 2 bottles of beer."
System.out.println("Take one down and pass it around, 1 bottle of beer on the wall."
System.out.println();
System.out.println("1 bottle of beer on the wall, 1 bottle of beer."
System.out.println("Take one down and pass it around, no more bottles of beer on the wall."
System.out.println();
System.out.println("No more bottles of beer on the wall, no more bottles of beer. "
System.out.println("Go to the store and buy some more, 99 bottles of beer on the wall."
}
}
how about this?
Very nice...
public class Beer {
public static void main(String[] args) {
for (int i = 90; i > 0; i--)
if (i != 1)
System.out.println(i+ " bottles of beer on the wall, " +i+ " bottles of beer. Take one down, pass it around, " +(i-1)+ " bottles of beer on the wall."
System.out.println("1 bottle of beer on the wall, 1 bottle of beer. Take it down, pass it around, no bottles of beer on the wall.
}
}
for (i=99; i>0; i--) context.Response.Write(String.Format("{0} bottles of beer on the wall, {0} bottles of beer;<br />Take one down, pass it around there'll be {1} bottles of beer on the wall<br />", i, i-1));
It takes a real (ly mad?) programmer to come up with something like this!
Personally, I love this - it's a proper enterprisey solution! Plus, the components are reusable and replacable. It's.... astonishing! I think it's possibly the most verbose and complex solution to a simple problem I've ever liked!
Props!
This is starting to sound just like people who post in youtube...
Of course it could be made a lot simplier, but people should really get used to application like this one, for the sack of programming future.
Congractullations
{
public static void main(String[] args)
{
int bottles = 99;
while(bottles > 0)
{
System.out.print(bottles + " bottles of beer on the wall, "
+ bottles + " bottles of beer,\nTake one down and pass"
+ " it around, "
bottles --;
System.out.println(bottles + " bottles of beer on the wall."
System.out.println();
}
}
}
//another way it can be done
It is *not* about size.
I love this version, looks like a sketch for a movie. 8|D>
public class NinetyNineBottlesOfBeerOnAWall {
public static void main(String[] a) {
String e = "", b = " bottle", s = "s", o = " of beer", w = " on the wall", c = ", ",
p = ".", l = "\n", t = "Take one down and pass it around, ", n = "n", m = "o more",
g = "Go to the store and buy some more, ", mbs = m+b+s, opl=o+p+l, owpl=o+w+p+l, owc=o+w+c;
int nn = 99;
for(int i=nn;i>0;i--) {
System.out.print( i + b + (i >1 ? s : e) + owc + i + b + (i >1 ? s : e) + opl +
t + (i-1 > 0 ? i-1 + b + (i-1 >1 ? s : e) : n + mbs) + owpl + l);
}
System.out.print(n.toUpperCase() + mbs + owc + n + mbs + opl + g + nn + b + s + owpl);
}
}
http://99-bottles-of-beer.net/language-python-1154.html
It made me think of that article.
That is OOP. And OOP is the same in C++, java, .net, etc.
Nice code.
Also you shouldn't compare strings with '==', unless if you're interested in whether they happen to be in the same place in memory.
You should also consider using the keyword 'private'.
This is really bad java (besides being very verbose
2. But it is so typical of the problem that programmers start with a simple task and over-engineer and over-feature the solution.
3. Compare the BASIC version - go past the BASIC and click on version "2", to see a solution that solves the problem and stops there.
4. While I admire this, a student learning to program would be deterred by it.
5. If we want people to yearn to program, let's stay simple.
problem is nearly always the best. However, you cite as an example a program that
is admirably simple but stops before the problem of printing the end of the song is
solved. I love to browse the different versions and different languages to see the
beauty of diversity. I don't frown at the complex solutions because they have their
own appeal, but I do frown on "solutions" that are incorrect or incomplete, because
they serve as poor examples to beginners, hobbyists, and professionals alike.