Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Head First Jave Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Jave "EchoTestDrive" Pool Puzzle" Watch "Head First Jave "EchoTestDrive" Pool Puzzle" New topic
Author

Head First Jave "EchoTestDrive" Pool Puzzle

Mike Hudek
Greenhorn

Joined: Dec 31, 2005
Posts: 17
Hi

I imagine there can be more than one answer... I came up with the following alternative code which seems to do the same sort of thing (anyone else who has worked through this exercise in this book might like to compare!



and the code for the Echo class itself:



This code will still, when compiled and run, give four repeats of the "Helloooo..." and a value of 10 for e2.count at the end of the routine.

Cheers!


Mike
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
What is your question?


Java API Documentation
The Java Tutorial
Mike Hudek
Greenhorn

Joined: Dec 31, 2005
Posts: 17
Hi Layne,

It's not clear in the book that there are alternative solutions that will give the required print out, so I was wondering if it were intentional, or if there was anything that made the answers in the Head First book 'righter' (or stylistically preferable) to my alternative solution. Maybe others who have worked through this book came up with others?

Regards,

Mike
Stuart Goss
Ranch Hand

Joined: Mar 21, 2001
Posts: 169
Hi Mike,

there is no need to write



count is a class member data field and will automatically be "nulled" (set to a default value, in this case "0"), when an instance of the class is created.

Sorry, can't say much more than this. I can't remeber having problems with the exercises. And I don't spend much time on the puzzles. Perhaps the next time I read the book (if I ever have the time).

Stuart
[ January 16, 2006: Message edited by: Stuart Goss ]
Mike Hudek
Greenhorn

Joined: Dec 31, 2005
Posts: 17
Hi Stuart,

Yes, I was wondering about that... but the code structure contained this line so it had to be used to complete the exercise properly.

Personally I find the puzzles in the Head First Java book very satisfying, they often get me testing how well I understand, remember and interpret java notation and code - and I feel that they are a feature of this text that sets it apart from mere reference books.

For example, I just completed (successfully I have to add ) the "A Heap o' Trouble" puzzle at the end of chapter 3, and while I was in the process of figuring it out, I realised I had a major query/confusion regarding the way in which reference variables are created as copies of other existing reference variables in parts of code such as the last line of the following:



So... I wrote a completely different java code that highlighted my confusion, and I included plenty of System.out.print()statements at various stages so I could clearly see what was actually occurring, and what was overwriting what. Suddenly it became far more 'obvious' (dangerous word, I know!) and then I went back to the exercise and sorted it out immediately!

Regards,

Mike
[ January 16, 2006: Message edited by: Mike Hudek ]
Curtis Brown
Greenhorn

Joined: Jan 10, 2006
Posts: 20
So... I wrote a completely different java code that highlighted my confusion, and I included plenty of System.out.print()statements at various stages so I could clearly see what was actually occurring, and what was overwriting what. Suddenly it became far more 'obvious' (dangerous word, I know!) and then I went back to the exercise and sorted it out immediately!


This issue has got me wondering....

I haven't used an IDE since 1996 when I was developing a database in 4D. One of my favorite features of that software was a built-in debugger that would allow you to step through your scripts one line at a time, with a side-panel showing all references, variables, etc. and their current value.

Is this a statndard feature in an IDE these days, or is it in them at all? Do you need a de-bugger for this feature? I'm so clueless on that topic, I honestly don't even know how to ask appripriate questions about it!

^CB
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Mike Hudek:
Hi Layne,

It's not clear in the book that there are alternative solutions that will give the required print out


In programming, there are always alternative solutions to a given problem. Usually alternatives come with different trade-offs. For example, one solution my use a minimal amount of memory at the cost of execution time while another may use some extra memory to increase speed. This is where design comes in. Typically, you will need to consider the requirements and how it will affect end-users. Of course, in puzzles like the ones you are doing, you are the only end-user, so many of these trade-offs are only considered as a learning exercise.

Originally posted by Stuart Goss:
Hi Mike,

there is no need to write




While this is technically true, many programmers prefer to explicitly initialize variables like this. If someone else reads this code (or if you come back to it later), the intial value is perfectly clear. This way you don't have to worry about remembering the default values that Java uses and you don't have to worry about these default values potentially changing (although that's highly unlikely).

Originally posted by Mike Hudek:
So... I wrote a completely different java code that highlighted my confusion, and I included plenty of System.out.print()statements at various stages so I could clearly see what was actually occurring, and what was overwriting what. Suddenly it became far more 'obvious' (dangerous word, I know!) and then I went back to the exercise and sorted it out immediately!
[ January 16, 2006: Message edited by: Mike Hudek ]


This is a great technique! Keep it up!

Originally posted by Curtis Brown:


This issue has got me wondering....

I haven't used an IDE since 1996 when I was developing a database in 4D. One of my favorite features of that software was a built-in debugger that would allow you to step through your scripts one line at a time, with a side-panel showing all references, variables, etc. and their current value.

Is this a statndard feature in an IDE these days, or is it in them at all? Do you need a de-bugger for this feature? I'm so clueless on that topic, I honestly don't even know how to ask appripriate questions about it!


Yes, such debuggers are a standard feature in modern IDEs. However, in the case you quoted, I think System.out.println() (SOP) is the way to go because you can just run the program and view the output without taking too much time. If you are confused by why a particular piece of output appears, then you can take the time to set a breakpoint in the code and view values of variables and such with a debugger.

Layne
William Hess
Greenhorn

Joined: Feb 19, 2009
Posts: 1
It's three years later; hope you're still available. I got exactly the same answer as you did. Unfortunately, I had to hack at with eclipse. An old professor railled vigorously against hacking (it's not what computer scientists do), that it's always stayed with me. These pool puzzles are very hard for me; but I'd really like to find a methodical approach to solving these problems.

Sincerely
Billy Hess
red-sox@cox.net
2/19/09
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Welcome to JavaRanch

It is probably not worth reopening an old thread; we usually regards more than 2 weeks as too old. Please read this FAQ.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Head First Jave "EchoTestDrive" Pool Puzzle