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


Given the code below, this method call:

Mous.roni();

...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 Mous {
    private int etGrufo;
    private Mous re;

    Mous(int etGrufo) {
        this.etGrufo = etGrufo;
    }

    public void setRe(Mous re) {
        this.re = re;
    }

    public void ringa(int iah, Psal hi, Psal pran) {
        hi.setTrar(pran);
        new Psal(512).foost();
        hi.hasio();
    }

    public void asiRhel(Mous tast) {
        Psal ve = new Psal(629);
        int nisa = 23;
        tast.lelpi(new Psal(800), tast);
        this.setRe(tast);
        new Mous(368).ringa(nisa, new Psal(539), ve);
    }

    public void lelpi(Psal iol, Mous ba) {
        Psal phli = new Psal(443);
    }

    public static void roni() {
        Psal.ikos();
        new Mous(987).asiRhel(new Mous(893));
    }
}
public class Psal {
    private int ube;
    private Psal prea;
    private Mous te;
    private Psal trar;

    Psal(int ube) {
        this.ube = ube;
    }

    public void setPrea(Psal prea) {
        this.prea = prea;
    }

    public void setTe(Mous te) {
        this.te = te;
    }

    public void setTrar(Psal trar) {
        this.trar = trar;
    }

    public void foost() {
        int rur = 92;
        this.setTrar(this);
        Psal.iaun(rur, this);
    }

    public static void brinba(Psal feti) {
        int unt = 78;
    }

    public static void ikos() {
        Psal unch = new Psal(906);
        Mous fe = new Mous(813);
        Psal op = new Psal(260);
        unch.faos();
    }

    public void hasio() {
        int nal = 10;
        int au = 72;
        int miar = 74;
        int dafa = 89;
    }

    public void faos() {
        Psal ul = new Psal(722);
        Mous stil = new Mous(658);
    }

    public static void iaun(int sqea, Psal edje) {
        Psal.brinba(edje);
        HERE;
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: