• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Java - Chapter 4 Mixed Messages Puzzle p.90  RSS feed

 
Daniel Vlad
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I recently decided to start learning Java so i started reading the HF Java 2nd Edition. I've gone through the first 4 chapters and did all of the exercises but this one is giving me a headache.




Output: count=14
m4a1.counter=1
x=9


I wrote the code down on paper and tried to understand what it did after which I created the class in Eclipse and ran it to see what happens, needless to say my guess was nowhere near the actual result. So i banged my head against the code for quite a while(i commented out a bunch of lines, replaced them with some of my own , then added some extra lines to the code just to figure out how things worked) and in the end I managed to get what's going on in the class but i still don't understand a few things:

1. Why does "m4a[1].counter" equals 1 (for that matter m4a[0-9].counter equals 1), i was under the impression that it should increase with each pass through the loop.

2. I know why the maybeNew() method is returning something , but what exactly does "return 1" do to the method. I've replaced "return 1" with "return index" and i understood exactly why the result changed in the main method, but i don't seem to get what's going on with "return 1".

I apologize if the questions are too "easy"(English isn't my first language so i tend to translate some phrases exactly as they are written in Romanian and they usually don't make much sense ).

Any help with these issues of mine will be greatly appreciated , I will of course keep trying to figure them out in the meanwhile.

Thanks,
Daniel
 
Bernhard Waidacher
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey^^

The first question is easy.
You asign the value 1 to every Mix4 object which is created.
May you can explain what the variable counter should count.

Second question.
It is a difficult question. Because we have to gues what the function may could do.
I supose it returns 1 everytime a new object could be created.
But as the function is written now, it don't make much sense for me ^^


(BTW: I think your English is good. English isn't the first language for me too. But I think, for both, me and you, our English is acceptable, altough I know I sometimes make mistakes, and I also think that sometimes my sentences doesn't make much sense xD )
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bernhard Waidacher wrote:But as the function is written now, it don't make much sense for me ^^

That's because it's meant to confuse you. The entire object m4 is irrelevant. It's created, it's counter is increased, and it's then discarded. All maybeNew really does is return 1 if the index is <5 and 0 otherwise.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are supposed to go through the whole thing with a pencil and paper and write down the values of each object. Then you can see which bits confuse you.
 
Daniel Vlad
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to your answers i managed to fully understand what's going on in that class.

Campbell Ritchie wrote:You are supposed to go through the whole thing with a pencil and paper and write down the values of each object. Then you can see which bits confuse you.


That's exactly what I did Campbell, after that didn't get me too far I used Eclipse to go through the code and try to figure out how it works. As I said, I did understand what maybeNew() did(managed to figure out that "m4" was there just to throw us off) but for some reason I got stuck on those two issues I mentioned(the fact that i spent way too long in front of that problem without taking a break might have something to do with it). I think i kept associating "return 1" with the C++ version of it(or at least what we learned at school about it).

Anyway , thank you all for your answers and I hope the next issues I run into will be a lot further into the book
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I trust you realised the maybeNew method has a red herring in? Look at m4 and work out what is done with it outside that method. Rob has given a heavy hint about m4 already.
I think return 1; means the same in all the C-based languages.

Let's try maybeNew when x = 1 and count = 99.


... maybeNew(1) ... Since the value is less than 5, it returns 1. So count goes up to 100. And we already know what the value of m4 is used for. When I ran it I got 14 for count and 9 for x. You have already had it explained why m4a[1],counter is 1.
By looking closely at the loop, which runs 9 times, and the method maybeNew, counting how many 0s and how many 1s it returns, you should be able to guess where the 14 comes from.
 
Daniel Vlad
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I trust you realised the maybeNew method has a red herring in? Look at m4 and work out what is done with it outside that method. Rob has given a heavy hint about m4 already.
I think return 1; means the same in all the C-based languages.

Let's try maybeNew when x = 1 and count = 99.


... maybeNew(1) ... Since the value is less than 5, it returns 1. So count goes up to 100. And we already know what the value of m4 is used for. When I ran it I got 14 for count and 9 for x. You have already had it explained why m4a[1],counter is 1.
By looking closely at the loop, which runs 9 times, and the method maybeNew, counting how many 0s and how many 1s it returns, you should be able to guess where the 14 comes from.


Yea, managed to understand what everything in there does(and how) and it's such a relief, now i can move on to the next chapters with a clear conscience .

About the return 1 if i remember correctly, in C++ ,if used in a main method it means the same thing as EXIT_FAILURE and probably that's why it confused me.

Thank you for the help,
Daniel
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a Java™ convention of using 0 for normal exit and non-zero for exit on an error, but you usually only use that in System.exit(int). Anywhere else, return 1 counts as a normal value.
 
Jay Hale
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i'm trying to determine at the end of the while loop in the specified quote in the OP will be
Since the 9 lines for m4a are (0-8) and maybenew is 5 runs (0-4). Am I on the right thinking path here? Thanks!!

count=count+m4a[8].maybeNew(4)

Which give the end result of 14 for count.
 
Akin Millone
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I salute everyone,

I guess this Mix4 code is the first "bus-stop" one gets to while reading Head First Java. The 'bus' just runs smoothly until we see public class Mix4().

I'm as good as stuck... but I hope things will change as I go through the explanations in this forum for the x= x+1'th time (I'm really hoping).

I don't know if there's been any diagram (like all those 'cup' diagrams in the book) to show what's really happening in the class, with the array as well as with the counters; please, if there is, somebody help!!!

Or can someone lend me his brains?

private class Learner() {
confusedLearners= confusedLearners +1;
return enlightened;
}
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

What did you get when you drew diagrams of the mix objects? You would want something with 20 mix objects. That means 20 lines labelled 0 to 19. On each line you can write its counter field.
 
Akin Millone
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I seem to have found my way... Here's what I did: I altered the code slightly, adding lines to be printed out at every calculation stage to figure out what was actually happening to count, counter, x, m4a[x].counter and m4a[x].maybeNew[x].



It turned out that in the latter part of the code, i.e, the lines ...seemed to be the ones causing all the confusion as I discovered they weren't really doing anything! <<< please, correct me if I'm wrong >>>

Thanks to Campbell Ritchie and others... I know you'll see more of me in the coming weeks.

Till then,
private class javaHeadFirstReader() {
// ;)
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done working it out
 
Ben Polaski
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys.
I started reading this book recently and I also got stuck on this piece of code.I`ve got a problem with this line

count=count+m4a[x].maybeNew(x);

What m4a[x].maybeNew(x) is?Can you explain this?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Polaski wrote:
I started reading this book recently and I also got stuck on this piece of code.I`ve got a problem with this line

count=count+m4a[x].maybeNew(x);

What m4a[x].maybeNew(x) is?Can you explain this?


m4a is an array that holds objects of type Mix4.

x is a variable used as a counter in a loop. so, each time through the loop, we look at a specific element in the array - the xth one. It looks like in this loop, x will range from 0-8. each time through, when we look at that object, we call that objects maybeNew method, passing it the value of X.
 
Ben Polaski
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah.maybeNew method returns either 0 or 1.Lets say it returns 1.How does that fit in with m4a[x].maybeNew(x) for lets say x=0?
Is it the same as m4a[x].counter?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
step through the code:



x is zero, so it is less than 9. enter the loop.
m4a[0] is populated with a new Mix4 object. (from this: m4a[x]=new Mix4();)
The object reference by m4a[0] has its counter incremented (m4a[x].counter=m4a[x].counter+1;)
count is incremented (count=count+1; )
count is reset to a new value of whatever is was plus whatever m4a[0]'s maybeNew method returns when you pass it a 0 (count=count+m4a[x].maybeNew(x);)
x is incremented (x=x+1)
jump back to the top of the loop and see if we should run again.
 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a quick one if anyone is even listening to this post 5 years down the line, but i have just recently started to learn java and so decided based upon numerous review that this book was a good place to start, my question is this, if by my understanding of this puzzle that the maybeNew methods if statements doesn't have any meaningful impact other than returning 1, is this really conducive to a BEGINNER java programmers learning to throw them off at such an early stage with red herrings and meaningless code such as:

Mix4 m4 = mew Mix4();
m4.counter = m4.counter +1;

Please correct me or explain to me how this helps when beginners seem to be spending hours wasting time trying to figure out something that is:

A. Irrelevant.
B. Does not contribute anything to the existing code.

This method either returns 1 or 0, why confuse a beginner when its difficult enough just grasp the basics.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Judging by how many people get that example wrong, it is worth doing.
The idea is to work through the code with a pencil and paper, until you understand why the answer is 14. You will have to get used to the values of fields, and know where things are in arrays, to understand what your code is doing.
There are exams which ask exactly that sort of question.

And welcome to the Ranch
 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply,

While i fully understand and appreciate your point of view, i feel my question still remains in that how can placing meaningless and deliberately misleading/confusing code into a BEGINNERS exercise help them learn when in reality all it serves to do is further confuse, demoralize and frustrate the neophyte, for a book that purports to be all knowing in its methods of teaching and learning using a strong psychological argument as its basis i fell these pool puzzles are just too much for BEGINNERS, and while i can fully grasp and understand the code once i have looked at the answer and then as you rightly state iterate this on paper, this still is not the original intention of the authors of the puzzle, i would venture that for a BEGINNER having to figure out what goes where is just simply too much, being able able to understand the code once its completed (which i can), and being able to construct the code into something that outputs the desired result (which i can't), are two completely different things, i also don't feel that using the example of the fact that they sometimes have exams with that style/type of question is a strong argument as this is a BEGINNERS BOOK and so nobody in their right mind would be/should be attempting any sort of programming language exams style questions with only 90 pages of java programming experience under their belt (page 91 being the pool puzzle in question).

So to summarize my question now would be, how is it in your opinion that by your own admission placing exam style questions into a BEGINNERS book is beneficial or conducive to the neophytes learning process?

Thanks.

K.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tell us: have you ever had a for loop go wrong becuase you have counted the values wrongly? This exercise is only one stage beyond working out how to get a for loop to run the requisite number of times.
 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show me a beginner that hasn't, i'm not sure i understand your point?
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it forces you to use a pencil and paper to work out what has gone wrong. Because it shows you the literal way a computer “thinks”.
Until you get into the way of thinking how computers work things out, you have no hope of writing code which works. You need to learn that sometimes pencil and paper are the only tools which allow you to crack a programming problem. Getting away from the screen and onto paper is a necessary skill; as Fred Rosenberger often says, programming is 90% thinking. Mixed messages is straightforward to solve if you do it with pencil and paper properly.
If you don't get into the habit of thinking before you write something down, your programs will take much longer to write and will be full of errors.
Also even the simplest questions in an exam will catch you out.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!