Monday, January 12, 2015

Head First Java Chapter 1


I learned that a source file holds one class definition and that it represents a piece of the program. A class has one or more methods and the methods must be declared inside a class. Method is a set of statements, their like functions or procedures. Everything goes in a class. Running a program means it tells Java Virtual Machine to load a class and start executing its main ( ) method. Also java has three standard looping constucts, while, do-while, and for.

public class BeerSong {
    public static void main(String[] args) {
        int beerNum = 99;
        String word = "bottles";
        while (beerNum > 0)
        {
            if (beerNum == 1)
            {
                word = "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");
            }
        }
    }
}



This is the beer song it works by launching the main class and it has 99 bottles of beer on the wall. The code will count all the way to 0. Wheb it gets to 1 bottle on the wall it changes bottles to bottle so it is grammercally correct. Then the code is correct and there is no more issues. 

No comments:

Post a Comment