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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head first Java Puzzle page 24" Watch "Head first Java Puzzle page 24" New topic
Author

Head first Java Puzzle page 24

Paulius Kukulskis
Greenhorn

Joined: Dec 27, 2012
Posts: 6

Hi there! This is the first time I am writing here. Just bought a Head First Java book with an intention to become a programmer. And guess what? My illusions started leaving me on the first puzzle in the book. I just couldnt solve it. I mean I could get the answer but I am not able to find a solution. I feel so destroyed. Are there any methodoly to solve these puzzles or this is the indication that programmer's path is not for me?? I would really appreciate if somebody more experienced could reply to this post.

Thanks for your help.

Paul


[Thumbnail for java1.png]

Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 523
    
    3

Great Ghu, what a silly way to teach a person to code! I've heard great things about this book, but that puzzle ain't worth your time, friend (imho).

Please don't take your management of that problem as an indicator of your fitness to write computer programs.
Steve Myers
Ranch Hand

Joined: Dec 08, 2012
Posts: 47
I spent about 15 minutes working on this, and figure it would take me about 45 mins to an hour to solve (and its too silly to continue). I'm a relative newb also. If you must finish it, you just approach it in the usual way - break down the problem into smaller problems.

What I did was just write all the print statements in descending order that I will want executed. It appears we are going to have to use the loop to reuse some combination of "a", "n ", "an".

The condition of the while loop can only be one of two choices and we know we're going to have go through the loop three times, because that's the only way we can get a new line.

There's about where I quit - you will just have to try things until you figure it out, it seems pretty inane.

If you want to do puzzles, I recommend codingbat - these seem more similar to the types of problems I'm encountering in my (limited) experience of writing desktop apps.

Some of the harder level 3 problems can take an hour to come up with a working solution also, but they seem less ridiculous
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2176
    
  47
Welcome to the Ranch.
Bill Clar
Ranch Hand

Joined: Sep 21, 2006
Posts: 150

Maybe you're looking at this the wrong way. Does this kind of problem solving interest you? If you're motivated to solve it, then you'll find an answer.

You can teach someone programming, but you can't teach them motivation.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2176
    
  47
Bill Clar wrote:Maybe you're looking at this the wrong way. Does this kind of problem solving interest you? If you're motivated to solve it, then you'll find an answer.

You can teach someone programming, but you can't teach them motivation.

You are correct in that you can't teach motivation but you can certainly demotivate someone by poor teaching technique such as by giving them tasks that are way beyond their skills or that aren't particularly useful/meaningful (as I personally think this sort of puzzle is).

I think it is a little unfair to suggest the OP isn't motivated, after all he/she has tried to find a logical way to solve it rather than a brute force approach and has then asked here for advice.
Bill Clar
Ranch Hand

Joined: Sep 21, 2006
Posts: 150

Tony Docherty wrote:You are correct in that you can't teach motivation but you can certainly demotivate someone by poor teaching technique such as by giving them tasks that are way beyond their skills or that aren't particularly useful/meaningful (as I personally think this sort of puzzle is).

I think it is a little unfair to suggest the OP isn't motivated, after all he/she has tried to find a logical way to solve it rather than a brute force approach and has then asked here for advice.


I was trying to encourage the OP, not question his motivation. IMO, it's better to tackle a problem with zeal, and fail, then never try at all.

I guess I failed in my wording.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2176
    
  47
IMO, it's better to tackle a problem with zeal, and fail, then never try at all.

Quite agree.

I guess I failed in my wording.

Or I failed in my understanding of what you wrote. Either way it's all cleared up now
Paulius Kukulskis
Greenhorn

Joined: Dec 27, 2012
Posts: 6

I wrote here because of 3 things.

1. Wanted to join the community of Java programmers.
2. Wanted to get the reply about my fitness of becoming a programmer(unable to solve the exercise).
3. Wanted to get the logical solution for the exercise instead of a forward brainless miss/match
approuch.

Now, nobody replied regarding my 3rd point - concerning logical solution of the exercise. I mean
nobody gave any suggestions. So I conclude that the task is really pretty straight forward(?)

Somebody had doubts about my motivation of solving puzzles or motivation of learning Java.
To what I can say that I posted the post when I was at page 24 of the book I am learning from.
Now I am at page 165. And I haven't skimmed through it or smth. As a matter of fact regarding
exercises later on the book, they are all solvable. And I did them all.

The exercise I am asking for help is one of the first in the book. SO it made me think that maybe Java is not for me.
I think this exercise is one of such that if you add 2 times more logical comparisons (x>5) etc. then you spend 8 times more solving it.
So I really don't see a purpose for spending so much time on this one unless there is a logical approach.

Thank you all for your help. Thank you for replies. It is really nice to be a part of this forum.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2176
    
  47
1. Wanted to join the community of Java programmers.

Well this is certainly a good place be. Everyone here is friendly and helpful.

2. Wanted to get the reply about my fitness of becoming a programmer(unable to solve the exercise).

It's not possible to determine that from one exercise, particularly one such as this one. But from your current post if you have made it to page 165 without further problems I wouldn't worry.

Now, nobody replied regarding my 3rd point - concerning logical solution of the exercise. I mean
nobody gave any suggestions. So I conclude that the task is really pretty straight forward(?)

There are two ways of solving this type of puzzle:
1. Brute force: that is you try every possible combination until you find one that works.
2. Logic: Look at each line in turn and deduce what can possibly go on that line. As you go through the problem you will find a line(s) that can only have one possible solution and then with that line in place you will probably find other line(s) that can be resolved. I don't think you will solve it on the first pass. The problem isn't particularly straight forward and the code it will produce is a mess and I can't see what it is trying to teach you so, personally, I don't see the point in completing the exercise.

Somebody had doubts about my motivation of solving puzzles or motivation of learning Java.

To be fair to Bill, his intention was to help motivate you and not to question your motivation. Please see his second post for clarification.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7553
    
  18

Tony Docherty wrote:
2. Wanted to get the reply about my fitness of becoming a programmer(unable to solve the exercise).

It's not possible to determine that from one exercise, particularly one such as this one. But from your current post if you have made it to page 165 without further problems I wouldn't worry.

@Paulius: The only thing I can add to Tony's good advice is that you shouldn't be able to solve all the problems you run into. If you can, they're probably too easy; and you usually learn more from tackling something that's just beyond your capabilities than something you can do with your eyes shut.

So, don't beat yourself up about not seeing the solution: learn from it.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Satish Raju
Greenhorn

Joined: Apr 03, 2014
Posts: 1
Paulius Kukulskis wrote:Hi there! This is the first time I am writing here. Just bought a Head First Java book with an intention to become a programmer. And guess what? My illusions started leaving me on the first puzzle in the book. I just couldnt solve it. I mean I could get the answer but I am not able to find a solution. I feel so destroyed. Are there any methodoly to solve these puzzles or this is the indication that programmer's path is not for me?? I would really appreciate if somebody more experienced could reply to this post.

Thanks for your help.

Paul




Hey buddy,

I just started with Head First Java Book, I have worked in C++, never had hands on Java (fully).

I felt exactly the way you felt, I had become cranky for almost an hour and half - finally nailed it after trying crazy stuff like x = (x-2) * (x+2) ;)... but it is far simpler... only problem I see is we are anxious about the topics which are about to follow, which makes us to haste up the puzzle solving as it is the last step (and as its a puzzle it needs time)..

I have attached the solution with executed output... just in case some one is becoming cranky like me...

Experts please remove the solution if you feel it is in the negative spirit..

Here's how I solved it:
Step 1:
Based on the output - only possible phrases are "a", " noise", "annoys", "an", "noys", " oyster", " "

Step2:
Since all conditions other than "x < 1" and "x < 4", will never allow the flow to enter the while loop.. it has to be "x < 1" or "x < 4", since "x < 1" is already present and cant be used again - it has to be "while (x < 4)"

Step 3:
x > 0 and x > 3 were the left overs which had to be used in the two "fill in the blanks" "if" conditions.

Step 4:
x > 0 will definitely occur more than once as we are printing 3 lines of output, hence from Step 1 - "annoys" is ruled out, and we can be sure that "an" will be printed twice

Step 5:
Below prints has to be in the same order - as it is the output order

if(x < 1)
{
System.out.print("a ");
System.out.print("noise");
}

if ( x > 0 )
{
System.out.print("an");
}

if(x == 1)
{
System.out.print("noys");
}

if ( x > 3 )
{
System.out.print(" oyster");
}

Note: the steps above took no more than 10 mins, but the next step is the craziest (I am sure most of us will give up this puzzle due to the next step, as it consumes valuable time)

Step 6:
Thought: We need to jump x by 1 until we print "noys", after that we need to satisfy while condition of x < 4, at the same time after it enters the loop need set x >= 4 so that " oyster" gets printed.
Most of the time I never had "x = x - 1;" within if(x < 1) - as soon as I realized this - the problem got solved with in a min.
Another thing, what most of us are accustomed to is "we put the incremental/decremental condition at the end of the while loop", which this puzzle points out
once we have printed "noys", we have to increase the counter to 3 (not more than 4), at the same time the counter should go over 4 once it enters for the last time to print " oyster" - which resulted in putting x = x + 2; after if (x == 1) and before if (x > 3).

God I wasted so much time - so to justify it, adding this comment - hoping to save someone's time.


[Thumbnail for Head_First_Java_Pool_Puzzle_1_Page_24.png]

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
Welcome to the Ranch
 
jQuery in Action, 2nd edition
 
subject: Head first Java Puzzle page 24
 
Similar Threads
Head First Java by Bert Bates, Kathy Sierra
Very new to this, could use some help
compiling this java program
Problem with pool puzzle!
Whos reading the "Head First Java" book currently?