<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>KirkHings.com &#187; head first java</title>
	<atom:link href="http://kirkhings.com/tag/head-first-java/feed/" rel="self" type="application/rss+xml" />
	<link>http://kirkhings.com</link>
	<description>The '-berger' part trips up so many folks that I sometimes simplify my name for you. Yes, just for you.</description>
	<lastBuildDate>Sun, 27 Feb 2011 23:53:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Bottles of Beer Song via Java</title>
		<link>http://kirkhings.com/code/java/bottles-of-beer-song-via-java/388/</link>
		<comments>http://kirkhings.com/code/java/bottles-of-beer-song-via-java/388/#comments</comments>
		<pubDate>Sun, 24 May 2009 19:42:03 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[head first java]]></category>
		<category><![CDATA[self-instructional challenge]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=388</guid>
		<description><![CDATA[photo credit: NUCO This programming challenge is from the Head First Java book, which I studied and practiced spring 2009. The end result itself is not terribly sexy, but I liked the algorithm construction process. My programming curriculum changed course during the first year, and they eliminated our Java classes. I was still curious about [...]


Related posts:<ul><li><a href='http://kirkhings.com/code/java/plain-and-recursive-factorials-with-full-print-enhancements/239/' rel='bookmark' title='Plain and Recursive Factorials with Full Print Enhancements'>Plain and Recursive Factorials with Full Print Enhancements</a> <small>photo credit: gadl This programming challenge is from a WiBit.net...</small></li>
</ul>]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/89046340@N00/107340991/" title="PA300108" target="_blank"><img src="http://farm1.static.flickr.com/51/107340991_b1bda6f04d.jpg" alt="PA300108" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by/2.0/" title="Attribution License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/89046340@N00/107340991/" title="NUCO" target="_blank">NUCO</a></small></div>
<p>This programming challenge is from the Head First Java book, which I studied and practiced spring 2009. The end result itself is not terribly sexy, but I liked the algorithm construction process. My programming curriculum changed course during the first year, and they eliminated our Java classes. I was still curious about Java, so I took it upon myself to self-learn Java. This was one of my first products.</p>
<p>I post these self-instructional challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it. Finally, self-instructional challenges are interesting and different than academic challenges because I sought, discovered, and performed them on my own volition without anyone else telling me to do so. I did it because I wanted to learn and practice.</p>
<blockquote><p>Challenge: Create a simple program which outputs the classic &#8220;99 Bottles of Beer&#8221; song, counting down from 99 to 0 bottles. In addition to outputting the correct number of bottles in each chorus, output the correct plural or singular form of &#8220;bottle&#8221; where appropriate to the number remaining. When there are no more bottles left then state final line.
</p></blockquote>
<p>My solution:</p>
<pre class="brush: java; title: ; notranslate">
package bottlesofbeer;

/**
 *
 * @author kirk
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // initialize starting number of bottles on wall
        int beerNum = 99;

        // initialize plural form of bottle used most often
        String word = &quot;bottles&quot;;

        while (beerNum &gt; 0) {
            // if only 1 bottle is left then change to singular form
            if (beerNum == 1) { word = &quot;bottle&quot;; }

            // output song chorus using current variable states
            System.out.println(beerNum + &quot; &quot; + word + &quot; of beer on the wall,&quot;);
            System.out.println(beerNum + &quot; &quot; + word + &quot; of beer!&quot;);
            System.out.println(&quot;Take one down, pass it around.&quot;);

            // decrement beerNum cuz we took one down and passed it around
            beerNum--;
            if (beerNum == 1) { word = &quot;bottle&quot;; }

            // so long as there are still bottles on the wall
            if (beerNum &gt; 0) {
                System.out.println(beerNum + &quot; &quot; + word + &quot; of beer on the wall!&quot;);
                System.out.println();
            }
            // when there are no more bottles on the wall
            else {
                System.out.println(&quot;No more bottles of beer on the wall!&quot;);
            } // ends if/else
        } // ends while loop
    } // ends main method
} // ends class
</pre>
<p>Here is the output:</p>
<div id="attachment_394" class="wp-caption alignleft" style="width: 310px"><img src="http://kirkhings.com/wp-content/uploads/beersong-java.png" alt="99 Bottles of Beer Song output via Java" title="beersong-java" width="300" height="454" class="size-full wp-image-394" /><p class="wp-caption-text">99 Bottles of Beer Song output via Java</p></div>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fkirkhings.com%2Fcode%2Fjava%2Fbottles-of-beer-song-via-java%2F388%2F&amp;title=Bottles%20of%20Beer%20Song%20via%20Java" id="wpa2a_2"><img src="http://kirkhings.com/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share"/></a></p>

<p>Related posts:<ul><li><a href='http://kirkhings.com/code/java/plain-and-recursive-factorials-with-full-print-enhancements/239/' rel='bookmark' title='Plain and Recursive Factorials with Full Print Enhancements'>Plain and Recursive Factorials with Full Print Enhancements</a> <small>photo credit: gadl This programming challenge is from a WiBit.net...</small></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/java/bottles-of-beer-song-via-java/388/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

