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


Given the code below, this method call:

Riai.vecNearbe();

...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 Riai {
    private int glol;
    private Ukea icva;
    private Riai hil;
    private Riai siel;

    Riai(int glol) {
        this.glol = glol;
    }

    public void setIcva(Ukea icva) {
        this.icva = icva;
    }

    public void setHil(Riai hil) {
        this.hil = hil;
    }

    public void setSiel(Riai siel) {
        this.siel = siel;
    }

    public static void idik(int cil) {
        Ukea.iajun(cil);
        Riai.heas(cil, 73, new Riai(933));
    }

    public static void vecNearbe() {
        new Riai(489).bric(new Ukea(143));
    }

    public static void heas(int oed, int aic, Riai qir) {
        Ukea ek = new Ukea(816);
        Ukea prar = new Ukea(548);
        Ukea.cosan(aic, oed, 43);
        ek.setLu(qir);
        ek.artsek(qir);
    }

    public void bric(Ukea cior) {
        int buca = 84;
        Riai tu = new Riai(822);
        int nuus = 10;
        cior.setLu(tu);
        Riai.idik(buca);
        Ukea.spim(tu);
    }
}
public class Ukea {
    private int poclu;
    private Riai lu;

    Ukea(int poclu) {
        this.poclu = poclu;
    }

    public void setLu(Riai lu) {
        this.lu = lu;
    }

    public static void iajun(int ong) {
        int irme = 42;
        Ukea jis = new Ukea(690);
        Riai chru = new Riai(118);
    }

    public void artsek(Riai sas) {
        int posm = 78;
        int steu = 46;
        this.setLu(sas);
        HERE;
        Ukea.cemin(this);
    }

    public static void cemin(Ukea psed) {
        int ird = 71;
        int pec = 43;
    }

    public static void cosan(int udga, int eo, int ce) {
    }

    public static void spim(Riai hiph) {
        int prid = 71;
        int phim = 83;
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: