Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Head First into Java - A Heap o Trouble

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 76487
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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."
 
Joe Sarge
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

Capture.JPG
[Thumbnail for Capture.JPG]
Snapshot of Microsoft Word
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahhhhh.....Eureka! Thank you Mr Weber! I am sure we will be in touch! lol
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad that helped!
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

marc weber wrote: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."



I just pick this book after a few years. I have a good understanding once you explained it. But im a bit confuse with one. Hq2=Hq0, H2q should be assign to id0 right.
 
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I don't think so.

What happened when this line executed earlier?

hq[0] = hq[3];

What was the result of that?

After that executes, what will the value of hq[0]  be for the rest of the method?
 
alex lesta
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im sorry im still not following.
 
alex lesta
Ranch Hand
Posts: 30
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:No, I don't think so.

What happened when this line executed earlier?

hq[0] = hq[3];

What was the result of that?

After that executes, what will the value of hq[0]  be for the rest of the method?



I finnaly figure it out. I was a bit confuse for a moment. H2 will have the value of null because it was pass to h0 from h3. H0 passed it to H2. Bang lmao.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic