• Post Reply Bookmark Topic Watch Topic
  • New Topic

Just need a little help.  RSS feed

 
Michael Kinberger
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

= 25 1

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!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

I'll let you work out the second part.
 
Michael Kinberger
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot figure out the second part...maybe I should restart the book?
lol
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!