Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a problem with the resolt of the code

 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is how i think this code should work
i put it also in the debugger
and it also shows the resolt 11 2 7

and then when it goes to the print method suddenly it shows
a=7 b=2 c=7

why??

i showed here the diagram which represents my logic

http://img401.imageshack.us/my.php?image=img8298gp7.jpg

where did i go wrong??

 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope you never hand in such convoluted code for an examination.

You are setting the parameter a equal to 11 in the two-arguments constructor, not the field, which would be this.a
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, this code should not compile. Class A doesn't implement interface C. But, assuming that you posted the wrong code, and everything else is correct...

this is how i think this code should work
i put it also in the debugger
and it also shows the resolt 11 2 7


Well, I highly doubt that... There are no timing issues (shown in the code) related to this application that would make an actual run different from a debugging run.

and then when it goes to the print method suddenly it shows
a=7 b=2 c=7

why??


Well, 7 2 7 is correct. And here is why...



In step 6 of your diagram, you have this line as assigning the value of the parameter b to the instance variable a. This is not true. This line assigns the value of the parameter b to the parameter variable a. To do what you want expect in step 6, you need to do this...



Henry
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i forgot to mention that the first part of the question was
to fix the code

i added abstract in the the signature of class A

is that changes some how your answers??
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by donaldth smithts:

is that changes some how your answers??


That explains why it compiles. None of the other answers changes.

Henry
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so you are saying that in step 6

that i changed the input variable "a"(a variable which exists only in the signature)??
and not this.a??

my next question (presumption) is that in step 5 i made a similar
command
but here b.a do change although its a=b too
because the signature is public B(int b)
there is no a there so we change the b.a variable
am i correct
[ February 17, 2008: Message edited by: donaldth smithts ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by donaldth smithts:
so you are saying . . .
my next question (presumption) is that in step 5 i made a similar
command
but here b.a do change although its a=b too
because the signature is public B(int b)
there is no a there so we change the b.a variable
am i correct?


Yes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic