Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language Java

(Java 5.0 object-oriented version)

Date:11/19/05
Author:Kvols
URL:n/a
Comments:3
Info:n/a
Score: (3.00 in 38 votes)
/**
 * Java 5.0 version of the famous "99 bottles of beer on the wall".
 * Note the use of specific Java 5.0 features and the strictly correct output.
 *
 * @author kvols
 */

import java.util.*;

class Verse {
    private final int count;

    Verse(int verse) {
        count= 100-verse; 
    }

    public String toString() {
        String c=
            "{0,choice,0#no more bottles|1#1 bottle|1<{0} bottles} of beer";
        return java.text.MessageFormat.format(
            c.replace("n","N")+" on the wall, "+c+".\n"+
            "{0,choice,0#Go to the store and buy some more"+
            "|0<Take one down and pass it around}, "+c.replace("{0","{1")+
            " on the wall.\n", count, (count+99)%100);
    }
}

class Song implements Iterator<Verse> {
    private int verse=1;

    public boolean hasNext() {
        return verse <= 100;
    }

    public Verse next() {
        if(!hasNext()) 
            throw new NoSuchElementException("End of song!");
        return new Verse(verse++);
    }

    public void remove() {
        throw new UnsupportedOperationException("Cannot remove verses!");
    }
}

public class Beer {
    public static void main(String[] args ) {

        Iterable<Verse> song= new Iterable<Verse>() {
            public Iterator<Verse> iterator() {
                return new Song();
            }
        };

        // All this work to utilize this feature:
        // "For each verse in the song..."

        for(Verse verse : song) {
            System.out.println(verse);
        }
    }
}

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
bytecode-version with loaderTilo Dickopp05/23/0610
exception orientedJarek Ratajski09/08/055
object-oriented versionAnonymous04/20/0533
Singing with Java Speech APIKevin Seifert05/04/062
standard versionSean Russell04/20/0512

Comments

>>  Charles said on 04/12/06 12:30:55

Charles I think this program is quite good because of its simplicity and clarity.But i am not quite familiar with the way the author dealt with the string.Maybe i am a little stupid, will you take some minutes to explain it to me.Thank you.

>>  kvols said on 12/04/06 15:47:08

kvols Sure - and thanks for the comment! ;-)

The main idea is to make jave.text.MessageFormat format the string for the verse. MessageFormat is a quite over-looked class in Java but it's a very convinient way to format numbers, dates and strings, taking plurals into account, select between several texts depending on a number etc. The c vaiable in simply used to insert either 'no more bottles of beer', '1 bottle of beer' or 'x bottles of beer'.

The c.replace(...) is used because the verse refers to both the current and the coming number of beers, and I therefore refers to argument 0 or argument 1.

I hope this cleared up your questions. Otherwise please write to my first name dot last name at gmail dot com. ;-)

Cheers,

Povl

>>  Ian said on 08/20/08 22:14:23

Ian On the one hand, MessageFormat is the right tool for the job.

On the other hand, a custom Iterator giving you a new object per verse seems like overkill. And the Iterable constructor should of course take the bottle count.

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!

Name:

eMail:

URL:

Security Code:
  
Comment: