aspose file tools*
The moose likes Beginning Java and the fly likes Head First into Java - A Heap o Trouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First into Java - A Heap o Trouble" Watch "Head First into Java - A Heap o Trouble" New topic
Author

Head First into Java - A Heap o Trouble

Joe Sarge
Greenhorn

Joined: Jan 23, 2011
Posts: 7
I am having such an issue with the " A heap o trouble" exercise in the Head First into Java book. It involves Reference variables and Objects in an array list. I noticed that a couple of years ago, you posted some comments on this exercise.



Given->

Reference Variables:
hq[0]
hq[1]
hq[2]
hq[3]
hq[4]

HeapQuiz Objects:
Id=0
Id=1
Id=2

Match the reference variables to the objects. Now I get the concept on arrays, but I would really appreciate an easy, simple explanation as to what I should be comparing to get the answers. Do I go through the while loop? Do I compare the list of variables given in the code? I am so confused on this. I have the answers from the book. So please do not just give me the answers. I am more concerned about HOW to arrive at the answers. The problem I am having with this book is the lessons are easy to understand, like 10th grade. But then the quizes and puzzles are like 3rd year college stuff! BTW, the answers are as follows:
hq[1] id=1
hq[3] id=2
hq[4] id=0
The rest of the Reference variables aren't used.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36574
    
  16
Get a pencil and draw 5 squares, and put the values in the 5 references in them.
You will have to go through the while loop similarly to see which values go where, and it isn't obvious just looking at the code what will happen.
Then copy the numbers into each square as the objects are re-assigned. It only takes a few moments and will give the correct answer, but you have to be methodical. And the 0 and 2 objects are changed.

And welcome to JavaRanch Please use the code button and maintain indentation; since you are new I have edited your post and you can see how much better it looks.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to the Ranch!

The idea here is for you to work through the code line by line, to help understand how a computer would execute this. So let's walk through this one.

The first thing to notice is the class HeapQuiz has an instance variable called "id." So each new instance (object) of HeapQuiz will have its own id.

Now, let's work through the logic in the main method...

An int called x is set to 0.

An array called hq is created with a length of 5. This array holds references of the type HeapQuiz. Initially, each of these references is null.

The block of code in the while loop will execute repeatedly, as long as x remains less than 3.

At this point, x is 0, so the block executes. A new instance of HeapQuiz is created and assigned to hq[0]. This id in this instance of HeapQuiz is set to 0. Then x is incremented to 1.

Now, x is 1, so the while block executes again. Another new instance of HeapQuiz is created and assigned to hq[1]. This id in this instance of HeapQuiz is set to 1. Then x is incremented to 2.

Now, x is 2, so the while block executes again. Another new instance of HeapQuiz is created and assigned to hq[2]. This id in this instance of HeapQuiz is set to 2. Then x is incremented to 3.

Now, x is 3, so the while block does not execute, because x<3 evaulates to false.

At this point, hq[0], hq[1], and hq[2] each reference a separate instance of HeapQuiz. And each instance of HeapQuiz has a variable id assigned a different value. So...

hq[0].id is 0
hq[1].id is 1
hq[2].id is 2
hq[3] is null
hq[4] is null

The next 7 lines of code just reassign references. It's kind of a shell game, so you need to keep your eyes on which objects are where.

With hq[3] = hq[1], the reference in hq[1] (which points to the object with id=1) is assigned to hq[3]. So now...

hq[0].id is 0
hq[1].id is 1
hq[2].id is 2
hq[3].id is 1
hq[4] is null

With hq[4] = hq[1], the reference in hq[1] (which points to the object with id=1) is assigned to hq[4]. So now...

hq[0].id is 0
hq[1].id is 1
hq[2].id is 2
hq[3].id is 1
hq[4].id is 1

Try working through the next few lines on your own, and see what you end up with.

Note: Java is case sensitive, so "Class" needs to be "class," "While" needs to be "while," and "Hq" needs to be "hq."


"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
Joe Sarge
Greenhorn

Joined: Jan 23, 2011
Posts: 7
Thanks for the quick responses guys. I haven't tried these yet, but i will let you know. Also, thanks for the coding tip as well. Wondered what that button was for! lol.
Joe Sarge
Greenhorn

Joined: Jan 23, 2011
Posts: 7
Ok, so here is what I have done.... I pretty much knew about the while loop and how all the objects were created. Where I kept getting into trouble is at the bottom of the code where all the assignment statements take place. So, here's what I tried:




[Thumbnail for Capture.JPG]

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

These array elements are references to objects. When we say...

...a copy of the reference in hq[1] is assigned to hq[3].

Nothing has "moved." hq[1] still holds its original reference, and now hq[3] holds a separate copy of that reference. So both hq[1] and hq[3] are pointing to the same object...

hq[0].id is 0
hq[1].id is 1
hq[2].id is 2
hq[3].id is 1
hq[4] is null

Note that hq[3] is not pointing to hq[1]. The reference in hq[3] is a distinct copy of what hq[1] held at the time of the assignment. This is important, because if we now change hq[1] to reference a different object, then hq[3] will not be effected.
Joe Sarge
Greenhorn

Joined: Jan 23, 2011
Posts: 7
Ahhhhh.....Eureka! Thank you Mr Weber! I am sure we will be in touch! lol
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Glad that helped!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Head First into Java - A Heap o Trouble
 
Similar Threads
Someone teach me line by line how to read this particular code
A Heap O� Trouble :-- Head First Java
Understanding Objects and Reference Variables
matching reference variables with objects
Array quiz I can't solve