Language Java
(exception oriented)
Date: | 09/08/05 |
Author: | Jarek Ratajski |
URL: | n/a |
Comments: | 5 |
Info: | http://java.sun.com |
Score: | (3.02 in 205 votes) |
/** * 99 Bottles in Java - Exception Oriented Programming :-) * This program abuses concept of exceptions. * notice: it does not use any loops (for, while) or if statements * * use java 1.4 or newer * @author Jarek Ratajski */ public class EOP99Bottles { /** * this main method is very simple - * only throws exception and prints stack trace * would You suspect it is 99 bottles program ? :-) */ public static void main(String[] args) { try { throw new BottleException(1, null); } catch (Exception e) { e.printStackTrace(); } } private static class BottleException extends Exception { private final int cnt; /** * notice beauty of this signature: object that may throw exception of * its own type during its creation !!! */ public BottleException(int i, BottleException c) throws BottleException { super(c); this.cnt = i; try { int a = 03 / (99-i); throw new BottleException(i+1, this); } catch (ArithmeticException e) { //deliberately } } public void printStackTrace() { System.out.println(cnt+" Bottle(s) of beer on the wall,"+cnt+"bottle(s) of beer"); System.out.println("Take one down and pass it around,"); System.out.println((cnt-1)+" bottle(s) of beer on the wall"); try { getCause().printStackTrace(); } catch (NullPointerException npe) { //deliberately } } } }
Download Source | Write Comment
Alternative Versions
Version | Author | Date | Comments | Rate |
---|---|---|---|---|
object-oriented version | Anonymous | 04/20/05 | 33 | |
standard version | Sean Russell | 04/20/05 | 12 | |
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
Robert Hayes said on 10/20/05 03:40:08
Stick it in whatever class you want:
public static final String BEER = " BOTTLES OF BEER";
public static final String BEER_WALL = BEER + " IN THE WALL";
public static void bottleCounter() {
for (int i = 99; i > 0 {
System.out.println(
i + BEER_WALL + ", " + i + BEER + "."
+ "TAKE ONE DOWN AND PASS IT ARROUND " + --i +
BEER_WALL);
}
}
John T. said on 04/17/09 12:58:00
Lovely...the exception-oriented code, not the pointless code in the comment.
Very nice take on recursion - maybe Exception-oriented programming will be the next big thing - certainly seems to have more potential than aspect-oriented programming.
John, the Fisherman said on 05/28/09 21:54:27
Nice! 8|D>
barrym said on 06/07/10 23:59:25
Jarek, I like your style and creativity!
Robert Hayes, I think you're a complete and total DOOFUS!!
barrym said on 06/08/10 06:39:29
Can Jarek or someone else smarter than I am show a way to handle the singular
case and the final sentence while retaining the cool exception-oriented flavor?