• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Head first Java Puzzle page 24

 
Greenhorn
Posts: 10
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
java1.png
[Thumbnail for java1.png]
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.
 
Ranch Hand
Posts: 163
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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

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
Posts: 10
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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.
Head_First_Java_Pool_Puzzle_1_Page_24.png
[Thumbnail for Head_First_Java_Pool_Puzzle_1_Page_24.png]
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got this right... here is the solution:

 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You have posted the same thing twice, so I have deleted one copy.
If you indented the code, I can add code tags, which usually make the post look much better
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks @arjun ..nice to see the solution you gave ...alhamdulillah..
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it shows that old threads can still be useful And welcome to th Ranch
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arjun K Prasad wrote:i got this right... here is the solution:



This solution is incorrect as you have used x<1 twice Misread the puzzle
 
lili sadili
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Yes, it shows that old threads can still be useful And welcome to th Ranch



thanks sherif..glad to know all you guys..
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello to everyone!
Got some troubles with the PoolPuzzleOne. Looks like they came with russian translation =)
20160926_144338_.jpg
[Thumbnail for 20160926_144338_.jpg]
1
20160926_144349_.jpg
[Thumbnail for 20160926_144349_.jpg]
2
 
Anton Levin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or I did undestood something wrong )
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch:)

Please explain what problem you are having.
 
Anton Levin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch:)

Please explain what problem you are having.


TY, Ritchie!

Ill try to explain it as clear as I can.
1. fragments *print("a ") and *print("n ") has a space symbols which are doesnt allow us to make the the nedded phrases. The printing will be like that: "a n oise" for example.
2. There is a such string in the solve of this puzzle: System.out.print("n"
How can it work?
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't check properly because I don't have my HFJ to hand at the moment, but there doesn't appear to be "n " in the solution. I do not know whether there is a printing mistake or something.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
print("n is a mistake; it shou‍ld read
System.out.print("n");
Otherwise the code shown there works and prints

A noise
Annoys
An oyster

 
reply
    Bookmark Topic Watch Topic
  • New Topic