This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

PoolPuzzle from Head First Java has me stumped  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

New to the forum, but have been lurking for a couple of weeks now (since I started getting into Java). You all seem like a very helpful bunch and I'm glad to be a part of the community. I'm currently reading Head First Java (2nd edition). A little stumped on one of the exercises. I've found the solution online, but many of the parts I simply don't understand what is going on. I've tried to research online, with little success. The code is below, and as you can see, I've added some comments at the parts where I THINK I understand what is going on (if I'm wrong, please do correct me).

When I compile and run, I get:

a noise
annoys
an oyster



Now, the part that is really throwing me off the most is the x<1, x==1, etc... (ie. == means equal, = means assigns a value)
I understand the while loop (x < 4)..basically means run this as long as x is less than 4
But the nested stuff is throwing me off. I've looked at the code for a long time, and it's bugging me that I can't figure it out.
Any advice?

 
author
Sheriff
Posts: 23588
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tommy Moe wrote:
But the nested stuff is throwing me off. I've looked at the code for a long time, and it's bugging me that I can't figure it out.
Any advice?



Welcome to the ranch !!

I recommend following the loop through each iteration, and write everything down keeping track of the variables (and output) for each iteration. Make a table so you can distinguish each iteration. It takes some experience before you can do it just by looking at it -- so I would not recommend staring at the code.


And BTW, the term "nested" generally refers to nested loops, and not really used in regards of "if" blocks inside a "while" loop.


Hope this helps,
Henry
 
Tommy Moe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:

Tommy Moe wrote:
But the nested stuff is throwing me off. I've looked at the code for a long time, and it's bugging me that I can't figure it out.
Any advice?



Welcome to the ranch !!

I recommend following the loop through each iteration, and write everything down keeping track of the variables (and output) for each iteration. Make a table so you can distinguish each iteration. It takes some experience before you can do it just by looking at it -- so I would not recommend staring at the code.


And BTW, the term "nested" generally refers to nested loops, and not really used in regards of "if" blocks inside a "while" loop.


Hope this helps,
Henry



Thanks Henry, I really appreciate it! Could you possibly link me to an example of a table that you a referring to? That sounds like a wonderful idea (I tend to understand things better when I write them out), but I just don't know where to start.

Also, thanks for the tip in regards to the term nested. Glad you cleared that up for me early on; certainly don't want to get into the habit of using bad lingo.
 
lowercase baba
Bartender
Posts: 12627
50
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just keep doing what you've been doing with the comments, but on a separate piece of paper (or electronic document...). something like:

line 6: Assign 0 to integer named x
line 8: x is 0, so x is less than four - enter while loop
line 9: print 'a'
line 10: x is still 0, so x is < 1, so print " "
line 13: print "n"
line 15: x is NOT > 1, so jump to line 19
line 19: x is NOT == 1, so jump to 22
line 22: x is < 1, so enter if body
line 23: print "oise" and a newline (Note: is this really a println in the text?)
line 24: print a newline
line 27: x is assined to be 0 + 1, or 1
line 29: loop back up to line 4
....and keep going...


 
Sheriff
Posts: 5121
138
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Henry is suggesting doing something like this. (you should fill in the other columns and rows.)

Note: when I started this reply, fred's comment had not yet posted.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12627
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Note: when I started this reply, fred's comment had not yet posted.


yeah...that happens a lot around here.

Same concept, just different execution.

Ultimately, the OP should find the way that makes the most sense to them - and by all means, adapt it as they get more comfortable with the language.
 
Knute Snortum
Sheriff
Posts: 5121
138
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred: just wanted you to know that I didn't look at your post and think, "I have a better way!"
 
fred rosenberger
lowercase baba
Bartender
Posts: 12627
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't think that at all. it was a different way was all.

and I can't tell you the number of times I've started a reply, posted it, then seen that someone else posted before me. or I see someone make a post a minute after me. And honestly, a lot of the time theirs IS better than mine.
 
Tommy Moe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are all awesome. Thank you so much!

The if statements where throwing me off, but I get it now. Just one question. In this case, the largest x becomes is 2 (in order to get the output the book wants). What was the point of "x < 4" at the beginning? Wouldn't x < 3 made more sense? Does it make any difference in terms of good "software engineering" principles or software speed?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12627
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that many of these examples are contrived. My guess is that they were trying to illustrate some concepts and not trying to write the best possible code. After all, the 'best' way to do this would be with one (possibly three) print statements, not use a counter to get clever with spaces and such to append data.

Of course, it could be considered an "off by one error". Maybe Bert will chime in - haven't seen him around recently, but he does pop in every once in a while.
 
Bartender
Posts: 3320
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:fred: just wanted you to know that I didn't look at your post and think, "I have a better way!"


Hey and even if you do read one of our posts and think "I have a better way" then let us know.
We don't have a monopoly on being right and if your way is a better way of solving the problem then we get to learn something as well as the OP.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!