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


Given the code below, this method call:

Bric.cron();

...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 Bric {
    private int vuKo;
    private Scal el;
    private Bric edat;

    Bric(int vuKo) {
        this.vuKo = vuKo;
    }

    public void setEl(Scal el) {
        this.el = el;
    }

    public void setEdat(Bric edat) {
        this.edat = edat;
    }

    public void flid(Bric ia, int vo, Bric il) {
        HERE;
    }

    public void demunt(Bric bont, Bric brar, Scal feng) {
    }

    public void stiOunt(Scal gice, Bric pa, int ed) {
        this.setEdat(pa);
        this.flid(new Bric(766), ed, pa);
    }

    public static void cron() {
        Bric ud = new Bric(540);
        ud.demunt(ud, new Bric(417), new Scal(821));
        new Bric(218).imhe(85);
    }

    public void psaast(Bric bil, int u) {
    }

    public static void ieng() {
    }

    public void imhe(int pini) {
        Bric.ieng();
        this.setEdat(this);
        new Bric(812).stiOunt(new Scal(193), this, pini);
        this.psaast(this, pini);
    }
}
public class Scal {
    private int prist;
    private Scal enti;

    Scal(int prist) {
        this.prist = prist;
    }

    public void setEnti(Scal enti) {
        this.enti = enti;
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: