• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to solve PoolPuzzleOne ?  RSS feed

 
J. Silver
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I see and understand the solution for PoolPuzzleOne but my question is how to approach this problem? Assuming you don't have the solution. For example by divide and conquer. My intuitive method was to make a table, with a row for each System.out.print("...") option. I then mapped out the sequence you would have to select (including the order of the sequence) to arrive at each of the three desired outcomes. From this I could see the first two steps ("a" and then " ") but I'm not sure how to work out the next bits... thanks!
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For those who don't know what the OP referring to, the PoolPuzzleOne is from the Head First Java 2e book p24
 
J. Silver
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using a Kindle for Mac Version of the text so I'm not sure on what page it appears in the published copy. It is at the end of chap01. The problem statement is to make a class that will compile and run and produce the output listed. The desired output is:

The following is the solution...

So my question is; given the desired output. How would you approach finding the solution? Using nested while/if statements only (no other types of loop).
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not certain what you mean by your question. The Pool Puzzle problem gives you the loop structure for the solution, and asks you to fill in various blanks from a pool of available expressions. To solve this problem you don't need to come up with your own loop structure.
 
J. Silver
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the method to use to choose from the pool of available expressions?
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It really is up to you to find the solution to this puzzle. There are many ways you can go about doing it and most of the time for these Pool Puzzles you will make a mistake and have to rewrite some of the code. I did try the puzzle and I'll post a summation of my thought process.

1. I studied the output. How could I make the output come out in the format it was presented by using the code in the pool? All the outputs are specified as print() and not println(). Very good to know that you have to rely on the open line puzzle code for making new lines in the output.
2. int x = 0. x starts at zero so I know that whatever condition I make in the code needs to pass for x's value.
3. What HAS to happen first? The first line is "a noise" so I know I need to use an 'a' at minimum.
4. I will leave the while loop as undefined for now. I don't know how many times it will run, yet.
5. The first line is blank, I know that 'a' must be printed first so I use system.out.print("a"); for the first empty line. Great, now it will print 'a'.
6. The if test tests for (x < 1). x is less than 1 right now (it's 0) so this if block will run. What needs to happen next?
7. We need a 'space' and then "noise" to be printed.
8. In the if test I will include the space since that HAS to happen next.
9. The next line is tricky. I could put in something like System.out.print("noise") and have "noise" printed. But this statement would be outside of an if block so it will print EVERYTIME. I can't have "noise" printed every single time. What do the output lines have in common that they could have printed everytime? An 'n'. Therefore, I add System.out.print("n"); to the line.
10. Now we have 'a' 'space' 'n' printed, so we just need 'oise'. There is a statement for "oise" it is System.out.print("oise"); But where does it go?
11. The remaining if blocks... One has two empty lines, another has to be equal to one so that won't work and the last has one empty line. That might work.. Also, below the System.out.println(""); there is an empty line? If I put it there that means "oise" would print everytime. Can't have that! So it would best fit in the last if block.
12. Now that I put System.out.print("oise") into the last if block, we need to make sure it passes its boolean test. x is equal to what right now, x is equal to 0. The best suited test to insert would be if( x < 1).
13. What needs to happen next? I need a new line, no code in the pool will provide a new line so I need the System.out.println(""); at the bottom.
14. There is still a problem though, x is still equal to 0. Don't we need to increment x? Yup! So on that last empty line let's throw in an increment statement. Let's try x = x +1. That should work! Now we are at the bottom of the first round of the while loop!
15. Done with the first round of the while loop.
16. So what needs to happen next? We need "annoys" to print. I know that everytime I will have an 'a' printed and an 'n' printed so I know that I can't simply find a spot for system.out.print("annoys"); I have to build "annoys" from an 'a' and an 'n'. What is left is "noys" Anything in the pool for noys? Yup. I see System.out.print("noys"); So I have to find a spot to add this, but where?
16. Well, x is equal to 1 this time and there is an if( x == 1) so let's just try to fill that one empty line in the if(x==1) block and see if it works. So I add System.out.println("noys") to that if block.
17. The 'a' and 'n' were provided (everytime) so adding this last "noys" finished the output of "annoys"
18. We move to the bottom of the while loop, printing a new line and increment x by 1.
19. Done with round 2 of the while loop.
20. x is now equal to 2. And we only have one if block left. So let's try and add to the remaining if block the test of if(x>1).
21. Now that we can access this block, what needs to happen next? We have an 'a' and 'n' printed, so all we need is a space and oyster to print.
22. Look, in the pool there is System.out.print(" oyster")! Everything we need, so let's add it!
23. Great everything should print. But we are still not done, there is one line left in the if block with System.out.print(" oyster"). What could go there?
24. Well we still haven't defined the while loop's test yet so let's kill this loop after about 4 runs, since x<4 is in the pool. So, if x is 2 right now, let's kick it up by using the x = x +2 and the next time this loop tries to run the while loop fill return false!
25. Done!

That was pretty much exactly what went through my mind when I solved this puzzle. They might not all work out just like this one but trial and error is sometimes a great way to go as there are lots of choices. Best of luck to you and I hope this helped!
 
Dave Berman
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for your detailed explanation, Skye. This one had me stymied as well, mainly because my logic is rusty and I didn't have a clue how to approach the problem. It's too bad the book doesn't provide a few hints as well as a walkthrough of the solution (like yours).
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I actually cannot believe this was posted over a year ago today. How time flies! Glad I could help!
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Artemy Pogosov
Greenhorn
Posts: 2
Android Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have my own solution. What's your opinion?

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

You may get the desired output but you used statements that were not in the pool of choices that you were allowed to choose from. Specifically, there are no System.out.println() statements in the pool, only System.out.print(). Therefore, yours is not a valid solution.
 
Artemy Pogosov
Greenhorn
Posts: 2
Android Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! I was mistaken.
 
Stanislav Bartosh
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Silver wrote:



I thought that is "A " symbol with space inside quotes. So, i can't resolve this task because of poor quality of book copy. 
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!