javatracediagramming

What sort of diagramming method could I use to keep everything organized while tracing through this program?


Here is one of my self-study homework problems.

I am supposed to write down the output of this program without actually running it. I understand all the syntax and the variable-passing here, (and I have the answers) but for some reason, tracing it out on paper isn't working.

Is there some secret diagramming technique that keeps everything organized?

Is figuring this out by hand mere child's play to an experienced programmer?

Thanks

public class Mystery extends ConsoleProgram {
public void run() {
    ghost(13);
}

private void ghost(int x) {
    int y = 0;
    for (int i = 1; i < x; i *= 2) {
        y = witch(y, skeleton(x, i));
    }
    println("ghost: x = " + x + ", y = " + y);
}

private int witch(int x, int y) {
    x = 10 * x + y;
    println("witch: x = " + x + ", y = " + y);
    return x;
}

private int skeleton(int x, int y) {
    return x / y % 2;
}
}

Solution

  • You can unroll loops:

    for (int i = 1; i < 13; i *= 2) {
        E[i]
    }
    

    becomes

    E[1]; E[2]; E[4]; E[8];
    

    There's no recursion here, so the rest should be straightforward.