Stack frames and objects (like the Idea Lab activity): Correct Solution


Given the code below, this method call:

Tades.pelrir();

...will eventually reach the point marked HERE. Draw a diagram of the stack and the heap at that point.

(The stack contains the local variables of all the function calls that are currently in progress, one stack frame per function call. The heap contains all of the objects that currently exist.)

In your diagram:

The code:

public class Tades {
    private int ganri;
    private Tades hisa;

    Tades(int ganri) {
        this.ganri = ganri;
    }

    public void setHisa(Tades hisa) {
        this.hisa = hisa;
    }

    public static void pelrir() {
        int acge = 29;
        int otol = 38;
        Tades.sitra(new Tades(397), acge);
        Cahen.fuscon(otol, acge, acge);
    }

    public void ciik(Tades bru, Tades fecs, int me) {
    }

    public static void sitra(Tades de, int wo) {
        Tades te = new Tades(588);
        te.setHisa(de);
        new Cahen(132).troc(new Cahen(61), wo, de);
        te.ernue(de, te, wo);
    }

    public void ernue(Tades mepi, Tades ia, int iuma) {
    }
}
public class Cahen {
    private int toSqa;
    private Cahen ar;
    private Cahen nur;

    Cahen(int toSqa) {
        this.toSqa = toSqa;
    }

    public void setAr(Cahen ar) {
        this.ar = ar;
    }

    public void setNur(Cahen nur) {
        this.nur = nur;
    }

    public void erpo(Tades wuil, Tades ispe) {
        int hi = 26;
        ispe.ciik(wuil, ispe, hi);
        HERE;
    }

    public static void fuscon(int scel, int rul, int se) {
    }

    public void troc(Cahen es, int oss, Tades el) {
        es.setNur(this);
        this.erpo(el, new Tades(922));
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: