=============================================*/
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