This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
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
Pie
Posts: 20532
54
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
Sheriff
Pie
Posts: 48972
60
  • 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
Sheriff
Pie
Posts: 48972
60
  • 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
Sheriff
Pie
Posts: 48972
60
  • 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
Sheriff
Pie
Posts: 48972
60
  • 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
Sheriff
Pie
Posts: 48972
60
  • 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: 12126
30
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: 12126
30
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic