aspose file tools*
The moose likes Beginning Java and the fly likes A Heap O� Trouble :-- Head First Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A Heap O� Trouble :-- Head First Java" Watch "A Heap O� Trouble :-- Head First Java" New topic
Author

A Heap O� Trouble :-- Head First Java

iti garg
Greenhorn

Joined: Oct 03, 2008
Posts: 9
Please help me solving this problem. Actually i did not get this.

A Heap O� Trouble---

A short java program is given. When �// do stuff� is reached, some objects and reference variables have been created. Your task is to which of the reference variables refer to which objects. Not all the reference variables will be used, and some objects might be referred to more than once. Draw lines connecting the reference variables with their matching objects.

Class HeapQuiz {
int id = 0;
public static void main(String [] args) {
int x = 0;
HeapQuiz [] hq = new HeapQuiz[5];
While(x<3) {
Hq[x] = new HeapQuiz();
Hq[x].id = x;
x = x+1;
}
hq[3] = hq[1];
hq[4] = hq[1];
hq[3] = hq[1];
hq[4] = hq[0];
hq[0] = hq[3];
hq[3] = hq[2];
hq[2] = hq[0];
// do stuff
}
}

Given->
Reference Variables:
hq[0]
hq[1]
hq[2]
hq[3]
hq[4]

HeapQuiz Objects:
Id=0
Id=1
Id=2
[ October 28, 2008: Message edited by: iti garg ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Don't know the answer, and haven't got that book, but the idea of those quizzes is that you have to work out which is the possibilities will fit in which space. It is a bit like a jigsaw puzzle. We shall be pleased to help if you post what you thought was the answer (even if incomplete) and where it differs from what Sierra and Bates said the answer was.

And please use the code tags; your code is difficult to read without them.
iti garg
Greenhorn

Joined: Oct 03, 2008
Posts: 9
My problem is this----
this is head first java problem of chapter-3

A Heap O� Trouble---

A short java program is given. When �// do stuff� is reached, some objects and reference variables have been created. Your task is to which of the reference variables refer to which objects. Not all the reference variables will be used, and some objects might be referred to more than once. Draw lines connecting the reference variables with their matching objects.

}

Given->
Reference Variables:
hq[0]
hq[1]
hq[2]
hq[3]
hq[4]

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


first of all please tell me what does this line mean---

Hq[x].id = x;

I dont understand meaning of this line.
Ken Kisser
Greenhorn

Joined: Oct 19, 2008
Posts: 11
Originally posted by iti garg:
first of all please tell me what does this line mean---

Hq[x].id = x;

I dont understand meaning of this line.[/QB]


Howdy iti

This code ostensibly (I'll get back to why I say ostensible in a bit) makes an array of 5 objects of type HeapQuiz. Each of these objects has its own copy of the instance variable id. To access the variable id of a particular object we identify the object Hq[x] followed by a "dot" followed by the name of the variable. Thus we are saying: "In the array Hq assign the value of the local variable x to the value of the instance variable id belonging to the object of type HeapQuiz stored at the location [x] in the array." So if x equaled 0 we would ostensibly be saying "Assign 0 to the id of the HeapQuiz object stored at Hq[0] (the first position in the HQ array)."

Now if I were trying to figure out "which of the reference variables refer to which objects" I would type the code into a text editor, at the end of the code add a bunch of clunky statements to show me the values of each of the elements of the array, something like:

system.out.println("Variable id of hq element 0 = " + hq[0].id);

etc. and then compile & run the code.

The programmers I respect the most & who seem to know the most are like the hero of those "Bourne" movies. They trust no one. Not their teachers, not the book, not other programmers. They check the answer for themselves by running code. Also, ditto what Campbell said. He is one of the programmers I am talking about. Did I just say he was like Matt Damon?!

If you do that in this case, you will find out what I mean by "ostensible". I'll give you a hint: Java is case sensitive. I'll give you another hint too: There are at least two other compile-time errors in the code as you posted it, also due to "case insensitivity." How do I know? I typed the code in & tried to run it.
[ October 29, 2008: Message edited by: Ken Kisser ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
I haven't seen Bourne, Ken, and what makes you think I trust nobody?
iti garg
Greenhorn

Joined: Oct 03, 2008
Posts: 9
Hello Ken,
sorry i could not understand whatever you explianed me.
I compiled this code and print the value of hq[0].id

system.out.println("Variable id of hq element 0 = " + hq[0].id);

It's value comes out zero.

please explain me with some other example.
Ken Kisser
Greenhorn

Joined: Oct 19, 2008
Posts: 11
Howdy all

First let me get some apologies out of the way.

I apologize to Campbell for being impertinent, impudent, presumptuous & untowardly familiar on such short acquaintance, for "blabbing like a narcissistic fool with a superiority complex", to quote Mr. Ess.

And I apologize to you iti for not practicing what I preach. It should be:

System.out.println("Variable id of hq element 0 = " + hq[0].id);

as per some narcissistic fool's blabbering about Java's case sensitivity.

Re.


Hello Ken,
sorry i could not understand whatever you explianed me.
I compiled this code and print the value of hq[0].id

system.out.println("Variable id of hq element 0 = " + hq[0].id);

It's value comes out zero.

please explain me with some other example.



Now, iti, when I run the code you originally posted, with the capitalizations corrected so it would compile, & with the above println of the value hq[0] added, I get the following output:

Variable id of hq element 0 = 1

which is what I would have guessed, since hq[0] = hq[3] = hq[1] & hq[1] was set to to the value 1 by the loop.

Here is the code I used to get the above result(hopefully I formatted to to JR standards):





Since you got different results, why don't you post YOUR code iti, & we can talk about it some more.
[ November 02, 2008: Message edited by: Ken Kisser ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18135
    
    8

Originally posted by Campbell Ritchie:
I haven't seen Bourne, Ken, and what makes you think I trust nobody?
Wasn't "Matt Damon" supposed to refer to the character from Good Will Hunting?
Ken Kisser
Greenhorn

Joined: Oct 19, 2008
Posts: 11
I think that's what he won his Academy Award for but later he made the "Bourne" series of movies. Alas I must watch all his movies multiple times as they come out as per my Otaku Girl because "he is so cute - for an old guy."
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
I accept the apology, since we know it was his Otaku Girl who told him to apologise, and Ken always does exactly as she tells him.
iti garg
Greenhorn

Joined: Oct 03, 2008
Posts: 9
Hello Ken,
My code is...



By this code my ans comes out zero.
please check it once and then explain it to me.
Ken Kisser
Greenhorn

Joined: Oct 19, 2008
Posts: 11
Originally posted by Campbell Ritchie:
Ken always does exactly as she tells him.


I want to live, Campbell. I want to live.
Ken Kisser
Greenhorn

Joined: Oct 19, 2008
Posts: 11
Hi iti

Take out the /* & the */ & see what you get.
Those are block comments. Anything between them is considered a comment & ignored by the program. You'll notice my code does not have them, nor does your original posting. Also put some more printlns(if you want to check the values of the array elements after the assignments that are currently commented out by the /* & */) as the last lines of code inside the curly braces. Where you have yours right now the value of h[0] SHOULD be 0 since the loop set it to that.

kk
[ November 05, 2008: Message edited by: Ken Kisser ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Originally posted by Ken Kisser:


I want to live, Campbell. I want to live.


Fantine Ponter
Ranch Hand

Joined: Aug 11, 2008
Posts: 30
The object of the exercise in Head First, chapter 3, Heap o' Trouble, was to test whether the reader understands how objects become 'unreferenced', 're-referenced' and therefore could possibly become available for garbage collection. for example:
When playing the part of the JVM (pretend that the code is perfect):

Reference Variables Heap Quiz Objects
hq[0] references id=0,

but later that reference is removed, and therefore hq[0] has a null value (no reference to an object), but the object, id=0 gets referenced by hq[4].

So, to conclude, the final links to the 3 Heap Quiz Objects, would be from hq[1] ----> id=1; hq[3] ----> id=1; and hq[4] ----> id=0. hq[0] and hq[2] are null.
iti garg
Greenhorn

Joined: Oct 03, 2008
Posts: 9
Hi Ken,
Sorry i did not notice that comments.
I have run that code after removing comments. You are absolutely right Ken
hq[0].id comes out to be 0
hq[1].id comes as 1
hq[2].id as 2

Thanks.
iti garg
Greenhorn

Joined: Oct 03, 2008
Posts: 9
Hello Fantine,
Sorry i could not understand what are you saying, please explain me with any other example if possible.
Fantine Ponter
Ranch Hand

Joined: Aug 11, 2008
Posts: 30
Hi Iti
I apologise for for throwing a spanner in the works.
Just ignore what I said. Ken has supplied all the solutions that you were looking for.
Happy coding!
Ken Kisser
Greenhorn

Joined: Oct 19, 2008
Posts: 11
You are welcome iti (My internet was not talking to me for a few days)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A Heap O� Trouble :-- Head First Java
 
Similar Threads
Understanding Objects and Reference Variables
matching reference variables with objects
Array quiz I can't solve
Someone teach me line by line how to read this particular code
Head First into Java - A Heap o Trouble