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 Eiffel

Date:04/20/05
Author:Anonymous
URL:n/a
Comments:4
Info:n/a
Score: (3.01 in 102 votes)
class SHELF

      -- A shelf of bottles

creation

   make

feature

   make (l_bottles: INTEGER) is
      require
         positive_bottles: l_bottles >= 0
      do
         bottles := l_bottles
      end

   remove is
      require
         bottles_exist: bottles > 0
      do
         bottles := bottles - 1
      ensure
         removed: bottles = old bottles - 1
      end

   bottles: INTEGER

   short_description: STRING is
      do
         if bottles = 0 then
            Result := "No"
         else
            Result := bottles.out
         end
         Result.append (" bottle")
         if bottles /= 1 then
            Result.append ("s")
         end
         Result.append (" of beer")
      ensure
         result_exists: Result /= Void
      end

   description: STRING is
      do
         Result := short_description
         Result.append (" on the wall, ")
         Result.append (short_description)
         Result.append ("%N")
      ensure
         result_exists: Result /= Void
      end

   empty: BOOLEAN is
      do
         Result := bottles = 0
      end

invariant

   positive_bottles: bottles >= 0
         
end -- class SHELF

class BEER

   -- Produuce the ditty
   -- Nick Leaton

creation

   make

feature

   shelf: SHELF

   make is
      do
         from
            !!shelf.make (99)
         until
            shelf.empty
         loop
            io.put_string (shelf.description)
            shelf.remove
            io.put_string ("Take one down, pass it all around%N%N")
         end
         io.put_string (shelf.description)
         io.put_string ("Go to the store and buy some more%N%N")
         shelf.make (99)
         io.put_string (shelf.description)
      end   
            
end -- class BEER

 
  	Programming language: Eiffel
  	

class BEERS

creation

    make

feature     -- Creation

    make is
        local
            i : INTEGER
            b : STRING;
        do
            from i := 99 variant i until i <= 0 loop
                if i = 1 then
                    b := " bottle";
                else
                    b := " bottles"
                end -- if
                io.put_integer(i);
                io.put_string(b);
                io.put_string(" of beer on the wall, ");
                io.put_integer(i);
                io.put_string(b);
                io.put_string(" of beer,");
                io.put_new_line;
                io.put_string("Take one down and pass it around, ");
                i := i - 1;
                io.put_integer(i);
                io.put_string(b);
                io.put_string(" bottles of beer on the wall.");
                io.put_new_line;
            end -- loop
            io.put_string("Go to the store and buy some more,");
            io.put_new_line;
            io.put_string("99 bottles of beer on the wall.");
            io.put_new_line;
        end;

end -- class BEERS

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
Single class version with inline agentsFinnian Reilly12/06/091

Comments

>>  A Visitor said on 01/04/06 09:01:43

A Visitor The author of this language is Bertrand Meyer and Interactive Software Engineering Inc

>>  Ivo said on 06/17/09 19:26:59

Ivo nice one

>>  Batibix said on 11/17/09 02:00:41

Batibix Only one version of 99 bottles in Eiffel? Strange, I'd thought that more people used, since Bertrand Meyer is held in high regard among OO language buffs.

>>  Finnian Reilly said on 12/06/09 03:05:06

Finnian Reilly I have submitted 2 versions, a long one and a short one. Waiting for approval.

The long one demonstrates: the latest ECMA-367 syntax, inline agents, convertors, agent iteration, inheritance, generic lists, anchored types, tuple to object conversion, loop invariants, list structure use, visitor pattern, extended inheritance tree. Total of 10 classes.
Unfortunately I couldn't think of way to use multiple inheritance.

The short one is terse, just one class with 4 routines.

You can download the source code here:
http://www.eiffel-loop.com/99-bottles-of-beer.net/Eiffel-99-bottles.tar.gz
http://www.eiffel-loop.com/99-bottles-of-beer.net/Eiffel-99-bottles.short.tar.gz

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: