This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Very confused with Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Very confused with "object behavior"" Watch "Very confused with "object behavior"" New topic
Author

Very confused with "object behavior"

Jake Miller
Ranch Hand

Joined: Jun 27, 2007
Posts: 43
Sorry to bombard this message board with simple questions, but I am teaching myself this and it's really not going too well. I wasn't sure the best subject line for this question either.

There is an exercise at the end of one of the chapters in this book where it presents most of a code with just 2 parts taken out. You are supposed to put in certain pieces of the code into their respective parts and predict the output. I am having a lot of trouble with this both understanding line by line what is going on, and overall.

Here is the code, as well as what I understand each line is doing. The "//insert ..." are the different pieces the code gives, with the first piece of the first line going with the first piece from the second line. Anything in bold is my comments about the code.



Hopefully this all shows up how I want it to. If not, or it's not clear in any way, please don't hesitate to ask. I appreciate any responses at all since I know this is kind of an involved question. Thanks.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Jake Miller:
Sorry to bombard this message board with simple questions, but I am teaching myself this and it's really not going too well...

That's what the beginners forum is for.
Originally posted by Jake Miller:
Mix4 [] m4a = new Mix4[20]; //Very confused by this one. Declaring arrays, but isn't the name of this class Mix4 also?

When declaring an array, you need to specify the type of elements that will be in the array. So Mix4[] is an array that contains instances of Mix4. In other words, when you create objects based on the class Mix4, you can store them in this array. Of course, a Java array actually holds references to objects -- it does not contain the objects themselves. So this is really an array of references, and Java references must always have a type.

The variable m4a is the variable referencing this array. The keyword "new" creates the array, which in this case has a size of 20. (The size of an array is set when it's created and cannot be changed.) At this point, all 20 references are null because we've only created the array -- we haven't put anything in the array yet.
Originally posted by Jake Miller:
m4a[x] = new Mix4(); //Again, confused...

You're correct that m4a[x] references a particular element in the array. Remember that elements in this array are actually references of the type Mix4. This line is creating a new instance of Mix4 and assigning it to a particular place (x) in the array.
Originally posted by Jake Miller:
m4a[x].counter = m4a[x].counter + 1; //Adds 1 to the counter, but how does this relate to the cell in the array?

Again, elements in this array (like m4z[x]) are references to instances of Mix4. So m4z[x].counter is the "counter" variable in the instance of Mix4 that is stored in m4z[x].
Originally posted by Jake Miller:
count = count + m4a[x].maybeNew(x); //Something gets added to count, but I'm not entirely sure what...

Just as we accessed the member "counter" above, we are now calling the method maybeNew(x) on this instance of Mix4. The method returns an int value of 0 or 1 depending on the "if" condition in that method.

So when it comes to filling in the 2 missing parts, the question is how many iterations you want in the "while" loop, and the condition that should be used in the "if" statement. So now that you have some idea of what the rest of the code does, what do you think?
[ August 13, 2007: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jake Miller
Ranch Hand

Joined: Jun 27, 2007
Posts: 43
Thanks again Marc. I am starting to understand it...slowly. I am still a little confused with the

Mix4 [] m4a = new Mix4[20];

The name of the class I am creating is Mix4, right? So when I create a new Mix4 array, the elements of the array are (ultimately, after putting them in) references to other Mix4s. This seems to me like a neverending cycle: create class Mix4, method create an array of Mix4s, each Mix4 is a class with the method to create new Mix4s in it... Am I misunderstanding this?

I am also confused about the maybeNew method and how the adds in to this whole thing, specifically the
index < 5
part. We declare index to be an int value, right? But we don't set it equal to anything to start with, so how can the program know if it's less than 5, 7 or 1. And then the return 1/0 part, this is returning an int value or 1/0, right? Can it return any other values, such as strings?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Jake Miller:
... This seems to me like a neverending cycle: create class Mix4, method create an array of Mix4s, each Mix4 is a class with the method to create new Mix4s in it...

You're correct that this seems like a never-ending cycle. But the array is actually being created and valued inside the main method of Mix4 (it's a local variable inside main). Therefore, you can create as many instances of Mix4 as you like, but the main method will only execute if it's called.

In contrast, if code like this were inside a constructor for Mix4 (which executes every time a new instance is created), then you would have a never-ending problem.
Originally posted by Jake Miller:
... We declare index to be an int value, right? But we don't set it equal to anything to start with...

In the method maybeNew, "index" is the int value that's passed to the method as a parameter. So when we call the method with maybeNew(x), then "index" is assigned the value of x.

This method is defined as "public int maybeNew(int index)," so it must return an int value.
Jake Miller
Ranch Hand

Joined: Jun 27, 2007
Posts: 43
Originally posted by marc weber:

In the method maybeNew, "index" is the int value that's passed to the method as a parameter. So when we call the method with maybeNew(x), then "index" is assigned the value of x.

This method is defined as "public int maybeNew(int index)," so it must return an int value.


Ok, I get the first part of your post, but I'm still confused about this then:


I am supposed to insert index < whatever. You say it must return an int value, but where/what is this int value?

Thanks again.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343


In the above illustration, the value of 'y' depends on which option you choose to insert as the "if" condition: index < 5, index < 7, or index < 1. And remember that "index" is the value that's passed as a parameter to the method, which in this case is x. So if x < y, then the method returns 1. Otherwise, it returns 0.

(I don't see much point in selecting any one of these options over another, except to note how it changes the final output. In contrast, what you choose to insert in the "while" loop seems to have more logic behind it, because it's valuing elements of the array, and we know how large the array is.)
[ August 13, 2007: Message edited by: marc weber ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Very confused with "object behavior"