Results 1 to 6 of 6
  1. #1
    Palindrome5 is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default Help improving "bottles of beer on the wall" code

    I'm wondering if there's a way I could make my code more efficient without using arrays. Also tell me if I'm doing anything bad in terms of spacing, conventions, indenting, etc. The code itself works as intended, I just don't want to get into bad habits when working with classes.

    I'm mostly concerned about the for loop, which is pretty messy. I have to make an extra loop for the teens case (nineteen, eighteen, seventeen), then another one for the single digits, then another 2 statements for one and zero. However, to contain all that in a single for loop would probably require a ton of if-else statements.

    Java Code:
    public class BeerSong
    {
    
    	private int songs;
    		
    	public BeerSong(int songs)
    	{
    		if(songs<0)
    		{
    			this.songs = 0;
    		}
    		else if(songs>99)
    		{
    			this.songs = 99;
    		}
    		else
    		{
    		this.songs = songs;
    		}
    	}
    
    	private String FirstDigitDisplay(int n)
    	{
    		switch(n)
    		{
    		case 0:
    			return "";
    		case 1:
    			return "Ten";
    		case 2:
    			return "Twenty";
    		case 3: 
    			return "Thirty";
    		case 4:
    			return "Fourty";
    		case 5:
    			return "Fifty";
    		case 6:
    			return "Sixty";
    		case 7:
    			return "Seventy";
    		case 8:
    			return "Eighty";
    		case 9:
    			return "Ninety";
    		default:
    			System.out.println("Fatal error");
    			System.exit(0);
    			return "Fatal error";
    		}
    	}
    	
    	private String SecondDigitDisplay(int n)
    	{
    		switch(n)
    		{
    		case 0:
    			return "";
    		case 1:
    			return "one";
    		case 2:
    			return "two";
    		case 3: 
    			return "three";
    		case 4:
    			return "four";
    		case 5:
    			return "five";
    		case 6:
    			return "six";
    		case 7:
    			return "seven";
    		case 8:
    			return "eight";
    		case 9:
    			return "nine";
    		default:
    			System.out.println("Fatal error");
    			System.exit(0);
    			return "Fatal error";
    		}
    	}
    	
    	private String TeenDisplay(int n)
    	{
    		switch(n)
    		{
    		case 0:
    			return "Ten";
    		case 1:
    			return "Eleven";
    		case 2:
    			return "Twelve";
    		case 3: 
    			return "Thirteen";
    		case 4:
    			return "Fourteen";
    		case 5:
    			return "Fifteen";
    		case 6:
    			return "Sixteen";
    		case 7:
    			return "Seventeen";
    		case 8:
    			return "Eighteen";
    		case 9:
    			return "Nineteen";
    		default:
    			System.out.println("Fatal error");
    			System.exit(0);
    			return "Fatal error";
    		}
    	}
    	
    	private String LastDigitDisplay(int n)
    	{
    		String junk = SecondDigitDisplay(n);
    		String char1 = junk.substring(0,1);
    		String char2 = junk.substring(1);
    		return char1.toUpperCase() + char2;
    	}
    	
    	private String displayDigits(int a, int b)
    	{
    		return FirstDigitDisplay(a) + "-" + SecondDigitDisplay(b);
    	}
    	
    	private void Printer(String name)
    	{
    		System.out.println(name + " bottles of beer on the wall,");
    		System.out.println(name + " bottles of beer,");
    		System.out.println("Take one down, pass it around,");
    		System.out.println(name + " bottles of beer on the wall.");
    		System.out.println();
    	}
    
    	public void printSong()
    	{
    		
    		for(int i=songs/10; i>= 2; --i)
    		{
    			for(int j=9; j>=0; --j)
    			{
    				if(j==0)
    				{
    					Printer(FirstDigitDisplay(i));
    				}
    				else
    				{
    					Printer(displayDigits(i,j));
    				}
    				
    			}
    			
    		}
    	
    		for(int k=9; k>=0; --k)
    		{
    			Printer(TeenDisplay(k));
    		}
    		
    		for(int k=9; k>=2; --k)
    		{
    			Printer(LastDigitDisplay(k));
    		}
    	
    		System.out.println("One bottle of beer on the wall,");
    		System.out.println("One bottle of beer,");
    		System.out.println("Take one down, pass it around,");
    		System.out.println("One bottle of beer on the wall.");
    		System.out.println();
    		
    		Printer("Zero");
    		
    	}
    
    	public static void main(String[] args)
    	{
    		
    		BeerSong bs = new BeerSong(150);
    		bs.printSong();
    
    	}
    	
    }

  2. #2
    DrummondAW is offline Member
    Join Date
    Oct 2012
    Posts
    13
    Rep Power
    0

    Default Re: Help improving "bottles of beer on the wall" code

    Everything looks pretty broken up - Wouldn't it be better to have a single method that converts the number into words for you? It might be better to convert the songs integer into a string and parse it easily.

  3. #3
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: Help improving "bottles of beer on the wall" code

    Java Code:
    private void Printer(String name)
        {
            System.out.println(name + " bottles of beer on the wall,");
            System.out.println(name + " bottles of beer,");
            System.out.println("Take one down, pass it around,");
            System.out.println(name + " bottles of beer on the wall.");
            System.out.println();
        }
    it doesnt work. It will print out the name, take a bottle down, then print out the same name. That isn't how the song goes.

  4. #4
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Help improving "bottles of beer on the wall" code

    Quote Originally Posted by Reeling View Post
    It will print out the name, take a bottle down, then print out the same name. That isn't how the song goes.
    Maybe not for bottles 99-85... but it sure sound like it when you've had fifty or sixty beers.
    Last edited by SJF; 10-30-2012 at 07:59 PM. Reason: grammar

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Help improving "bottles of beer on the wall" code

    So, just an FYI - premature optimization is the beginning of the end of many projects. If you optimize without there actually being a problem (or requirement), you're asking for trouble. Also, the compiler is pretty smart and will many optimizations behind the scenes.

  6. #6
    sibernewf is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default Re: Help improving "bottles of beer on the wall" code

    lol. Not that its much help, but I recall writing a java app that did exactly this, but I used recursion to count down.
    Unfortunately, I have no idea where I have the code anymore. But I recall it was fun doing it recursively.
    something like below maybe. I havent factored into converting numbers to strings tho..

    Its a bit messy, but I think it works.

    Java Code:
    public class Beer
    {
      public static void main(String[] args)
      {
        Beer b = new Beer();
        b.printSong(100);
      }
    
      public void printSong(int bottles)
      {
        if(bottles >= 2)
        {
          System.out.println(bottles + " bottles of beer on the wall,");
          System.out.println(bottles + " more bottles of beer.");
          System.out.println("You take one down and pass it around,");
          if((bottles-1) == 0)
          {
            System.out.println("No more bottles of beer on the wall.");
          }
          else
          {
            System.out.println((bottles -1) + " more bottle of beer on the wall.");
          }
        }
        else if(bottles == 1)
        {
          System.out.println(bottles + " bottle of beer on the wall,");
          System.out.println(bottles + " more bottle of beer.");
          System.out.println("You take one down and pass it around,");
        }
        else if(bottles < 1)
        {
          System.out.println("No more bottles of beer on the wall.");
        }
        System.out.println();
        if(bottles <1)
        {
          System.exit(0);
        }
        printSong(bottles -1);
      }
    }
    Last edited by sibernewf; 11-02-2012 at 04:33 AM.

Similar Threads

  1. Replies: 5
    Last Post: 04-29-2012, 07:18 AM
  2. Program skips "If" code and goes straight to "Else"
    By Logik22 in forum New To Java
    Replies: 12
    Last Post: 01-21-2012, 05:40 PM
  3. 'Bottles of Beer' project
    By s4rd59 in forum New To Java
    Replies: 2
    Last Post: 02-14-2010, 03:27 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

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