Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Feeling stupid after Chapter 1 Pool Puzzle  RSS feed

 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These Java Pool Puzzle's are kind of upsetting me and making me feel as if I'm incompetent or something. I have done the Java Pool Puzzle's in both chapter 1 and chapter 2 in the Head First Java book. I feel as if I have a great understanding of the material, but the PoolPuzzle's really get to me.

I'm not sure if any of you guys remember the pool puzzle in book, but basically I am supposed to use each snippet once from the pool and fill in the blank spaces.

The first sample went something like this:



The final output should look like this:

a noise

annoys

an oyster



We are given the following code snippets only to be used once where the blank lines of code are needed:

System.out.println(" ");
System.out.println("a");
System.out.println("n");
System.out.println("an");
System.out.println("noys");
System.out.println("oise");
System.out.println(" oyster");
System.out.println("annoys");
System.out.println("noise");

x > 0
x < 1
x > 1
x > 3
x < 4

x = x + 1;
x = x + 2;
x = x - 2;
x = x - 1;

I understand the first while conditional statement of while ( x < 4 ), but after that, I do not understand. Please explain it to me. Thank you!
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure it is meant to be println and not print?
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahmed Bin S wrote:Are you sure it is meant to be println and not print?

Looks like three of them should be println, and the rest should be print.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naziru, please post your attempted solution and what it prints. We'll see what we can do based on what you've got so far.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:
Ahmed Bin S wrote:Are you sure it is meant to be println and not print?

Looks like three of them should be println, and the rest should be print.


The last Sys.out in the loop is a println(), so all the others (unless there is supposed to be a blank line) ought to be print(), as far as I can see.
Also, shouldn't the x < 4 be x < 3? There's only 3 lines to build.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
Stevens Miller wrote:
Ahmed Bin S wrote:Are you sure it is meant to be println and not print?

Looks like three of them should be println, and the rest should be print.


The last Sys.out in the loop is a println(), so all the others (unless there is supposed to be a blank line) ought to be print(), as far as I can see.


The OP says,

Naziru Gelajo wrote:The final output should look like this:

a noise

annoys

an oyster

So, there being no newlines in any of his snippets, I figured there had to be some printlns in there, to get the three lines. You're right that the last one is in the hard code, so that means there only have to be two, not three. (Not sure how to get the blank lines at all, actually).
 
Campbell Ritchie
Marshal
Posts: 55722
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have my copy of HFJ here, but I think there should not be blank lines like that, so there need not be any additional println instructions.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could really do with the actual question then...


Any comment on whether x < 4 is wrong?
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Any comment on whether x < 4 is wrong?

Heh. Only that debugging incomplete code is slightly less fun that pounding with a hammer on my thumb. Yes, I do wish the OP would say more.
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
Any comment on whether x < 4 is wrong?


you have x = x + 2 too, so calling x = x + 1 (twice) and x = x + 2 (once) can have the desired result, but given the OP's shoddy question, it's only right to ask!
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ladies and Gentlemen, thank you for responding to my thread. I actually found the answer on a similar thread. Nothing was written in error but thanks for your amazing insight. Here is where I found the solution:

http://www.coderanch.com/t/638161/java/java/solve-PoolPuzzleOne
 
Sevastian Kichgin
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day to everyone.

Can you tell me how that kind of task is named? The pool puzzles in "Head first".

What kind of logic we use to create or to solve theese puzzles when you got some parts of program (text lines, letters... in our case) and you need to activate them in certain order.

What is the name of that scientific element? Where can I read more about that? I tried but I don't know how to formulate a search reqest. It's logic or it's math?

I will be grateful for answering, it's really interresting to me.
 
Campbell Ritchie
Marshal
Posts: 55722
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am not sure that there is a name for that sort of puzzle.
 
Reuven Kishon
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if this was captured, but I struggled mightily with this one because of two reasons:

1) I assumed there was a need to print two new lines after each line of text (otherwise how would you get that blank space outputted between each line?)
2) It looked as if the System.out.print("a") and System.out.print("n") commands had a space after the a and n.

May alleviate some others frustrations if the above were rectified...

Thanks!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!