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


Given the code below, this method call:

Giok.mewhe();

...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 Giok {
    private int chec;
    private Giok cec;
    private Alles siid;
    private Giok ipat;

    Giok(int chec) {
        this.chec = chec;
    }

    public void setCec(Giok cec) {
        this.cec = cec;
    }

    public void setSiid(Alles siid) {
        this.siid = siid;
    }

    public void setIpat(Giok ipat) {
        this.ipat = ipat;
    }

    public static void mewhe() {
        new Alles(251).kuwli(new Giok(873));
    }

    public void otoc(Alles ral, Giok ecat) {
        int gic = 78;
        ecat.setIpat(this);
        ral.brosm(ecat, gic, new Alles(241));
    }

    public void esslek() {
        Alles.vissne(new Alles(428));
        HERE;
        Alles.nalEsviss(this);
    }

    public void funnil() {
    }
}
public class Alles {
    private int sof;
    private Alles leso;

    Alles(int sof) {
        this.sof = sof;
    }

    public void setLeso(Alles leso) {
        this.leso = leso;
    }

    public void brosm(Giok iset, int ugn, Alles enu) {
        enu.setLeso(this);
        new Giok(325).esslek();
        new Giok(685).funnil();
    }

    public static void nalEsviss(Giok an) {
        Giok mic = new Giok(245);
        Alles nion = new Alles(648);
        Giok scro = new Giok(474);
    }

    public static void gial(Giok swui, Giok eno, Alles fa) {
    }

    public static void vissne(Alles vid) {
    }

    public void kuwli(Giok vi) {
        Alles.gial(vi, new Giok(374), this);
        vi.setSiid(this);
        new Giok(572).otoc(this, vi);
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: