• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Head First Jave "EchoTestDrive" Pool Puzzle

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is your question?
 
Mike Hudek
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Tell me how it all turns out. Here is a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic