Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Java  RSS feed

 
zicheng lu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys

Is there anybody can walk me through the following questions given an exercise from Head First Java P.90, "Mix4"


As you can see from the code, there are couple of confusion I don't get it.
First one is : how we suppose to know what is m4a[x].counter? At the loop one, x = 0, so m4a[x].counter = m4a [0].counter, am I correct? Yet, it does not mean any specific figure, or is there anything I have missed out? Thank you very much for your help in advance.

Lu
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

First, thanks for (trying to) use code tags. You didn't get the ending tag; I put it in for you.

Second, there's something wrong with the line that starts with System.out.println. Be sure to PostRealCode (that's a link).

how we suppose to know what is m4a[x].counter? At the loop one, x = 0, so m4a[x].counter = m4a [0].counter, am I correct?

Correct.

Yet, it does not mean any specific figure, or is there anything I have missed out?

Not sure what you mean. It's specifically m4a[0] when x = 0, m4a[1] when x=1, and so on.
 
zicheng lu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Welcome to the Ranch!

First, thanks for (trying to) use code tags. You didn't get the ending tag; I put it in for you.

Second, there's something wrong with the line that starts with System.out.println. Be sure to PostRealCode (that's a link).

how we suppose to know what is m4a[x].counter? At the loop one, x = 0, so m4a[x].counter = m4a [0].counter, am I correct?

Correct.

Yet, it does not mean any specific figure, or is there anything I have missed out?

Not sure what you mean. It's specifically m4a[0] when x = 0, m4a[1] when x=1, and so on.


Hi Knute
Thank you for your prompted reply, and personalized reediting.

The remained question is " how we get answer, 1 for 'm4a[1].counter' ?"

Thank you very much for your help

Lu
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how we suppose to know what is m4a[x].counter? At the loop one, x = 0, so m4a[x].counter = m4a [0].counter, am I correct? Yet, it does not mean any specific figure, or is there anything I have missed out?

If you're asking how to look at the value of m4a[0].counter, then I would say correct the System.out.println statement to look like this:

If you want to know how the program arrived at number, I would write out what the program is doing, line by line. I'll start:

* set count to 0
* initialize m4a to 20 elements
* set x to 0
--- first iteration
* instantiate m4a[0] to new Mix4(), counter is 0
* add one to m4a[0].counter, counter is 1
* add one to count, count is 1
* add m4a[0].maybeNew(0) to count
** index is 0
** instantiate m4 to new Mix4(), m4.counter is 0
** add one to m4.counter, 1
** return 1
* count is 2
* add one to x, x is 1
--- second iteration
* instantiate m4a[1] to new Mix4(), counter is 0
* add one to m4a[1].counter, counter is 1
* add one to count, count is 3
...

You can continue like this as far as you like, then check your assumptions with System.out.println() statements in the code.

HTH
 
zicheng lu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote: . . .
If you want to know how the program arrived at number, I would write out what the program is doing, line by line.
. . .
You can continue like this as far as you like, then check your assumptions with System.out.println() statements in the code.

HTH

Thank you very much for your help and patience Knute! Just one more thing, what is the value of m4a[x].maybeNew(x)? HOW to determine it?

Lu
 
Campbell Ritchie
Marshal
Posts: 55745
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through the maybeNew method and work out what each line does. What does the Mix4 variable mean? What does it do? What difference does the values of the fields of the current object make to what that method returns?
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi op,welcome to Ranch!!!
I find you have 2 doubts,
1-m4a[x].counter -(value)
2-m4a[x].maybeNew(x) -(value)

Let see if I can help
I will not directly tell you the answer, intact I will help you finding it.
So talking about your first doubt,
You should be knowing what an instance variable is?
How a fresh copy of it's is created for every new instance.
Every fresh copy gets a value,either the default one or the
one you assign explicitly
Now after reviewing all these points you should again work on your first doubt


Now talking about your second doubt

Whenever we invoke a method,all those statements written in method gets executed(if there is no conditional statement), then the execution return to the point from where method was invoked and place a value which is returned by method(if return type is not void)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!