Language Eiffel
| Date: | 04/20/05 |
| Author: | Anonymous |
| URL: | n/a |
| Comments: | 4 |
| Info: | n/a |
| Score: |
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
| Version | Author | Date | Comments | Rate |
|---|---|---|---|---|
| Single class version with inline agents | Finnian Reilly | 12/06/09 | 1 |
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
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