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

- 1

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.

All things are lawful, but not all things are profitable.

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 withSystem.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

- 1

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

All things are lawful, but not all things are profitable.

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

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)

Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.

It is sorta covered in the JavaRanch Style Guide. |