to problem set

Problem 2 - Code comprehension

    public class Foo {
        public int x;
        public int y;

        public Foo(int a) {
            x = a;
            y = 1;
        }

        public int bar(int a) {
            x += a;
            y += 1;
            return (x + y);
        }

        public void bar(Foo f) {
            if (f.x < x) {
                f.bar(x);
            } else {
                bar(f.x);
            }
        }
    }

Fill in the table based on the following

1: Foo f1 = new Foo(1);
2: Foo f2 = new Foo(2);
3: f1.bar(3);
4: f2.bar(4);
5: f1.bar(f2.bar(5));
6: f2.bar(f1);
7: f1.bar(new Foo(6));

So we are going to walk through the values after every call.

By the time we get to line 3 we have the following information

Line # f1.x f1.y f2.x f2.y
start 1 1 2 1

Line 3 gets executed: f1.bar(3);

Line # f1.x f1.y f2.x f2.y
3 4 2 2 1

Line 4: f2.bar(4);

Line # f1.x f1.y f2.x f2.y
4 4 2 6 2

line 5: f1.bar(f2.bar(5));

Line # f1.x f1.y f2.x f2.y
5 18 3 11 3

Line 6: f2.bar(f1);

Line # f1.x f1.y f2.x f2.y
6 18 3 29 4

line 7: f1.bar(new Foo(6));

Line # f1.x f1.y f2.x f2.y
7 18 3 29 4