Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

1. Dive in A Quick Dip: Breaking the Sur... > Coding a Serious Business Applicatio...

Coding a Serious Business Application

Let’s put all your new Java skills to good use with something practical. We need a class with a main(), an int and a String variable, a while loop, and an if test. A little more polish, and you’ll be building that business back-end in no time. But before you look at the code on this page, think for a moment about how you would code that classic children’s favorite, “99 bottles of beer.”

image with no caption
public class BeerSong {
   public static void main (String[] args) {
     int beerNum = 99;
     String word = "bottles";

   while (beerNum > 0) {

      if (beerNum == 1) {
        word = "bottle"; // singular, as in ONE bottle.
      }

      System.out.println(beerNum + " " + word + " of beer on the wall");
      System.out.println(beerNum + " " + word + " of beer.");
      System.out.println("Take one down.");
      System.out.println("Pass it around.");
      beerNum = beerNum - 1;

      if (beerNum > 0) {
         System.out.println(beerNum + " " + word + " of beer on the wall");
      } else {
         System.out.println("No more bottles of beer on the wall");
     } // end else
   } // end while loop
 } // end main method
} // en....

  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial