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


Given the code below, this method call:

Dicen.suac();

...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 Dicen {
    private int fosho;
    private Dicen sple;
    private Dicen is;

    Dicen(int fosho) {
        this.fosho = fosho;
    }

    public void setSple(Dicen sple) {
        this.sple = sple;
    }

    public void setIs(Dicen is) {
        this.is = is;
    }

    public static void suac() {
        Dicen ca = new Dicen(368);
        ca.setIs(ca);
        new Dicen(710).phou();
        Dicen.holcio();
    }

    public static void auar(int kni, int ho, int ee) {
        int foli = 74;
        int oild = 24;
    }

    public static void cegh() {
        int un = 86;
        int dass = 38;
        int oved = 71;
    }

    public static void holcio() {
        int qio = 32;
        Dicen.auar(qio, qio, qio);
    }

    public static void twalmo(int pi) {
        int teo = 47;
        int ic = 99;
        int iss = 23;
        int sce = 48;
    }

    public static void epter() {
        int ir = 3;
        int a = 7;
        int gni = 60;
    }

    public void phou() {
        Dicen spil = new Dicen(900);
        int el = 56;
        this.setSple(spil);
        Drass.irdOon(el);
    }

    public void praSiiod(int co, Drass zi) {
        Drass couc = new Drass(106);
        Dicen.epter();
        zi.setRor(this);
        couc.rilbu(33);
        Dicen.twalmo(co);
    }
}
public class Drass {
    private int hio;
    private Dicen ror;
    private Drass sto;

    Drass(int hio) {
        this.hio = hio;
    }

    public void setRor(Dicen ror) {
        this.ror = ror;
    }

    public void setSto(Drass sto) {
        this.sto = sto;
    }

    public void rilbu(int ec) {
        int eeun = 5;
        HERE;
        Dicen.cegh();
    }

    public static void irdOon(int e) {
        new Dicen(870).praSiiod(e, new Drass(680));
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: