• Post Reply Bookmark Topic Watch Topic
  • New Topic

Mixed Messages Problem  RSS feed

 
Dave DiRito
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to understand the logic of the Mixed Messages problem in Head First Java, Chapter 4, page 90. I see that an array of instance variables named m4a of type Mix4 is created and it has a length of 20. If the while statement runs Mix4 objects get created and assigned to the m4a reference variables. Then we increment each counter of each Mix4 object. Then count gets incremented.

Can someone explain this next line of code to me?


I understand that the last part of it calls the method maybeNew and passes it the value of x. What does the m4a[x] part do?

Thanks,
Dave
 
Marilyn de Queiroz
Sheriff
Posts: 9082
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
m4a[x] is the particular Mix4 object at index 'x' in the 'm4a' array that you are dealing with.

Perhaps it would be clearer to you if you add a line after
m4a[x] = new Mix4();
like
Mrx4 currentObject = m4a[x];
and substitute that for every m4a[x] you see after that in the code.

Then you would see
count = count + currentObject.maybeNew(x);
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That line could be broken out into three lines, which may make it more clear:



So all it's doing is getting the Mix4 object at the 'x' index of the 'm4a' array. So if x is 0, we'd be getting the first Mix4 object. When x is 1, the seond; etc.
 
Dave DiRito
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Marilyn and Mark. That helps.

So m4a[x].maybeNew(x) is calling the maybeNew method and passing it the value of x for the Mix4 object that is referenced at m4a[x]. Is that correct?

Then lets say x is 1 when maybeNew is called and index is < 5 and the if statement runs. So it creates a new Mix4 object called m4 and then it increments m4.counter by 1. Is that counter the same as counter outside the method or is it unique because it's m4.counter distinct from the m4a[x].counter's?

Thank you again,
Dave
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So m4a[x].maybeNew(x) is calling the maybeNew method and passing it the value of x for the Mix4 object that is referenced at m4a[x]. Is that correct?

Yup.


Then lets say x is 1 when maybeNew is called and index is < 5 and the if statement runs. So [the maybeNew method] creates a new Mix4 object called m4 and then it increments m4.counter by 1.

So far so good.

Is that counter the same as counter outside the method or is it unique because it's m4.counter distinct from the m4a[x].counter's?

If we look at the Mix4 class definition:

We see that the 'counter' variable is an instance variable. In other words, each instance of Mix4 has its own counter. So since the 'maybeNew' method creates a new Mix4 object which is assigned to the m4 reference, that counter is completely independent of the counter for the object referenced by (i.e. pointed to) the m4a[x] reference. So it is unique.

Later on in the Head First book you'll learn about a way different instances of an object can share a common value for a variable. For now, you know that each instance of an object has its own variables. So the only way the 'maybeNew' method could affect the 'counter' on a Mix4 object from 'outside the method' would be for the 'maybeNew' method to have a reference to that object. In this situation, it does not.

Does that help clear things up?
 
Dave DiRito
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark, many, many thanks! That is a tremendous help. These object oriented concepts are all new to me and your answers to these questions is really helping me understand it.

Just one more question to confirm my understanding. I see that maybeNew returns 1 if the if statement runs and 0 if it does not. By the output of the program it looks like it returns that 1 or 0 back to where it's called from in the statement:

Is that correct?

Your responses are so valuable to me getting this stuff. I appreciate it so much.

Thank you,
Dave

 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dave DiRito:

Just one more question to confirm my understanding. I see that maybeNew returns 1 if the if statement runs and 0 if it does not. By the output of the program it looks like it returns that 1 or 0 back to where it's called from in the statement:

Is that correct?

That's correct. Since all this program wanted to do was add the returned result of maybeNew to the 'count' variable, the code did so immediately via an inline call. If we wanted to do something additional to or with the returned variable, we could store it in a variable. If you look at the code I posted in my first reply, you see I broke that one line of code out into three lines of code. The one line and the three lines do the same thing in the end. The only difference is that in the three line format, we are creating some references to some objects. However since those references are immediately used, and not used elsewhere for anything additional, the end net effect is the same. However, we could, if desired, do something with them. For example, if we want to see, for learning purposes, what the 'maybeNew' method returned, we could do the following:

Notice the added System out?

If you wanted to get real fancy, you could change that to:

When first learning programming, adding some System out statements like that can help you track what is going on. As you get into more intermediate programming, you'll learn about logging frameworks (like log4j and Java Util Logging (JUL)) that are used to do just such a thing. But for now, System out will do the job for you.

Hope that helps.
[ February 18, 2008: Message edited by: Mark Vedder ]
 
Dave DiRito
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark, yes, that helps a ton! You're great!

Thank you, thank you,
Dave
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!