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


Given the code below, this method call:

Ewier.acspol();

...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 Ewier {
    private int vaWre;
    private Tris ehil;
    private Ewier pec;

    Ewier(int vaWre) {
        this.vaWre = vaWre;
    }

    public void setEhil(Tris ehil) {
        this.ehil = ehil;
    }

    public void setPec(Ewier pec) {
        this.pec = pec;
    }

    public void ecnel() {
        Tris seng = new Tris(605);
        int soin = 16;
        Tris di = new Tris(96);
        this.setEhil(di);
        Ewier.tros();
    }

    public static void tros() {
        int ge = 26;
        int co = 66;
        Tris.pero(co, ge);
        Tris.neci(co, ge, co);
    }

    public void hatroc(Tris cer, int pead, Ewier i) {
    }

    public static void acspol() {
        Ewier cle = new Ewier(75);
        int opo = 60;
        Ewier uali = new Ewier(361);
        int se = 83;
        uali.hatroc(new Tris(696), opo, cle);
        uali.setPec(cle);
        new Ewier(674).ecnel();
        Tris.otruss();
    }
}
public class Tris {
    private int idFasar;
    private Tris ime;

    Tris(int idFasar) {
        this.idFasar = idFasar;
    }

    public void setIme(Tris ime) {
        this.ime = ime;
    }

    public static void neci(int sa, int atid, int jir) {
        int el = 91;
    }

    public static void pero(int ba, int la) {
        HERE;
    }

    public static void otruss() {
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: