• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help understanding the output

 
Aurika Vegana
Greenhorn
Posts: 11
Linux Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



I don`t get it why the output is 6.

Hope anyone can give me a hint to figure it out.

Thank you in advance
 
Winston Gutkowski
Bartender
Pie
Posts: 10087
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aurika Vegana wrote:Hope anyone can give me a hint to figure it out...

Hi. I've added indentation to your code and spaced it out a bit. See how much easier it is to read?
That's your first hint.

Second: The 'a' you ask about is the 'a' created in the previous line. The Foo constructor takes an int and a Foo.

So, now explain to me what happens when program gets to c.getV(). What will the method do?
More specifically:
  • what is the value of 'value' in c.getV()?
  • what is the value of 'other' in c.getV()?

  • Winston
     
    Anayonkar Shivalkar
    Bartender
    Posts: 1557
    5
    Eclipse IDE Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello Aurika Vegana,

    Welcome to CodeRanch!

    Its good to see that you've used code tags in your very first post. Also, its nice to ask for hints (instead of actual answers).

    Now, coming to your question.

    Aurika Vegana wrote:it seems like a never ending loop to me

    Please have a close look. Is it a loop at all?

    Aurika Vegana wrote:returns 3 right?

    Technically, it will create a new object of class Foo and reference 'a' will refer to that.

    Aurika Vegana wrote:what is "a" here actually? 3?

    Again, a is a reference to an object of class Foo.

    I would suggest to debug the code, and/or try to decode the algorithm/logic of getV method. What does it return? What does 'x * value' means? How a new Foo object is created (and how its members are initialized)?

    Once you get this, its not difficult to understand why the program prints that particular value.

    I hope this helps.
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12083
    29
    Chrome Java Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Personally, I think that "System.out.println()" is your best friend when trying to figure out what code is doing. Stick them in all over the place. print out what variables are at different places. print out boolean conditions. print out what value and other is in each object after you create them. print out what 'other' is inside getV, print out what other.getV(); gives you when you call it...

    put some in, take some out, move around stuff...put it as the first and last line of each method to see exactly how many times each method is REALLY called...

    it's easy, it's powerful, it's fast, and it lets YOU figure out the code, which is infinitely more gratifying than having someone explain it to you...plus, you'll remember it better and learn how to debug code yourself. you won't always be able to ask someone else for help.
     
    Aurika Vegana
    Greenhorn
    Posts: 11
    Linux Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for your quick replies and patience =)

    I thought:
    First a new Foo class object is created with int = 3 and other= null, "a" refers to that.
    So than
    this
    public int getV() {
    int x = 1;
    if (other != null) {
    x = other.getV();
    }
    return x * value;

    will return 3?

    Than we move on to "b", with int=2 and other=a.

    Running through getV:
    if (other != null) {
    x = other.getV();
    }
    other is a, not null, so we have x= a.getV . In "a" Foo we have other = null, so return x * value ---> will be 2, as in "b" Foo value is 2.


    In "c" Foo we have int= 1, other= b.
    When going through getV:
    other is b, not null, so we have b.getV. In "b" Foo other = a , not null, so a.getV with other = null will return x *value will be 1, as in "c" value is 1.

    c.getV , with int=1, other= b :

    here I`m stucked a bit.


    I would suggest to debug the code, and/or try to decode the algorithm/logic of getV method. What does it return? What does 'x * value' means? How a new Foo object is created (and how its members are initialized)?


    How to debug/ decode with no extra software for it?

    @fred:
    I posted the code in browxy.com with System.out.println a, b etc., it returned a@1be2d65 (sthm. like this)
    Couldn`t get me further, so I posted my question here then.


     
    Anayonkar Shivalkar
    Bartender
    Posts: 1557
    5
    Eclipse IDE Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Aurika Vegana wrote:I posted the code in browxy.com with System.out.println a, b etc., it returned a@1be2d65 (sthm. like this)

    You'll need to override toString method to get more readable output from 'println' method
     
    Farhat Am
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The first call to getV() is made by object c where:
    other = b, value = 1 (from object c), x = b.getV()

    In b.getV():
    other = a, value = 2 (from object b), x = a.getV()


    In a.getV():
    other = null, value = 3 (from object a), x = 1
    so the value returned from a.getV() us 3 * 1 = 3

    3 will be received by b.getV() in x. There value = 2 and returned value is 3 so new value to be returned becomes 3 * 2 = 6

    6 will be received by c.getV() in x. There value = 1 and returned value is 6 so new value to be returned becomes 6 * 1 = 6

    Hope it helps
     
    Anayonkar Shivalkar
    Bartender
    Posts: 1557
    5
    Eclipse IDE Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello F Am,

    Welcome to CodeRanch!

    Please UseRealName.

    Also, please note that this is NotACodeMill, and LetThemDoTheirOwnHomework
     
    Farhat Am
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    As I am new to this forum, may b I went too far in explaining the solution. I will be careful next time.
    Thanks
     
    Anayonkar Shivalkar
    Bartender
    Posts: 1557
    5
    Eclipse IDE Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Farhat Am wrote:As I am new to this forum, may b I went too far in explaining the solution. I will be careful next time.
    Thanks

    That is fine. Just make sure that you go through FAQs before asking/answering questions.
     
    Aurika Vegana
    Greenhorn
    Posts: 11
    Linux Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Farhat Am,

    thank you very much for you detailed solution, now I understand it finally and can move on.

    ;))


    Aurika
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic