aspose file tools*
The moose likes Beginning Java and the fly likes Head First Java - Chapter 4 Mixed Messages Puzzle p.90 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Java - Chapter 4 Mixed Messages Puzzle p.90" Watch "Head First Java - Chapter 4 Mixed Messages Puzzle p.90" New topic
Author

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

Daniel Vlad
Greenhorn

Joined: Aug 07, 2011
Posts: 29
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

Joined: Aug 06, 2011
Posts: 3
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

Joined: Oct 27, 2005
Posts: 19760
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
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

Joined: Aug 07, 2011
Posts: 29
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

Joined: Oct 13, 2005
Posts: 39828
    
  28
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

Joined: Aug 07, 2011
Posts: 29
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

Joined: Oct 13, 2005
Posts: 39828
    
  28
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

Joined: Jul 28, 2011
Posts: 4
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

Joined: May 27, 2012
Posts: 4
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

Joined: Oct 13, 2005
Posts: 39828
    
  28
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

Joined: May 27, 2012
Posts: 4
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

Joined: Oct 13, 2005
Posts: 39828
    
  28
Well done working it out
Ben Polaski
Greenhorn

Joined: Jul 09, 2013
Posts: 5
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

Joined: Oct 02, 2003
Posts: 11477
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ben Polaski
Greenhorn

Joined: Jul 09, 2013
Posts: 5
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

Joined: Oct 02, 2003
Posts: 11477
    
  16

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Head First Java - Chapter 4 Mixed Messages Puzzle p.90