Results 1 to 9 of 9
  1. #1
    darkbandit is offline Member
    Join Date
    Sep 2012
    Location
    Sherburne, NY
    Posts
    6
    Rep Power
    0

    Default recursive 99 bottles program is there better way to write it as a beginner?

    okay so just being a hmmm... what is the word beneath novice, anyways for a book exercise i had to right a program for the song 99 bottles of beer, this was involving recursive methods. it hinted around to more than one method. but my code only had one method and got what it wanted.
    so my question is simple with out going above beginner level what is better way to write it as a recursive method the comment was for me to think about it i couldnt come up with anything different.
    This is not homework i am 30 years old and learning java from a book and the web, this is my first programming language i decided to learn
    here is my code:

    Java Code:
    public class nineninebottlesofbeer {
    	//book said maybe more methods, smaller problems broken down to different methods
    	public static void main(String[] args) {
    		//99 bottles of beer song
    	    beer(10);
    	}
    	public static void beer( int x){
    		if(x>0){
    			System.out.print(x +" bottles of beer on the wall, ");
    			System.out.println(x +" bottles of beer, ");
    			System.out.println("ya'take one down and ya' pass it around");
    			System.out.println((x-1)+" bottles of beer on the wall");
    			beer(x-1);
    		}else{
    			System.out.println("No bottles of beer on the wall, no bottlesof beer ");
    			System.out.println("ya'can't take one down, ya can't pass it around,");
    		    System.out.println("'cause there is no more bottles of beer on the wall");
    		}
    	}
    }

  2. #2
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    85
    Rep Power
    0

    Default Re: recursive 99 bottles program is there better way to write it as a beginner?

    I too am an Apprentice :) A little older but also trying to learn Java self-taught. Just to share, I thought I would try out your idea of a recursive bottle of beer program.
    Java Code:
    public class BeerSong{
    
      public static void main(String[] args){
        
        int beer = 99;
        
        while(beer>0){
                      System.out.println(beer + " bottles of beer on the wall,");
                      System.out.println(beer + " bottles of beer,");
                      System.out.println("ya' take one down, pass it around");
                      beer--;
                      System.out.println(beer + " bottles of beer on the wall");
                      System.out.println();
                                          
                             }
        if(beer==0){
          System.out.println("No more bottles of beer you lush,");
          System.out.println("No more bottles of beer");
          System.out.println("You drank them all, and now you fall");
          System.out.println("Cause you drank all your bottles of beer on the wall");
      }
    }
    }
    Not sure if it was how you were thinking.

  3. #3
    darkbandit is offline Member
    Join Date
    Sep 2012
    Location
    Sherburne, NY
    Posts
    6
    Rep Power
    0

    Default Re: recursive 99 bottles program is there better way to write it as a beginner?

    that is kind what i was thinking but, the book i am reading hasn't covered while so i think i am going to have to look that up. hmm.... if you dont mind me asking what books/media are you using to teach yourself

  4. #4
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    85
    Rep Power
    0

    Default Re: recursive 99 bottles program is there better way to write it as a beginner?


  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: recursive 99 bottles program is there better way to write it as a beginner?

    @darkbandit, your code looks OK to me. Although classes should begin with an upper case letter NineNineBottlesOfBeer (this affects the name of the .java file as well).

    If you think the book was hinting at something, post what it said as that might shed light on things. For what it's worth your code does use more than one method: main() and beer().

    rru96's code is a good example of how you would do this without recursion. The "if(beer==0)" is not really needed because the while loop won't finish until the beer is.

  6. #6
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    85
    Rep Power
    0

    Default Re: recursive 99 bottles program is there better way to write it as a beginner?

    <--- Still not familiar with terminology and all that jazz. Thank you pbrock for pointing out the if(beer==0) redundancy. It makes a lot of sense that once the while loop is done, it continues on with the code, i.e. No more bottles.

  7. #7
    darkbandit is offline Member
    Join Date
    Sep 2012
    Location
    Sherburne, NY
    Posts
    6
    Rep Power
    0

    Default Re: recursive 99 bottles program is there better way to write it as a beginner?

    sorry its taken a little bit but here is what the book said:

    "Exercise 4.3. The rst verse of the song \99 Bottles of Beer" is:
    99 bottles of beer on the wall, 99 bottles of beer, ya' take one
    down, ya' pass it around, 98 bottles of beer on the wall.
    Subsequent verses are identical except that the number of bottles gets smaller
    by one in each verse, until the last verse:
    No bottles of beer on the wall, no bottles of beer, ya' can't take
    one down, ya' can't pass it around, 'cause there are no more
    bottles of beer on the wall!
    And then the song( nally) ends.
    Write a program that prints the entire lyrics of \99 Bottles of Beer." Your
    program should include a recursive method that does the hard part, but you
    might want to write additional methods to separate the major functions of
    the program.
    As you develop your code, test it with a small number of verses, like \3
    Bottles of Beer."
    The purpose of this exercise is to take a problem and break it into smaller
    problems, and to solve the smaller problems by writing simple methods."

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: recursive 99 bottles program is there better way to write it as a beginner?

    Well I can't see anything obviously wrong with your code.

  9. #9
    darkbandit is offline Member
    Join Date
    Sep 2012
    Location
    Sherburne, NY
    Posts
    6
    Rep Power
    0

    Default Re: recursive 99 bottles program is there better way to write it as a beginner?

    hmm... cool thanks. guess i did it right awesome yeah

Similar Threads

  1. Replies: 7
    Last Post: 11-30-2011, 06:58 PM
  2. 10 green bottles
    By Dave013 in forum New To Java
    Replies: 2
    Last Post: 10-25-2011, 06:41 PM
  3. Replies: 8
    Last Post: 06-06-2011, 02:27 AM
  4. 'Bottles of Beer' project
    By s4rd59 in forum New To Java
    Replies: 2
    Last Post: 02-14-2010, 03:27 PM
  5. problem with recursive binary search program
    By imran_khan in forum New To Java
    Replies: 3
    Last Post: 08-02-2007, 03:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •