So this is one of programs in head first java...i want to know HOW the answer became 25 1...in detail if anybody could help me out, im trying to understand it the best i can, i only see code here though i cant really see HOW the answer became 25 1, step by step..please clarify for me please.
Thank you very much!
Never be afraid to put some System.out.println statements in the code to see what is going on. If "count" is something you want to understand how it is changing, put some statements in just before and after each place where count can be changed, and print its value before and after.
Well, the output is final value of the local variable count (25) and the final value of the counter field of one instance of Mix4 (1).
count starts at 0
there is a loop that iterates twenty times
each time through the loop, count is incremented by 1
each time through the loop, count is additionally incremented by the result of the maybeNew() method
maybeNew() returns 0 or 1. Everything else it does has no meaning whatsoever.
maybeNew() will be called with the values 0, 1, 2, ..., 19, but returns 1 only for 0, 1, 2, 3, 4, i.e. five times
so count gets incremented by 20 by hitting line 10 twenty times
and count gets incremented by 5 more by the five times maybeNew() returns 1
that makes count 25.
Start by searching this forum. You will find other threads about the same question, for example this one. Go through that and see whether it gives you any useful information.
Post by:autobot
Those cherries would go best on cherry cheesecake. Don't put those cherries on this tiny ad:
a bit of art, as a gift, that will fit in a stocking