• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Head First page 92 Pool Puzzle

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've hit a wall and I can't get my head around it.

I'm doing my bachelor in software engineering from home so I don't go to any classes. Also I don't have anyone in my direct social circle who knows Java.

Up till page 90 I was getting it pretty well but somehow a concept is not clicking so I'm hoping someone could dumb it down enough for me to get it.



the output is: result 543345 and I simply don't get where those numbers come from. Really not a clue.

One thing I don't understand is what for instance obs[x].ivar does. Does the value of of the new Puzzle object with a reference of obs[x] become the same as the integer ivar?

Same thing with result = result + obs[x].doStuff(x); I'm not sure what I have to do with obs[x].doStuff(x); Or better put, what they are doing to each other.

It's frustrating me to no end as I was cruising along nicely, getting all of it, and suddenly this pops up and I'm not sure where it was covered in the book. I'm also suspecting it's not that hard and if I read back this post in a couple of months I'll hang my head in shame and answer a couple of other beginners patiently just to make up for it.

I think y gets up to 1.000.000 in the 6 times it goes through the first while loop. And then we also go through the second while loop 6 times which modifies integer result every time.

So if anyone could in as simple as possible words explain the logic here. Don't worry, you won't hurt my feelings! That would be fantastic!




 
Timothy Oldbean
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's an array!

Never underestimate the power of stepping away for a couple of minutes and a couple of sips of Crown Royal as a reward for putting in some study hours.

So I get now that the first loop is actually filling up the 6 objects in the obs array. And as it's done before the y = y * 10 it would look like:

obs[0] = 1
obs[1] = 10
obs[2] = 100
obs[3] = 1000
obs[4] = 10000
obs[5] = 100000

So the solution lies in what ivar does with those numbers in Puzzle4b class after the boolean.

At least one part is tackeled, I think.
 
Ranch Hand
Posts: 479
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

One thing I don't understand is what for instance obs[x].ivar does. Does the value of of the new Puzzle object with a reference of obs[x] become the same as the integer ivar? ... Same thing with result = result + obs[x].doStuff(x); I'm not sure what I have to do with obs[x].doStuff(x); Or better put, what they are doing to each other.



obs[x] refers to an object in the array obs at index x.
obs[x].ivar refers to the variable ivar in that instance.
obs[x].doStuff(y) invokes the method doStuff() on that instance, passing in the value of y.
result = result + obs[x].doStuff(x) invokes doStuff() and adds whatever it returns to result and stores the sum back in result.

I can only guess that this is an exercise in following the logic as though you were the computer; the calculations don't have any discernable use, and in a well-engineered program would have comments to explain what was going on. Of course, if you ever work as a computer programmer, you will have to disentangle just such logic all the time, without comments. One thing they certainly don't tell you, or tell you often enough, in programming school (or software engineering school or computer science) is that you will read much more code than you write.

I've put comments in here not as they would be for the program were it actually useful, but in order to explain what is going on to someone close to starting out. I have assumed you know what a 'variable' and 'method' and 'instance' are; if not, you should go back in your studies and figure those out.



rc
 
Timothy Oldbean
Greenhorn
Posts: 17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is a very elaborate and very clear answer.

Thank you very much I can now start digesting this!
 
Marshal
Posts: 77899
373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those puzzles in HFJ are specifically designed to be understood with a pencil and paper.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Holy moses, thank you so much for that explanation! I had worked this out on paper, I had typed it into Eclipse, I had changed the variable names to make it more understandable but it wasn't until I read through your explanation that I got where ivar was getting its value!! Coderanch to the rescue again. Thanks so much!

Heidi
 
Heidi Atwood
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wait, I STILL don't get where we find the value of factor? HELP!
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you may need to go back and study "methods".

When a method is called, it may (or may not) have one or more "parameters" passed to it. The place from which the method is called puts one or more names of variables in the parentheses following the method name, and the values of those are "passed to" the method.

The variable "factor" in the code above is a parameter variable; when that method uses the parameter passed to it, that is what it calls the variable.

So the calling code does something like: "int x = doStuff(y);"

That calls the method doStuff and passes it y, which must be a variable in the calling code. Then the doStuff method might look like:

public int doStuff(int factor)
{
int result = factor * 68;
return result;
}

In our tiny example here, when the call in the first code was made, doStuff would get the value of y and be able to use it with the name 'factor'.

If this is not clear to you, you are really going to need instruction on how methods work in a larger context than I am liable to type out here 8>)

rc
 
Heidi Atwood
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When a method is called, it may (or may not) have one or more "parameters" passed to it. The place from which the method is called puts one or more names of variables in the parentheses following the method name, and the values of those are "passed to" the method.



I knew this, sorry, I've only been studying Java for about a month and I can't seem to keep hold of all the odds and ends. That makes much more sense now, but if this is the line of code in which doStuff is being called:



wouldn't the variable being passed be x? I'm sorry for being so thickheaded!!!

Heidi
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to try once more, but this really deserves several pages of explanation from a book, not what I can type in in my spare time.

The calling routine wants to make use of a method named doStuff; this method accepts (and demands) one parameter, an integer.

The *calling* routine can pass any variable it wants to into the method; let's say it has three integers and it is going to doStuff() with all of them:



So, that's in the calling routine. Now, the doStuff code must have a variable name for the parameter that is passed into it. It isn't going to be the same as ANY of the names of variables used by any callers; one great usefulness of a method is that it can be called multiple times from multiple places, and each call can pass in whatever is needed at the time:



So x, y, and Z are indeed parameters "passed in"; you can think of them as each being called "factor" by the method while it is executing. The method does not know or care what the variable is called by the caller. In the same vein, stuffResult is known only to doStuff(); since it is the value returned by the method, its value is assigned to result1, result2, and result3 by our three calls. We end up with those being equal to 9, 15, and 24 respectively.

I hope that's more clear.

rc
 
Heidi Atwood
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, and thank you for the explanation. I understand that factor can take on whatever the value of the variable is that it is being passed in the method. My confusion was that nowhere in the code did I see where y was specifically being passed into the method doStuff, I didn't understand you were using that as an example. I apologize for not getting it sooner.

Heidi
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This puzzle has made me question my decision to buy the book. I understand the replies I could never come up with the solution. I have had a very limited programming background and that was ten years ago. So I suppose I do not meet the prerequisites. Is there a better book out there for folks not smart enough for the Head First books?

BTW, while I understand the replies, it took me four tries with the solution in hand to work it out on paper. This just seems to be too much difficulty this early in the book.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic