This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

code

 
Citra Dil
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Can anyone explain me how the value for m4a[x].counter , m4a[x].maybeNew(x) are calculated?
Say when x=0,x=1
thanks.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way to tackle this problem is with a piece of paper and a pencil.

You'll need an eraser, too.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which bits don't you understand ?
Try putting some System.out.println statements in at various points so that you can see the value of the various variables as the program proceeds.
This should make it fairly clear what is going on. If you don't understand any of the output, post what results you are getting and what you don't understand about them and someone should be able to explain it.

Did you copy this code from somewhere ? If so, you might want to check that you copied lines 19 and 20 correctly. They don't make much sense - you create an object, modify one of it's fields and then let it be garbage collected.
 
Citra Dil
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have used eclipse debugger and I know out put in each places.I also used a pen and paper but I am getting stuck because i dont understand few steps.Joanne,lines are correct but may be not relevant because this code is to learn the basics thoroughly.i will list few of my doubts

1.As counter is defined before while loop, m4a[x].counter = 0 always isnt it?
also m4.counter = 0 always isnt it?

2.I did not understand how the method maybeNew(x) works
though the value of x (index) changes, return 1 gives the total outcome as "1" isnt it?

3.for eg. when x=0,
count = count + m4a[0].maybeNew(0)
index < 5
I am stuck here.



 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Citra Dil wrote:
1.As counter is defined before while loop, m4a[x].counter = 0 always isnt it?
also m4.counter = 0 always isnt it?

Yes. the reason is you are creating a new object every time.
Citra Dil wrote:
2.I did not understand how the method maybeNew(x) works
though the value of x (index) changes, return 1 gives the total outcome as "1" isnt it?
3.for eg. when x=0,
count = count + m4a[0].maybeNew(0)
index < 5
I am stuck here.

here i dont get your point. probably you are trying to tell that how this m4a[x].maybeNew(x); works?
if yes, in while loop you are creating a new object and place the object in index of x , and then you are accessing the object by m4a[x] then executing the method. and whatever value passing to the method < 5 then method returns 1 else 0. the object/m4 created by method maybeNew will be garbage collected.


 
Citra Dil
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi seetharaman, thanks for your quick reply.But I guess you are repeating my question in your answer.May be I am not able to express what exactly I need.
My doubt is: In all cases when x=0,x=1.... till x=4 ,m4a[x].maybeNew(x) = 1.Isnt it?

I am able to verify the values for count,x in eclipse.
Can anyone tell me is there anyway to display the value for the below in any tool?
1)counter
2)m4a[x].counter
3)m4a[x].maybeNew(x)
4)m4.counter

few more info.the below are the results I get.

count = 14 ,m4a[1].counter = 1


count = 25 ,m4a[1].counter = 1


count = 14 ,m4a[1].counter = 1


count = 20 ,m4a[1].counter = 1

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic