CookieJar V 1.0.0
 
This is an example program that demonstates some of the topics covered in week 3 and prior weeks. See comments for the details of what it demonstrates.
 Sample output:
 
Source code:
 
/*=============================================
    Purpose:    Balance reduction of cookies
                in a cookie jar using
                random numbers.
 
                Demonstrates:
                    1.  while flow structure
                    2.  if, else flow structure
                    3.  accumulators
                    4.  Math.max
                    5.  Math.min
                    6.  Math.random for random numbers
                    7.  System.println
                    8.  Expression based if-else
                    9.  \n and \t escape characters
                   10.  ++ and -= operators
                   11.  Nesting of flow structures
                   12.  Logic expressions
                   13.  Type casting (int)
 
    Version history:
 
    Number:         1.0.0
    Name:           CookieJar
    Date:           4/8/2003
    Programmers:    Lon Hosford
    Changes:        Original programming
 

=============================================*/
 

public class CookieJar
{
    /*---------------------------------------------
        Program starts here. No arguments are expected.
    ---------------------------------------------*/
    public static void main(String [] args)
    {
        /*---------------------------------------------
        |   Declare variables private                 |
        |   to the main method                        |
        ---------------------------------------------*/
 
        /*---------------------------------------------
            Initial total cookies in cookie jar
        ---------------------------------------------*/
        int     totalCookies = 25;
 
        /*---------------------------------------------
            Total cookies reamining in cookie jar
        ---------------------------------------------*/
        int     totalCookiesRemaining = totalCookies;
 
        /*---------------------------------------------
            Counter of helpings of cookies
        ---------------------------------------------*/
        int     cookieHelpings = 0;
 
        /*---------------------------------------------
            Largest number of cookies taken in a
            helping
        ---------------------------------------------*/
        int     mostCookiesPerHelping = 0;
 
        /*---------------------------------------------
            Least number of cookies taken in a helping
        ---------------------------------------------*/
        int     leastCookiesPerHelping = 0;
 
        /*---------------------------------------------
            Display report detail report header
        ---------------------------------------------*/
        System.out.println(" CookieJar V 1.0.0\n");
        System.out.println("            -----Cookies ----");
        System.out.println("  Helping #   Taken   Remaining");
        System.out.println("  ========= ========  =========");
        System.out.println("                        " + totalCookies);
 

        /*---------------------------------------------
            While there are cookies in the jar
        ---------------------------------------------*/
        while ( totalCookiesRemaining > 0 )
        {
            /*---------------------------------------------
                Increment the count of helpings
                from the cookie jar
            ---------------------------------------------*/
            cookieHelpings ++;
 
            /*---------------------------------------------
                Cookies eaten as
                smaller of cookies in jar or random
                number from 1 to 10
            ---------------------------------------------*/
            int cookiesTaken  =  (int) Math.min(totalCookiesRemaining, (Math.random() * 10 + 1)) ;
 
            /*---------------------------------------------
                Update the most cookies taken
                as larger of cookies taken or current
                record
            ---------------------------------------------*/
            mostCookiesPerHelping = Math.max(mostCookiesPerHelping, cookiesTaken) ;
 
            /*---------------------------------------------
                First helping
            ---------------------------------------------*/
            if (cookieHelpings == 1)
            {
                /*---------------------------------------------
                    Update the least cookies taken
                    as cookies taken
                ---------------------------------------------*/
                leastCookiesPerHelping = cookiesTaken;
            }
 
            /*---------------------------------------------
                Not first helping
            ---------------------------------------------*/
            else
            {
                /*---------------------------------------------
                    Update the least cookies taken
                    as smaller of cookies taken or current
                    record
                ---------------------------------------------*/
                leastCookiesPerHelping = Math.min(leastCookiesPerHelping, cookiesTaken) ;
            }
 

            /*---------------------------------------------
                Reduce total cookies remaining in the jar
            ---------------------------------------------*/
            totalCookiesRemaining -= cookiesTaken;
 
            /*---------------------------------------------
                Display the results
            -----------------------------------------*/
            System.out.println(
                            "\t" + cookieHelpings
                        +   "\t" + cookiesTaken
                        /*----------------------------------------
                            Expression based if-else:
                                Logic: totalCookiesRemaining > 0
                                True: "" + totalCookiesRemaining
                                False: "-"
                        ----------------------------------------*/
                        +   "\t" + (    (totalCookiesRemaining > 0)
                                        ? "" + totalCookiesRemaining
                                        : "-" )
                               );
 
        } // while ( totalCookiesRemaining > 0 )
 
        /*---------------------------------------------
            Display the summary statistics
        ---------------------------------------------*/
        System.out.println( "\nAverage cookies per helping: " + (totalCookies / cookieHelpings ) );
        System.out.println(   "Most cookies per helping:    " + mostCookiesPerHelping );
        System.out.println(   "Least cookies per helping:   " + leastCookiesPerHelping );
 

    } // End of main method
 

}